Delete Node in BST

 #include <iostream>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int xTreeNode *leftTreeNode *right) : 
                        val(x), left(left), right(right) {}
};
TreeNode *insert(int dTreeNode *root)
{
    if (root == NULL)
        return new TreeNode(d);
    else if (root->val == d)
        return root;
    else if (root->val < d)
        root->right = insert(droot->right);
    else if (root->val > d)
        root->left = insert(droot->left);
    return root;
}
TreeNode *BuildTree()
{
    TreeNode *root = NULL;
    int d;
    cin >> d;
    if (d == -1)
        return NULL;
    while (d != -1)
    {
        root = insert(droot);
        cout << "Enter another Value to be entered : ";
        cin >> d;
    }
    return root;
}
void print(TreeNode *root)
{
    if (root == NULL)
        return;
    print(root->left);
    cout << root->val << " ";
    print(root->right);
}
int max_val(TreeNode *root)
{
    if (root->right == NULL)
        return root->val;
    return max_val(root->right);
}
TreeNode *deleteNode(TreeNode *rootint key)
{
    if (root == NULL)
        return NULL;
    if (root->val < key)
        root->right = deleteNode(root->rightkey);
    else if (root->val > key)
        root->left = deleteNode(root->leftkey);
    else
    {
        //node with key value have no childs.
        if (root->left == nullptr && root->right == nullptr)
            return NULL;

        else if (root->right != nullptr && root->left != nullptr)
        {
            //node with key value having both left and right child.
            //node having both childs will be replaced by the the max value 
            //of its left sub tree,
            //bcz, in the left subtree max value will have no right child.
            //and it will be easy to remove the nodes with zero or 1 child.
            int mx = max_val(root->left);
            root->val = mx;
            cout << mx;
            root->left = deleteNode(root->leftmx);
            return root;
        }
        else
        {
            //node with key value having either left or right child only.
            if (root->right != nullptr)
                return root->right;
            else if (root->left != nullptr)
                return root->left;
        }
    }
    return root;
}
int main()
{
    TreeNode *root = BuildTree();
    int x;
    cout << "The inorder of Tree is : ";
    print(root);
    cout << "\nEnter the value to be deleted : ";
    cin >> x;
    deleteNode(rootx);
    cout << "\nThe inorder of tree is : ";
    print(root);
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree