有没有人有任何在C语言(!C ++)中迭代和递归地构建树数据结构的好例子?也遍历树并覆盖每个节点..
我可能会使用这个结构:
typedef struct treeNode *treeLink;
struct treeNode {
Item item;
treeLink left, right;
};
答案 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
}