二进制搜索树删除功能不起作用(作业)

时间:2017-11-01 06:11:10

标签: c++ binary-search-tree

对于我的任务,我要实现二进制搜索树类,我已经成功实现了除删除之外的所有方法。它确实工作不一致,我找不到问题。这是代码......

template <class T>
void binSTree<T>::remove(Node <T>* p, const T& v)
{
    // Find the item
    bool found = false;
    Node<T>* predecessor = nullptr;
    Node<T>* current = n;
    while (current != nullptr)
    {
        if (current->data == item)
        {
            found = true;
            break;
        }
        else
        {
            predecessor = current;
            if (item > (current->data))
                current = current->right;
            else
                current = current->left;
        }
    }
    if (!found)
    {
        return;
    }
    // CASE 1: Removing a node with a single child
    if ((current->left == nullptr && current->right != nullptr) || (current-
>left != nullptr && current->right == nullptr))
    {
        // Right Leaf Present, No Left Leaf
        if (current->left == nullptr && current->right != nullptr)
        {
            // If predecessor's left tree equals Node n
            if (predecessor->left == current)
            {
                // then predecessor's left tree becomes n's right tree
                // and delete n
                predecessor->left = current->right;
                delete current;
                current = nullptr;
            }
            // If predecessor's right tree equals Node n
            else
             {
                 // then predecessor's right tree becomes n's right tree
                way down left to locate smallest element
                    if ((current->right)->left != nullptr)
                     {
                         Node<T>* leftCurrent;
                         Node<T>* leftCurrentPred;
                        leftCurrentPred = current->right;
                        leftCurrent = (current->right)->left;
                        while (leftCurrent->left != nullptr)
                        {
                            leftCurrentPred = leftCurrent;
                            leftCurrent = leftCurrent->left;
                        }
                        current->data = leftCurrent->data;
                        delete leftCurrent;
                        leftCurrentPred->left == nullptr;
                    }
                    else
                    {
                        Node<T>* temp = current->right;
                        current->data = temp->data;
                        current->right = temp->right;
                        delete temp;
                    }
            }
            return;
     }

}

当它用于从BST中删除多个值时,这就是一个例子......

给出一个包含50个值的树......

   1   36  104  159  183  184  198  219  220  241  287  294  304  356  369  378  380  387  394  395
 418  464  491  516  521  525  582  583  623  641  648  653  667  692  702  703  724  729  743  765
 796  802  887  897  901  909  969  971  979  992

从树中删除一些值后,它应该包含这个..

   1   36  104  159  183  198  219  220  241  294  304  356  369  378  387  394  395  418  464  491
 516  525  582  583  623  641  648  653  692  702  703  724  729  765  796  802  887  897  909  969
 971  992

但这是我收到的输出......

   1   36  104  159  183  198  219  220  241  294  304  356  369  378  387  394  395  418  464  491
 516  525    0  582  583  623  641  648  653  692  702  703  724  729  765  796  802  887  897  909
 969  971  992

正如您所看到的,它包含的值多于应有的值,并且在第23次删除之前也可以完美运行,因为某些原因它会向树中添加0。在查看我的代码后,我似乎无法找到导致这种情况发生的原因,我们将非常感谢任何帮助。

0 个答案:

没有答案