現在,我明白下面的代碼只適用於root及其子項,但我不知道如何擴展它。每個節點在傳遞「孫子們」之前都必須有孩子。謝謝。如何從右向左插入C樹中的節點?
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;
}
}
}
既然你只是把節點放在一個固定的順序,這實際上是一個數組,而不是一棵樹。 – stark