Professional Documents
Culture Documents
BST Tree Implementation
BST Tree Implementation
BST Tree Implementation
//#include<moti.sophia>
#include<iostream>
using namespace std;
struct node
{
int data;
node* left;
node* right;
};
class tree
{
private:
node* temp;
public:
node* newNode(int d)
{
temp=new node;
temp->data=d;
temp->left=NULL;
temp->right=NULL;
return temp;
}
return current;
}
else
{
//case 1: no child
if(cur->left == NULL && cur->right == NULL)
{
delete cur;
cur=NULL;
}
//case 2: 1 child
else if(cur->left == NULL)
{
temp=cur;
cur=cur->right;
delete temp;
}
else if(cur->right == NULL)
{
temp=cur;
cur=cur->left;
delete temp;
}
//case 3: 2 or more child
else
{
temp=minValue(cur->right);
cur->data=temp->data;
cur->right=Delete(cur->right,temp->data);
}
return cur;
}
}
//traversal
obj.inorder(root);
cout<<endl;
obj.Delete(root,8);
//search
int n;
cin>>n;
temp=obj.search(root,n);
if(temp!=NULL)
cout<<"Value found!";
else
cout<<"Value not found!";