节点拒绝在二进制搜索树中删除

时间:2018-05-02 16:20:26

标签: c++ data-structures binary-search-tree

所以我有一个BST的对象,但出于某种原因,每当我尝试从BST中删除一个节点时它就会拒绝。我之前使用过相同的函数,但在BST中,节点中充满了字符串和整数。在这个程序中,节点充满了对象。此外,搜索不会产生问题,因为当我调试时,我可以看到它正确地找到了我正在查找的节点,但由于某种原因,当它到达删除调用时它除了mangle之外什么也不做对象中的数据有点。

template <typename DATATYPE, typename KEYTYPE>
Node<DATATYPE, KEYTYPE> * BSTree<DATATYPE, KEYTYPE>::deleteNode(Node<DATATYPE, KEYTYPE> * aRoot,KEYTYPE value)
{
    /* Given a binary search tree and a key, this function deletes the key
     and returns the new root */

    // base case
    if (aRoot == nullptr) return aRoot;

    // If the key to be deleted is smaller than the aRoot's key,
    // then it lies in left subtree
    if (value.year < aRoot->Key().year)
        aRoot->setLeft(deleteNode(aRoot->Left(), value));

    // If the key to be deleted is greater than the root's key,
    // then it lies in right subtree
    else if (value.year > aRoot->Key().year)
        root->setRight(deleteNode(aRoot->Right(), value));

    // if key is same as root's key, then This is the node
    // to be deleted
    else
    {
        /* first recur on left child */
        deleteNode(aRoot->Left(), value);

        /* now check if data matches*/
        if((value.film == aRoot->Key().film)&&(value.name == aRoot->Key().name)&&(value.winner == aRoot->Key().winner)&&(value.award == aRoot->Key().award)&&(value.year == aRoot->Key().year))
        {
            // node with only one child or no child
            if (aRoot->Left() == nullptr)
            {
                Node<DATATYPE, KEYTYPE> *temp1 = aRoot->Right();
                //this is where it ignores the delete 
                delete aRoot;
                cout << "Success!\n";
                return temp1;
            }
            else if (aRoot->Right() == nullptr)
            {
                Node<DATATYPE, KEYTYPE> *temp2 = aRoot->Left();
                //this is where it ignores the delete 
                delete aRoot;
                cout << "Success!\n";
                return temp2;
            }

            // node with two children: Get the inorder successor (smallest
            // in the right subtree)
            Node<DATATYPE, KEYTYPE> * temp = min(aRoot->Right());

            // Copy the inorder successor's content to this node
            aRoot->setKey(temp->Key());
            aRoot->setData(temp->Data());

            // Delete the inorder successor
            aRoot->setRight(deleteNode(aRoot->Right(), temp->Key()));
            cout << "Success!\n";
        }

        /*now recur on the right child*/
        deleteNode(aRoot->Right(), value);

    }
    return aRoot;
}

template <typename DATATYPE, typename KEYTYPE> Node<DATATYPE, KEYTYPE> * BSTree<DATATYPE, KEYTYPE>::deleteNode(Node<DATATYPE, KEYTYPE> * aRoot,KEYTYPE value) { /* Given a binary search tree and a key, this function deletes the key and returns the new root */ // base case if (aRoot == nullptr) return aRoot; // If the key to be deleted is smaller than the aRoot's key, // then it lies in left subtree if (value.year < aRoot->Key().year) aRoot->setLeft(deleteNode(aRoot->Left(), value)); // If the key to be deleted is greater than the root's key, // then it lies in right subtree else if (value.year > aRoot->Key().year) root->setRight(deleteNode(aRoot->Right(), value)); // if key is same as root's key, then This is the node // to be deleted else { /* first recur on left child */ deleteNode(aRoot->Left(), value); /* now check if data matches*/ if((value.film == aRoot->Key().film)&&(value.name == aRoot->Key().name)&&(value.winner == aRoot->Key().winner)&&(value.award == aRoot->Key().award)&&(value.year == aRoot->Key().year)) { // node with only one child or no child if (aRoot->Left() == nullptr) { Node<DATATYPE, KEYTYPE> *temp1 = aRoot->Right(); //this is where it ignores the delete delete aRoot; cout << "Success!\n"; return temp1; } else if (aRoot->Right() == nullptr) { Node<DATATYPE, KEYTYPE> *temp2 = aRoot->Left(); //this is where it ignores the delete delete aRoot; cout << "Success!\n"; return temp2; } // node with two children: Get the inorder successor (smallest // in the right subtree) Node<DATATYPE, KEYTYPE> * temp = min(aRoot->Right()); // Copy the inorder successor's content to this node aRoot->setKey(temp->Key()); aRoot->setData(temp->Data()); // Delete the inorder successor aRoot->setRight(deleteNode(aRoot->Right(), temp->Key())); cout << "Success!\n"; } /*now recur on the right child*/ deleteNode(aRoot->Right(), value); } return aRoot; }

void setLeft(Node<DATATYPE, KEYTYPE> * aLeft) { left = aLeft; };
void setRight(Node<DATATYPE, KEYTYPE> * aRight) { right = aRight; };

void setLeft(Node<DATATYPE, KEYTYPE> * aLeft) { left = aLeft; }; void setRight(Node<DATATYPE, KEYTYPE> * aRight) { right = aRight; };

0 个答案:

没有答案