deletion in a binary search tree

//insertion;deletion;searching;dispaly;menu driven program ;(BINARY SEARCH TREE)
#include <iostream>

using namespace std;
class node
{
public:
    int data;
    node*right;
    node*left;
};
node*getnewnode(int val)
{
    node *temp=new node;
    temp->data=val;
    temp->left=NULL;
    temp->right=NULL;
   return temp;
}
int getrightmin(node*root)
{
    node*temp=new node;
    temp=root;
    while(temp->left!=NULL)
    {
        temp=temp->left;
    }
    return temp->data;
}
node*insertbst(node*root,int val)
{
    if(root==NULL)
    {
        return getnewnode(val);
    }
    if(root->data>val)
    {
        root->left= insertbst(root->left,val);
    }
    else
    {
        root->right= insertbst(root->right,val);
    }
    return root;
}
int searchbst(node*root,int val)
{
    if(root==NULL)
    {
        return 0;
    }
    if(root->data==val)
    {
        return 1;
    }
    if(root->data<val)
    {
        return searchbst(root->right,val);
    }
    else
    {
        return searchbst(root->left,val);
    }
}
node*removebst(node*root,int val)
{
    if(root==NULL)
    {
        return 0;
    }
    if(root->data>val)
    {
        root->left=removebst(root->left,val);
    }
    else if(root->data<val)
    {
        root->right=removebst(root->right,val);
    }
    else
    {
        if(root->left==NULL&&root->right==NULL)
        {
            delete root;
            return NULL;
        }
        else if(root->left==NULL)
        {
            node*temp=new node;
            temp=root->right;
            delete root;
            return temp;
        }
        else if(root->right==NULL)
        {
            node*temp=new node;
            temp=root->left;
            delete root;
            return temp;
        }
        else
        {
            int min=getrightmin(root->right);
            root->data=min;
            root->right=removebst(root->right,min);
        }
    }
    return root;
}
void inorder(node*root)
{
    if(root==NULL)
    {
        return;
    }
    inorder(root->left);
    cout<<root->data<<" ";
    inorder(root->right);
}
int main()
{
    node*root=new node;
    root=NULL;
    while(1)
    {
        int value;
        cout<<"1.Insert to bst"<<endl<<"2.search in bst:"<<endl<<"3.display ordered bst"<<endl<<"4. exit"<<endl<<"5. delete "<<endl;
        int n;
        cout<<"enter your choice:"<<endl;
        cin>>n;
        switch(n)
        {
        case 1:
            {
                cout<<"enter the value to be inserted:"<<endl;
                cin>>value;
                root=insertbst(root,value);
                break;
            }
        case 2:
            {
                cout<<"enter the value you want to search:"<<endl;
                int search;
                cin>>search;
                int s=searchbst(root,search);
                if(s==1)
                {
                    cout<<"value found"<<endl;
                }
                else
                {
                    cout<<"value not found:"<<endl;
                }
                break;
            }
        case 3:
            {
                inorder(root);
                cout<<endl;
                break;
            }
        case 4:
            {
                exit(0);
                break;
            }
        case 5:
            {
                int val;
                cout<<"enter the value to be deleted:"<<endl;
                cin>>val;
                removebst(root,val);
                break;

            }
        default:
            {
                cout<<"invalid choice given:"<<endl;
                break;
            }

        }
    }
    return 0;
}

0
8
Krythic 105 points

                                    /* This is just the deletion function you need to write the required code.
	Thank you. */

void deleteNode(Node *root, int data)
{
    if(root == NULL)
    {
        cout &lt;&lt; &quot;Tree is empty\n&quot;;
        return;
    }

    queue&lt;Node*&gt; q;
    q.push(root);

    while(!q.empty())
    {
        Node *temp = q.front();
        q.pop();

        if(temp-&gt;data == data)
        {
            Node *current = root;
            Node *prev;

            while(current-&gt;right != NULL)
            {
                prev = current;
                current = current-&gt;right;
            }

            temp-&gt;data = current-&gt;data;
            prev-&gt;right = NULL;
            free(current);

            cout &lt;&lt; &quot;Deleted\n&quot;;

            return;
        }

        if(temp-&gt;left != NULL)
            q.push(temp-&gt;left);
        if(temp-&gt;right != NULL)
            q.push(temp-&gt;right);
    }

    cout &lt;&lt; &quot;Node not found for deletion\n&quot;;
}

0
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
deletion from a binary search tree deletion from binary tree Binary Tree how to delete Delete a node of binary search tree how to delete some thing from binary tree deletion of a node in binary tree how to delete a node in binary search tree how to delete tree in binary tree c binary tree deletion in c binary search tree deletion example binary search tree del\ remove in binary search tree deleting a node in a binary tree deletion of node in binary search tree code delete a node from a binary search tree remove binary search tree algorithm binary tree delete method deletion from a binary tree binary tree deletion example how to delete in BSD binary tree deletion in m way search tree deletion in binart tree deleting a node in binary tree program to remove element in a binary search tree deletion in bst algorithm deletion of binary trees program for deleting the entire binary tree remove element from binary search tree deleting in a binary search tree Binary serahc tree deletion deleting in a binary tree DELETE data binary tree deleting in binary serach tree remove binary search tree binary search tree delete algorithm and code removing from binary search tree delete node from binary search tree algorithm delete a node binary search tree explain how to delete element in binary search tree deleting a binary tree binary tree delete operation delete in binary search tree\ bst tree deletion example how to delete from a binary search tree how to delete from binary search tree binary tree deletiing a node binary tree node deletion deletion algorithm in binary search tree how remove on binary tree work tree deletion bst how to delete item from binary tree delete function in binary search tree delete in a binary tree bst delete tree how to delete from binary tree how to delete from a binary tree deletion in tree removing from a binary tree how to delete in a binary search tree binary tree deletion in c++ deletion i binary tree deletion operation in binary search tree deletion in bst c++ code why is deleting in a bst sqrt n bst delete root how to delete internal node in binary search tree delete tree geeksforgeeks remove in binary tree deletion of a root of a binary tree if the root is delete in binary search tree trimming a binary search tree through pointers delete root binary tree how to delete root binary tree how to delete a node from a bst bst delet node binary tree delition binary search tree deletion in c++ deletion in binary tree Deletion in a Binary Tree leetcode . Deletion in a Binary Tree deletion in binary search tree using recursion delete bst draw binary tree after deleting root node delete binary tree node algorithm to delete node from bst delet number from a tree deleting nodes in binary search tree root deleting tree deletion in binary tree without using queue delete from binary tree deletion of node from bst deletion in binary search tree in data structure remove node in bst iteratively using javascript delete node form bst delete node form bs deletion of a node from a binary tree bst delete node js In a binary search tree when a node with 2 children is deleted then its position is taken by which node? bst delete recursive is deletion node asked in trees theory deleting a node in bst binary search tree deleting a node Given the following binary search tree, after deleting the node with the value 40. the node with value ___ will be promoted to the right child of the root with value 15 best search tree algorithm for deletion binarysearch tree delete In Delete operation of Binary Search Tree, If a node is a leaf node then that will be directly remove and If a node have a single successor then replace that node with that successor. Select one: True False steps in deleting a node in bst remove node in place bst delete bst algo bst deletion algorithm bst deletion case &quot;how do you delete a node that has two children&quot; how to delete element from tree during traversal cpp how to delete element from tree during traversal delete key in binary tree binary tree insertion and deletion c++ delete binary search tree Write a program to construct a Binary Search tree and delete a given value algorithm for deletion in binary search tree how to remove a node from a tree in c Algorithm for deleting a node(having two children) from Binary Search Tree (BST). in c++ deletion operation on binary search tree delete bst tree c++ Implement a C program to construct a Binary Search Tree, to delete an element in BST and to display the elements in the tree using Inorder traversal. Implement a C program to construct a Binary Search Tree, to delete an element in BST and to display the elements in the tree using Inorder traversals. deletion of binary search tree java delete bst how to delete any node from bst deleting a node from a binary tree delete bst node struct soft delete in bst how to delete a node in a tree data structure how to delete a node with two child nodes from a binary search tree c delete head node binary search tree function to delete head node in BST Illustrate with an example deletion operations in a binary search tree and write a C function to delete a node with two child. n deleting a node from a binary search tree delete node of binary tree identify the new root after deleting of 8 delet node from bst deleting from a linear looking binary search tree removing node in binary search tree binary tree remove value remove subtree from binary search tree c++ binary search tree remove If the node to be deleted has two children, please find the inorder successor of the node. Copy contents of the inorder successor to the node and delete the inorder successor delete operation in binary search tree using linked list which of the following deletions in a binary tree is most difficult BSt deletion in c draw a BSt with numbers after deleting 7 draw a BSt with numbers after deleting remove min of a bst runtime java bst heap deletion If we delete 20 from the above (2,4) tree, which node would be the final new root of the resultant tree binary search deletion If a node having two children is to be deleted from binary search tree, it is replaced by its [1,BTL2,CO3,PO1,PO2] Select one: a. In-order predecessor b. Pre-order predecessor c. In-order successor d. None Delete root node from the Binary search tree created from the following data set {50, 60, 120, 40, 55, 130, 125, 140, 127, 100} delete node binary search tree java deletion in binary search tree examples tree sort implementation delete max how to delete in bst binary tree traversal delete in data structure tree remove operation binary search tree internal node recursion remove deletion in binary searrch tree program to delete node from tree delete element from binary search tree delete all nodes less than a given number binary search tree c++ binary search tree delere how many scenarios for deleting from a binary tree delete from a bst How many different possible scenarios do we need to consider for deleting keys from a binary search tree delete below average nodes binary search tree delete multiple nodes binary search tree removal from binary search tree deletion rule binary search tree what happens when you delete in a binary search tree delete in binary search tree delete maximum node iteratively from binary tree delete largest node iteratively from binary tree delete max node in binary search tree loop remove a node from a binary search tree Delete fuction in binary search tree in c++ remove item from binary search tree TreeNode* del = h-&gt;left; delete del; delete function for binary search tree algorithm for deletion in tree binary search tree remove delete node bst return void pointer delete node in binary search tree code removing node from binarysearch tree delete node in binary tree replaced by bst tree deletion delete from a binary search tree deletion binary search tree remove node binary search tree remove a node binary search tree how to delete a node iwth affecting binary tree order delete a nide in bst bst to search and delete an element Write an algorithm to create a binary search tree. Also write an algorithm to delete a node from BST. delete a node from binary tree delete node in tree binary search tree remove node iterative delete binary search tree binary search tree delete node how to delete a elemnt from binary trees binary search tree remove algorithm how to delete elemnt in bst deletion in bst tree binary tree remove how to delete node in bst how to delete element from a tree concept how to delete element from a tree how to delete root node in bst remove node in binarysearch tree Data Structures: Delete a Node with One Child in a Binary Search Tree deleting node from tree delete from bst in c iterative remove head of binary search tree python BST delete operation binary search remove c delete bst remove element in binary search tree binary tree delete how to delete a node from a tree delete Node binary search tree deleteNode binary search tree deleteNode binary tree delete in bst with parent pointer how to delete node from binary search tree in c++ delete in bst gfg delete node with parent pointer in binary search tree delete node with parent pointer delete element from binary tree c progam to delete a binary search tree and print the order in which nodes are deleted c program to Delete a BST. Print the order in which nodes are deleted. deletion of element in binary search tree how to add delete and update nodes in tree structure write a function to delete a given item from bst in c++ Write a program a function to delete a given item from BST inm c++ deleting from a binary search tree In the algorithm for removing a node from a binary search tree, if the node being removed has two children, the algorithm: kperterson delete node from bst c++ how delete in BST gfg binary tree deletion how to remove node from binary search tree binary tree deletion code bst delete algorithm remove a node from a binary search tree c Write a function removeBSTNode() that removes a given item from a Binary Search Tree. The function should return 0 if the item was found and successfully removed and 1 otherwise. binary tree delet node binary tree delete node delete value from binary search tree draw the tree after deletion remove in a binary tree how to delete the node in tree how to delete tnode and subtrees Deleting the root in this tree: remove from binary tree node deletion in binary search tree deleting node from binary search tree how to remove element from binary search tree deleting root node from binary search tree deleting from binary search tree delete a tree c++ BST remove algorithm delete element from bst delete a element node in binary search tree in c deletion of node in binary tree by given element how to delete a given node in binary tree by element how to delete a given node in binary tree how to delete a given node in bst binary tree delete node in c tree deletion deletion from binary search tree delete element in binary tree bst delete node in c bst deletion with parent pointer What three cases do we have when trying to remove a node from a binary tree? algorithm to delete a bst binary search tree delete node c++ delete node with two child delete node in bst iterative in c deleting a node from a BST in c deleting a node from a BST remove from a binary tree remove balanced binary tree balanced binary tree remove c++ delete node binary search tree if the root node of the bst is deleted, what becomes the root node remove element from binary tree java delete binary search tree node delete a node in binary search tree in c delete node in binary tree c++ delete node of bst delete bst node delete node from tree c delete node from tree c++ deletion in a simple binary tree how to write a delete function for binary search binary tree delete node c++ how to delete a node from a binary tree how to delete a node from a tree c++ write a program to delete a node from a binary tree. python delete pre binary tree deletion in binary search tree removing element from tree c++ deleting element from binary search tree how to remove element from a binary gtere delete binary tree java remove bst function c++ time complexity remove bst function c++ bst remove function c++ removing an element from a tree java removing an element from a tree jaca removing an element from a tree deleting nodes in bst binary search tree deletion code binary search tree remove c++ delete binary subtree c++ delete binary sub tree c++ deletion of node in bst bst delete c++ delete node is not deleting in BST delete a node in binary search tree Deleting a node from a BST and balance it. deleting an element for BST bst delete cpp remove value in binary search tree cpp deleting tree nodes removing BST cpp delete value in binary search tree cpp What node will replace when node 5 is deleted? * 2 points Captionless Image binary search tree removal binary tree node deletion in c++ deletion in binary tree binary tree c delete node remove from binary search tree deletion of node in binary tree using recursion bst deletion code binary search tree remove from the tree case 0 c++ deletion in a binary tree how to remove root from binary search tree alorithm to delete elemnt from binary tree algorithm to delete element from tree and rearrange the tree algorithm to connect the tree when node is deleted For deleting a node from Binary Search Tree containing two children can use which of the following node for replacement? a. Anyone (Inorder Predecessor &amp; Successor) b. Inorder Successor c. Inorder Predecessor d. Both (Inorder Predecessor &amp; Successor) delete n number of nodes from bst c++ delete a noes from bst c++ delete function for bst c++ delete from binary search tree iterative deletion of node in tree When a node with only one child is deleted, what replaces the pointer to the deleted node? Question 10 options: The nodes left sub-tree The nodes right sub-tree The child NULL deleting node from bst delete in a binary search tree delete from binary search tree deleting a node from binary search tree delete node from binary search tree deleting a node from a binary search tree tree is not deleting in java delete operation BST deleting a node from bst deleting a node in binary search tree if you delete the root of the binary tree what will be the next if you delete the head root binary tree if you delete a root binary tree geeks for geeks searching node in binary search tree deletion deletion of node in tree Delete Node in a BST node deletion in binary tree delete node from binary tree remove a node from a binary tree delete operation in binary search tree dlete node in bst how to delete a node from a binary search tree how to delete an element from bst delete a node from binary search tree delelition bst node delete node in BSTR delete an node from bst delete binary search tree For the following binary search tree, remove the node with the value 10 and show the resulting BST. removing a node from a binary search tree how do deletes work in binary tree can you delete the root of a binary search tree delete binary tree delete a node in binary tree delete binary tree tutorial remove function for a node in datastructure bst node deletion delete root node from bst C programming delete root node from bst C delet root node from bst how to delete a element in a tree delete from bst delete a binary tree algorithm delete a node operation in binary tree java delete operation in inary tree java when you delete a node in a binary tree, does its parent child node become null deletion in bst in c++ delete the root node of a binary search tree remove bst node deletion of node in binary search tree binary search tree delete binary tree deletion in data structure in java delete node from binary tree in java delete deepest node in binary tree how to delete element from bst binary tree deletion of a node deleting a node in a binary search tree how to remove node from a tree delete node bst deletion in a binary tree in java how to delete the node from the tree bst delete node remove a node from bst remove a node in bst delete value in binary search tree delete function binary search tree c++ c++ binary search tree delete with 3 cases c++ binary search tree delete delete node in binary search tree remove node in binary tree bst delete tree add and remove c++ delete node binary search tree recursive c++ delete node in a binary tree deletion of node in binary tree java deletion of node in binary tree how to delete a node in binary search tree c++ delete node in bst DELETONIG A NODE IN TREE Extended binary tree delete node delete in BST delete in SBST deleting a node in bst deletion in a binary search tree bst deletion binary search tree deletion algorithm delete node from bst delete a node in bst delete the node from BST how to delete a node in bst BST delete noew delete node in binary tree deleting node in binary tree deleting node in binary search tree delete in binary tree binary tree removing a node with 1 chiuld node c# binary tree remove node if binary tree remove node binary tree removenode binary tree elimination node binary tree elimination remove binary tree annotation remove binary tree To delete a node having two children from a binary search tree (BST) question To delete a node having two children from a binary search tree (BST), we replace the node with algorithm to deleting a tree binary tree how to remove a node binary tree dele how to delete a node in binary tree in java binary search tree deletion delete a node from bst deletion of a node in binary search tree binary tree deletion
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source