首页 > 解决方案 > 我在删除 C 中的树数据结构中的节点时遇到问题

问题描述

删除函数中的代码适用于具有 1 个子节点和离开节点的节点,但是当我输入 12(节点有 1 个子节点)时,该节点仍然存在,删除函数是否有任何问题:

void Delete(struct Node *current, int data){
     struct Node *X;
     if((current == NULL)
         printf("Can not delete that node since the tree is empty");
     else{
         if(data > current->data)
             Delete(current->rightchild, data);
         else(data < current->data)
             Delete(current->leftchild, data);
         else{
             X = current;
             if(current->leftchild == NULL){
                 current = current->rightchild;
             }
             else(current->rightchild == NULL){
                 current = current->leftchild;
             }
             X = NULL;
          }
}

int main(){
    int tree[7] = {5, 4, 15, 3, 12, 20, 13};
    struct Node *current;
    int I = 0;
    for(; I < 7; ++I){
        Tree(tree[I]); // Tree is the tree-creating function
    }
    current = root;
    printf("Entering the node u want to delete: ");
    scanf("%d", &j);
    Delete(current, j);
    InOrder_Traversal(root); // this is the printing function

我希望输出 3、4、5、12、13、15、20 为 3、4、5、13、15、20,但是当我输入 12 时节点 12 仍然存在。

标签: c

解决方案


推荐阅读