#Include Using Namespace Struct Int
#Include Using Namespace Struct Int
#include<iostream>
using namespace std;
struct Node
{
int data;
Node*llink;
Node*rlink;
};
class BST
{
private:
Node* root;
public:
BST()
{
root=NULL;
}
void isert(int data)
{
if(root==NULL)
{
Node* newnode=new Node;
newnode->data=data;
newnode->llink=NULL;
newnode->rlink=NULL;
root=newnode;
}
else
{
Node*current=root;
Node*prev=root;
while(current!=NULL)
{
prev=current;
if(current->data==data)
{
cout<<"Number already Exist\n";
break;
}
else if(current->data>data)
{
current=current->llink;
}
else
current=current->rlink;
}
if(current==NULL)
{
Node* newnode=new Node;
newnode->data=data;
newnode->llink=NULL;
newnode->rlink=NULL;
if(prev->data>data)
prev->llink=newnode;
else
prev->rlink=newnode;
cout<<"Number Added Succesfully\n";
}
}
}
Node*rot()
{
return root;
}
void preorder(Node*N)
{
if(N!=NULL)
{
cout<<N->data<<" ";
preorder(N->llink);
preorder(N->rlink);
}
}
void inorder(Node*N)
{
if(N!=NULL)
{
inorder(N->llink);
cout<<N->data<<" ";
inorder(N->rlink);
}
}
void postorder(Node*N)
{
if(N!=NULL)
{
postorder(N->llink);
postorder(N->rlink);
cout<<N->data<<" ";
}
}
bool search(int data)
{
Node*current=root;
bool present=false;
while(current!=NULL)
{
if(current->data==data)
{
present=true;
break;
}
else if(current->data>data)
{
current=current->llink;
}
else
current=current->rlink;
}
return present;
}
void delet(int data)
{
if(root==NULL)
{
cout<<"Empty List\n";
}
else if(search(data)==false)
{
cout<<"Number do not exist\n";
}
else if(root->data==data)
{
if(root->llink==NULL&&root->rlink==NULL)
{
root=NULL;
}
else if(root->llink!=NULL&&root->rlink==NULL)
{
root=root->llink;
}
else if(root->rlink!=NULL&&root->llink==NULL)
{
root=root->rlink;
}
else if(root->rlink!=NULL&&root->llink!=NULL)
{
Node*saveparent=root;
Node*child=root->llink;
if(child->rlink==NULL)
{
root->data=child->data;
root->llink=child->llink;
}
else
{
while(child->rlink!=NULL)
{
saveparent=child;
child=child->rlink;
}
if(child->llink!=NULL)
{
root->data=child->data;
saveparent->rlink=child->llink;
}
else
{
root->data=child->data;
saveparent->rlink=NULL;
}
}
}
}
else
{
Node*current=root;
Node*prev=root;
while(current!=NULL)
{
if(current->data==data)
{
break;
}
else if(current->data>data)
{
prev=current;
current=current->llink;
}
else
{
prev=current;
current=current->rlink;
}
}
if(current->llink==NULL&¤t->rlink==NULL)
{
if(prev->data>current->data)
{
prev->llink=NULL;
}
else
prev->rlink=NULL;
}
else if(current->llink!=NULL&¤t->rlink==NULL)
{
if(prev->data>current->data)
{
prev->llink=current->llink;
}
else
prev->rlink=current->llink;
}
else if(current->rlink!=NULL&¤t->llink==NULL)
{
if(prev->data>current->data)
{
prev->llink=current->rlink;
}
else
prev->rlink=current->rlink;
}
else if(current->rlink!=NULL&¤t->llink!=NULL)
{
Node*saveparent=current;
Node*child=current->llink;
if(child->rlink==NULL)
{
current->data=child->data;
current->llink=child->llink;
}
else
{
while(child->rlink!=NULL)
{
saveparent=child;
child=child->rlink;
}
if(child->llink!=NULL)
{
current->data=child->data;
saveparent->rlink=child->llink;
}
else
{
current->data=child->data;
saveparent->rlink=NULL;
}
}
}
}
}
int noleaves(Node*current)
{
int count=0;
if(current->llink==NULL&¤t->rlink==NULL)
{
count=1;
}
else
{
if(current->llink!=NULL)
{
count=count+noleaves(current->llink);
}
if(current->rlink!=NULL)
{
count=count+noleaves(current->rlink);
}
}
return count;
}
int NoNodes(Node*current)
{
int count=1;
if(current->llink==NULL&¤t->rlink==NULL)
{
return 1;
}
else
{
if(current->llink!=NULL)
count=1*NoNodes(current->llink);
if(current->rlink!=NULL)
count=count+1*NoNodes(current->rlink);
}
return count;
}
};
int main()
{
int t=5,data;
BST list;
while(t!=8)
{
cout<<"\tMain Menu\n";
cout<<"1.Insert Number\n";
cout<<"2.Show PreOrder\n";
cout<<"3.Show InOrder\n";
cout<<"4.Show PostOrder\n";
cout<<"5.Delete\n";
cout<<"6.No of Leaves\n";
cout<<"7.No of Nodes\n";
cout<<"8.Exit\n";
cout<<"Enter Choice:";
cin>>t;
switch(t)
{
case 1:
cout<<"Enter Data:";
cin>>data;
list.isert(data);
break;
case 2:
cout<<"Pre Order List:";
list.preorder(list.rot());
cout<<endl;
cout<<endl;
break;
case 3:
cout<<"In Order List:";
list.inorder(list.rot());
cout<<endl;
cout<<endl;
break;
case 4:
cout<<"Post Order List:";
list.postorder(list.rot());
cout<<endl;
cout<<endl;
break;
case 5:
cout<<"Enter Data:";
cin>>data;
list.delet(data);
break;
case 6:
cout<<"No Of Leaves:"<<list.noleaves(list.rot())<<endl;
break;
case 7:
cout<<"No Of Nodes:"<<list.NoNodes(list.rot())<<endl;
break;
case 8:
break;
default:
cout<<"Invalid Key Enter\n";
}
system("pause");
system("cls");
}
}