简体   繁体   English

二叉搜索树查找和删除 [C++]

[英]Binary Search Tree Find And Remove [C++]

At the moment, I am currently attempting to fix the Find and Remove function which I am doing with recursion.目前,我目前正在尝试修复我正在使用递归执行的查找和删除功能。 However, I am running with the issue of when it gets to the end of case two, how to make identify if it leads to case zero or case one.但是,我遇到的问题是何时到达案例二的结尾,如何确定它是导致案例零还是案例一。

Here's a short description of what I am attempting to do: Case two (two children) - swap with the minimum of the right sub tree, which leads to a case zero or case one.这是我尝试做的事情的简短描述:案例二(两个孩子) - 与右子树的最小值交换,这导致案例零或案例一。

    bool findAndRemove(const Type& v)
{
    return findAndRemove(root, nullptr, v);
}

bool findAndRemove(Node<Type> *fr, Node<Type> *parent, const Type& v) const
{
    if (fr == nullptr)
    {
        return true;
    }

    if (v < fr->element)
    {
        return findAndRemove(fr->left, fr, v);
    }
    else if (v > fr->element)
    {
        return findAndRemove(fr->right, fr, v);
    }
    else
    {
        switch (GetChildren(fr))
        {
        case 0:
            if (parent->left == fr)
            {
                parent->left = nullptr;
                delete fr;
            }
            else
            {
                parent->right = nullptr;
                delete fr;
            }
            break;
        case 1:
            if (parent->left == fr)
            {
                if (fr->left != nullptr)
                {
                    parent->left = fr->left;
                    delete fr;
                }
                else
                {
                    parent->left = fr->right;
                }

            }
            else
            {
                if (fr->right != nullptr)
                {
                    parent->right = fr->right;
                    delete fr;
                }
                else
                {
                    parent->right = fr->left;
                }
            }
            break;
        case 2:
        {
            Node<Type> * swap = fr->right;
            while (swap->left != nullptr)
            {
                swap = swap->left;
            }

            Type temp = fr->element; // 30
            temp = swap->element; // 35
            swap->element = fr->element; // 30
            fr->element = temp;

            //temp = swap->element;
            //swap->element = temp;
            //temp = fr->element;
            break;
        }
        }
    }
    return false;
}

According to the algorithm :根据算法

  • find a minimum value in the right subtree;在右子树中找到最小值;
  • replace value of the node to be removed with found minimum.用找到的最小值替换要删除的节点的值。 Now, right subtree contains a duplicate!现在,右子树包含一个副本!
  • apply remove to the right subtree to remove a duplicate.将 remove 应用于右子树以删除重复项。

In your code swap points to the minimum node in that right subtree, so if you call your findAndRemove function on this node, you can simply remove it.在您的代码中, swap指向该右子树中的最小节点,因此如果您在该节点上调用findAndRemove函数,则可以简单地将其删除。

{
    // STEP 1: find a minimum value in the right subtree;
    Node<Type> * swap = fr->right;
    while (swap->left != nullptr)
    {
        swap = swap->left;
    }

    // STEP 2: replace value of the node to be removed with found minimum.
    fr->element = swap->element;

    // STEP 3: apply remove to the right subtree to remove a duplicate.
    // Here you should call 'findAndRemove' on 'swap' node
    break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM