Professional Documents
Culture Documents
class bst
{
int data;
bst *left, *right, *root;
public:
bst* create(int);
int search(bst*, int);
bst* insert(bst*, bst*);
void inorder(bst*);
bst*left_Node(bst*);
bst* delete_Node(bst*, int);
};
void bst::inorder(bst*root)
{
if(root == NULL)
return;
inorder(root->left);
cout<<root->data<<"\t";
inorder(root->right);
}
return current;
}
curr=root;
while(k!=curr->data)
{
if(k<curr->data)
{
parent =curr;
curr=curr->left;
}
else
{
parent=curr;
curr=curr->right;
}
}
temp->data=curr->data;
if(parent != temp)
parent->left=NULL;
else
temp->right = curr -> right;
delete curr;
root = temp;
}
return root;
}
int main()
{
bst t;
int ch, key, valid;
bst *root = NULL;
bst *parent;
do
{
cout<<"\nEnter Your Choice:\n";
cout<<"\n1] Insert Element";
cout<<"\n2] Search";
cout<<"\n3] Inorder Traversal";
cout<<"\n4] Delete a node";
cout<<"\n0] ....Exit....\n";
cin>>ch;
switch(ch)
{
case 1:
root = t.insert(root, parent);
break;
case 2:
cout<<"\nEnter the element to be searched : ";
cin>>key;
valid = t.search(root, key);
if(valid == 0)
cout<<"\nElement not found\n";
else
cout<<"\nElement found\n";
break;
case 3:
if(root == NULL)
cout<<"\n.....EMPTY.....\n";
else
t.inorder(root);
break;
case 4:
cout<<"\nEnter the element to be deleted : ";
cin>>key;
valid = t.search(root, key);
if(valid == 0)
cout<<"\nElement is not present : ";
else
{
root = t.delete_Node(root, key);
if(root == NULL)
cout<<"\n.....TREE BECAME EMPTY.....\n";
else
t.inorder(root);
}
break;
case 0:
cout<<"\n.....THE END.....\n";
exit(0);
default :
cout<<"\n....INVALID CHOICE....\n";
}
}while(ch!=0);
}