Find a binary search tree insertion and deletion

Binary search tree (Binary Search Tree) , (Also: a binary search tree, binary sort tree) which is either an empty tree or a binary tree with the following properties:
1. If it's left subtree is not empty, the value of the left sub-tree, all the nodes are less than the value of the root;
2. If its right subtree is not empty, then the right sub-tree nodes are all values greater than the value of its root ;
3. its left and right sub-trees are binary sort tree.

struct BST_Node
{
    int val;
    BST_Node *left;
    BST_Node *right;
    BST_Node(int x) : val(x), left(nullptr), right(nullptr){}
};

Operating a binary search tree `
Find 1. binary search tree
if the root node is not empty:
If you find the key root key == Returns true
if the root key> Find key looks in its left subtree
if the root key < Find key looks in its right subtree
false otherwise

BST_Node* searchBST(BST_Node *root, int val)
{
    while (root != nullptr)
    {
        if (root->val < val)
        {
            root = root->right;
        }
        else if (root->val > val)
        {
            root = root->left;
        }
        else
        {
            break;
        }
    }
    return root;
}

2. binary search tree insertion
if the tree is empty, and then directly into the return true
if the tree is not empty, according to the nature of binary search tree to find the insertion position, insert a new node

BST_Node* insertBST(BST_Node *root, int val) {
    if (root == nullptr)
    {
        root = new BST_Node(val);
        return root;
    }
    if (root->val < val)
    {
        return insertBST(root->right, val);
    }
    else if (root->val > val)
    {
        return insertBST(root->left, val);
    }
    return nullptr;
}

3. Delete binary search tree
looks first element is in the binary search tree, and if not, return, or to delete nodes may divide the following four cases:

  1. To delete a node without child node
  2. To delete a node only left child node
  3. To delete a node only right child node
  4. To delete a node has left and right child nodes
    seem to be deleted node has four cases, the actual situation of the case 1 can be combined with 2 or 3, so the real removal process is as follows:
    Case 2: Remove the node and the be deleted node parent node points to left child node is deleted nodes
    case 3: Remove the node and so was deleted node parent node points to be deleted node and right child nodes
    Scenario 4: Right child in it Looking first tree node (minimum key) in the sequence, to fill the deleted node with its value, processing again issues the delete node
void deleteBST_Node(BST_Node *root, int val)
{
    if (root == nullptr)
    {
        return ;
    }
    BST_Node *parent = nullptr, *cur = root;
    while (cur != nullptr)
    {
        if (cur->val < val)
        {
            parent = cur;
            cur = cur->right;
        }
        else if (cur->val > val)
        {
            parent = cur;
            cur = cur->left;
        }
        else { //找到待删除结点
            BST_Node *del = cur;
            if (cur->left == nullptr && cur->right == nullptr) //左右子树都为空
            {
                if (parent->left == cur)
                    parent->left = nullptr;
                else if (parent->right == cur)
                    parent->right = nullptr
                delete cur;
            }
            else if (cur->left != nullptr && cur->right != nullptr) //左右子树都不为空
            {
                del = cur->right;
                while (del->left != nullptr)
                {
                    parent = del;
                    del = del->left;
                }
                cur->val = del->val;
                if (del == cur->right)
                {
                    parent->right = del->right;
                }
                else
                {
                    parent->left = del->right;
                }
                delete del;
            }
            else //左右子树其中一个为空
            {
                if (cur->left != nullptr)
                {
                    if (parent->left == cur)
                    {
                        parent->left = cur->left;
                    }
                    else
                    {
                        parent->right = cur->left;
                    }
                }
                else
                {
                    if (parent->left == cur)
                    {
                        parent->left = cur->right;
                    }
                    else
                    {
                        parent->right = cur->right;
                    }
                }
                delete cur;
            }
            break;
        }
    }
}
Published 10 original articles · won praise 16 · views 325

Guess you like

Origin blog.csdn.net/qq_45386840/article/details/104894071