【发布时间】:2017-01-01 18:48:17
【问题描述】:
struct tree{
struct node* root;
};
typedef struct tree* Tree;
struct node{
int key;
struct node* left;
struct node* right;
};
typedef struct node* node;
我的问题是我需要实现的函数需要一个树而不是节点作为参数。
int count_occurrences(Tree t, int k){}
如果第一个参数是 Node 类型,我知道如何实现此函数,但由于它需要 Tree 类型,我无法弄清楚如何在递归调用中传递 Tree 类型参数。
编辑:还有另一个问题。我无法直接访问结构字段,因为它们是在另一个文件(学校项目)中声明的。我可以访问一些功能,例如获取树的根、节点的左子或右子等
Tree newtree();
int treeempty(Tree t);
Node root(Tree t);
int readInfo(Node v);
void setInfo(Node v, int x);
Node leftChild(Node v);
Node rightChild(Node v);
【问题讨论】:
-
你可以有一个接受树的前端函数和一个接受节点的递归函数。从前端调用带有根节点的递归函数。
-
为什么需要递归调用?只需从根开始循环遍历树,直到到达结尾(这可能由
rightbeeing NULL 指示)。另外,用 typedef 隐藏指针类型的事实可能会让人困惑。
标签: c struct tree binary-tree nodes