从二进制搜索树中删除节点-错误的答案

时间:2018-11-15 23:01:12

标签: c++ binary-search-tree

我对二进制搜索树的删除代码给了我分段错误,我想知道问题出在哪里。

预购:64 8 4 32 16128512256 和运行的步骤是

删除4

删除64

删除128

,它应可预订:256 8 32 16 512

但我正在预订:256 8 0 32 16 512 9502512

我的代码在

下方
 bool IntBST::remove(int value){
     Node* n = getNodeFor(value, root);
 if(n == NULL){
     return false;
}

    else{
            if((n->right == NULL) && (n->left == NULL)){
                    if(n->parent->left == n){
                          n->parent->left == NULL;
                  }
                    else{
                            n->parent->right == NULL;
                    }
                    delete n;
                     return true;
             }
             if(n->left && (n->right == NULL)){
                     n->parent->left = n->left;
                     n->left->parent = n->parent;
                     n->left =NULL;
                     delete n;
                    return true;
                             }
            if(n->right && (n->left == NULL)){
                    n->parent->right = n->right;
                     n->right->parent = n->parent;
                     n->right =NULL;
                     delete n;
                    return true;
                    }
             else{
                    Node* n2 = getSuccessorNode(n->info);
                    int temp = n2->info;
                    remove(n2->info);
                    n->info = temp;
                     delete n2;
                    return true;
            }
    }

0 个答案:

没有答案