如何在C中创建可遍历树数据结构

时间:2012-08-13 02:03:17

标签: c tree linked-list treenode traversal

有没有人有任何在C语言(!C ++)中迭代和递归地构建树数据结构的好例子?也遍历树并覆盖每个节点..

我可能会使用这个结构:

typedef struct treeNode *treeLink;
struct treeNode {
    Item item;
    treeLink left, right;
};

1 个答案:

答案 0 :(得分:1)

treeLink Traverse_In_Order(treeLink  current){
          if(curernt == NULL) return NULL;
          Traverse(current->left);
          operation(current->item);
          Traverse(current->right);
          return current; // in case you want to do something on this node
}


treeLink Traverse_Pre_Order(treeLink  current){
          if(curernt == NULL) return NULL;
          operation(current->item);
          Traverse(current->left);        
          Traverse(current->right);
          return current; // in case you want to do something on this node
}

treeLink Traverse_Pos_Order(treeLink  current){
          if(curernt == NULL) return NULL;
          Traverse(current->left);        
          Traverse(current->right);
          operation(current->item);
          return current; // in case you want to do something on this node
}