【发布时间】:2017-01-24 18:52:19
【问题描述】:
现在,我知道下面的代码仅适用于 root 及其子级,但我不知道如何扩展它。在传递“孙子”之前,每个节点都必须有子节点。谢谢。
void insert_node(IndexTree **root, Node *node) {
IndexTree *temp = (IndexTree*)malloc(sizeof(IndexTree));
memcpy(&temp->value.cs, node, sizeof(Node));
temp->left = NULL;
temp->right = NULL;
temp->tip=1;
if ((*root) == NULL) {
*root = temp;
(*root)->left = NULL;
(*root)->right = NULL;
}
else {
while (1) {
if ((*root)->right == NULL) {
(*root)->right = temp;
break;
}
else if ((*root)->left == NULL) {
(*root)->left = temp;
break;
}
}
}
【问题讨论】:
-
因为你只是将节点按固定顺序放置,所以这实际上是一个数组,而不是一棵树。