"deletion in a binary search tree" Code Answer's

You're definitely familiar with the best coding language C++ that developers use to develop their projects and they get all their queries like "deletion in a binary search tree" answered properly. Developers are finding an appropriate answer about deletion in a binary search tree related to the C++ coding language. By visiting this online portal developers get answers concerning C++ codes question like deletion in a binary search tree. Enter your desired code related query in the search bar and get every piece of information about C++ code related question on deletion in a binary search tree. 

binary tree deletion

By Rid09Rid09 on Jun 07, 2020
/* 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 << "Tree is empty\n";
        return;
    }

    queue<Node*> q;
    q.push(root);

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

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

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

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

            cout << "Deleted\n";

            return;
        }

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

    cout << "Node not found for deletion\n";
}

Add Comment

5

deletion in a binary search tree

By ujjwal sotraujjwal sotra on May 21, 2021
//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;
}

Add Comment

0

All those coders who are working on the C++ based application and are stuck on deletion in a binary search tree can get a collection of related answers to their query. Programmers need to enter their query on deletion in a binary search tree related to C++ code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about deletion in a binary search tree for the programmers working on C++ code while coding their module. Coders are also allowed to rectify already present answers of deletion in a binary search tree while working on the C++ language code. Developers can add up suggestions if they deem fit any other answer relating to "deletion in a binary search tree". Visit this developer's friendly online web community, CodeProZone, and get your queries like deletion in a binary search tree resolved professionally and stay updated to the latest C++ updates. 

C++ answers related to "deletion in a binary search tree"

View All C++ queries

C++ queries related to "deletion in a binary search tree"

deletion in a binary search tree binary tree deletion binary search tree in cpp using class binary tree search binary search tree sorted order hashtable linear probing ,insertion deletion searching binary index tree c++ binary indexed tree top view of binary tree c++ heap sort heapify and max heap in binary tree searching display insert in a binary serach tree Write a program in C++ to find post-order predecessor of a node in a Binary Tree vertical traversal of binary tree Print Nodes in Top View of Binary Tree how to do binary search in c++ using STL binary search program c++ binary search stl binary search function in c++ binary search in c++ c++ binary search binary search algorithm c++ binary search lower bound C Binary Search binary search in java Binary Search implementation binary search in c binary search in stl gfg bottom view of tree gfg right view of tree bst to insert tree gfg left view of tree tree in c++ stl gfg top view of tree dfenwick tree code c++ avl tree implementation c++ find the graph is minimal spanig tree or not diameter of tree using dfs centroid of a tree convert binary to decimal c++ stl binary exponentiation binary sort c++ binary addition using bitwise operators convert decimal to binary in c++ convert int to binary string c++ binary exponentiation modulo m c++ display numbers as binary built in function in c++ for binary to decimal write and read string binary file c++ convert long int to binary string c++ print binary in c how to do decimal to binary converdsion in c++ Decimal to binary c++ binary heap decimal to binary predefined function find number of 1s in a binary cv::mat image c++ vector decimal to binary Print Decimal to binary using stack is obje file binary?? how to find the left most bit 1 in binary of any number how to show c++ binary files in sublime text binary algebra cpp building native binary with il2cpp unity search in vector of pairs c++ array search c++ ternary search c++ delete and search edge in adjacency matrix of a graph how to search integer in c++ c++ vector quick search dichotomic search c++ linear search in c bst search linear search

Browse Other Code Languages

CodeProZone