0

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

我可能会使用这个结构:

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

1 回答 1

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
}
于 2012-08-13T02:10:50.700 回答