多元樹 (Tree) -- C 語言

程式作品

C 語言

Java

C#

JavaScript

常用函數

文字處理

遊戲程式

衛星定位

系統程式

資料結構

網路程式

自然語言

人工智慧

機率統計

資訊安全

等待完成

訊息

相關網站

參考文獻

最新修改

簡體版

English

檔案:Tree.h

#ifndef TREE_H
#define TREE_H

#include "Array.h"

typedef struct {
  char *type;
  char *value;
  Array* childs;
} Tree;

Tree* TreeNew(char *pType, char *pValue);
void TreeFree(Tree *tree);
void TreeAddChild(Tree *tree, Tree *child);
void TreePrint(Tree *tree);

#endif

檔案:Tree.c

#include <stdio.h>
#include "Tree.h"

Tree *TreeNew(char *type, char *value) {
  ASSERT(type != NULL && value != NULL);
  Tree *rzTree = ObjNew(Tree, 1);
  rzTree->type = (void*) newStr(type);
  rzTree->value = (void*) newStr(value);
  rzTree->childs = NULL;
//  printf("TreeNew(%s,%s)\n", type, value);
//  printf("address of tree->type=%d, tree->value=%d\n", rzTree->type, rzTree->value);
  return rzTree;
}

void TreeFree(Tree *tree) {
  if (tree == NULL) return;
  freeMemory(tree->type);
  freeMemory(tree->value);
  if (tree->childs != NULL) {
    int i;
    for (i=0; i<tree->childs->count; i++)
      TreeFree(tree->childs->item[i]);
    ArrayFree(tree->childs, NULL);
  }
  ObjFree(tree);
}

void TreeAddChild(Tree *tree, Tree *child) {        // 在樹中加入一個新的子樹。
  ASSERT(tree != NULL && child != NULL);
  if (tree->childs == NULL)
    tree->childs = ArrayNew(1);
  ArrayAdd(tree->childs, child);
}

void TreePrintLevel(Tree *tree, int level) { // 將該樹列印出來。
  ASSERT(tree != NULL && tree->type != NULL);
//  printf("address of tree->type=%d, tree->value=%d\n", tree->type, tree->value);
  printf("%s+%s\t = %s\n", strSpaces(level), tree->type, tree->value);
  if (tree->childs != NULL) {
    int i;
    for (i=0; i<tree->childs->count; i++)
      TreePrintLevel(tree->childs->item[i], level + 1);
    printf("%s-%s\n", strSpaces(level), tree->type);
  }
}

void TreePrint(Tree *tree) {
  TreePrintLevel(tree, 0);
}

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License