以非递归方式在BST中删除节点

时间:2016-02-21 18:15:15

标签: c++ binary-search-tree

以下是我从BST删除节点的一​​段代码。它是一个非递归代码。 我已经应用了所有可能的条件。 然而,当我运行我的代码时,它会停止,好像卡在某个无限循环中的某个地方,或者在我的指针指向NULL的位置结束。但是我无法识别它。

任何帮助将不胜感激。

template <class T>
void bst<T>::delete_node(T key1)
bst_node<T>* delNode = search(key1); //delNode is the node I wish to delete

if(delNode!=root)
{
    if((delNode->left == NULL) && (delNode->right == NULL)) // node to be deleted has no children
    {
        delNode = NULL;
    }

else if((delNode->left!=NULL) && (delNode->right == NULL)) //node to be deleted has exactly one child
    {
        if(delNode->parent->left == delNode)
        {
            delNode->parent->left = delNode->left;
            delNode = NULL;
        }
        else if(delNode->parent->right == delNode)
        {
            delNode->parent->right = delNode->left;
            delNode = NULL;
        }
    }
    else if((delNode->right!= NULL) && (delNode->left == NULL))
    {
        if(delNode->parent->left == delNode)
        {
            delNode->parent->left = delNode->right;
            delNode = NULL;
        }
        else if(delNode->parent->right == delNode)
        {
            delNode->parent->right = delNode->right;
            delNode = NULL;
        }
    }
    else if((delNode->right!=NULL)&&(delNode->left != NULL)) //if has two children
    {
        bst_node<T>* temp = delNode;
        bst_node<T>* trav = delNode->right;
        if((trav->right == NULL)&&(trav->left == NULL))
        {
            delNode->value = trav->value;
            delNode->key = trav->key;
            trav = NULL;
        }
        else
        {
            bst_node<T>* pred = trav;
            while(trav!=NULL)
            {
                pred = trav; //smallest node in right subtree
                trav=trav->left;
            }
            delNode->value = pred->value;
            delNode->key = pred->key;
            pred = NULL;
        }

    }
}
    else if(delNode==root)//node to be deleted is Root
    {
        if((delNode->left==NULL)&&(delNode->right==NULL))
            root = NULL;
        else if((delNode->left!=NULL) && (delNode->right == NULL)) //root has exactly one child
        {
            root = delNode->left;
            delNode = NULL;

        }
        else if((delNode->right!= NULL) && (delNode->left == NULL))
        {
            root = delNode->right;
            delNode = NULL;
        }
        else if((delNode->right!=NULL)&&(delNode->left!=NULL)) {
        bst_node<T>* temp = delNode;
        bst_node<T>* trav = delNode->right;
        if((trav->right == NULL)&&(trav->left == NULL))
        {
            delNode->value = trav->value;
            delNode->key = trav->key;
            trav = NULL;
        }
        else
        {
            bst_node<T>* pred = trav;
            while(trav!=NULL)
            {
                pred = trav; //smallest node in right subtree
                trav=trav->left;
            }
            delNode->value = pred->value;
            delNode->key = pred->key;
            pred = NULL;
        }

    }
 }

}

1 个答案:

答案 0 :(得分:0)

在部分中:

else if((delNode->right!=NULL)&&(delNode->left != NULL)) //if has two children

何时采取:

    if((trav->right == NULL)&&(trav->left == NULL))

代码会将值从trav复制到delNode并将trav设置为NULL:

delNode->value = trav->value;
delNode->key = trav->key;
trav = NULL; 

但未将delNode->right设置为NULL,因为它应该是因为trav没有任何子女。

寻找最左边节点的下一个else语句会提交类似的错误,因为父节点的成员left未设置为NULL。

由于明显的复制粘贴(咳嗽!)代码,处理仅删除root节点的代码的下一个重要部分似乎有相同的问题。