简体   繁体   中英

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.

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.

{
    // 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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