#include<iostream.h>
#include<conio.h>
#include<process.h>
class node
{ public:
int info;
node* left,*right;
node* next;
node()
{
left=0;
right=0;
next=0;
}
node( int a)
{
info=a;
left=0;
right=0;
next=0;
}
};
class queue
{
node* front,*rear;
public:
queue()
{
front=0;
rear=0;
}
int isempty();
void enq(node*);
node* deq();
};
int queue::isempty()
{
return (front==0);
}
void queue::enq(node* el)
{
if(!isempty())
{
rear->next=el;
rear=rear->next;
}
else
{
front=rear=el;
}
}
node* queue::deq()
{
if(isempty())
return 0;
else
{
node* el;
if(front==rear)
{
el=front;
front=rear=0;
return el;
}
else
{
el=front;
front=front->next;
return el;
}
}
}
class BST
{
node* root;
public:
BST()
{
root=0;
}
int search(int);
void insert(int);
void VLR_preorder(node*);
void LRV_postorder(node*);
void LVR_inorder(node*);
void mirror();
void level();
int nonleaf(node*);
int leaf(node*);
int height(node*);
void delbycopy(node* &);
node* retroot()
{
return root;
}
};
int BST::search(int el)
{
node* curr=root;
while(curr!=0)
{
if(curr->info==el)
return 1;
else if(curr->info<el)
curr=curr->right;
else
curr=curr->left;
}
return 0;
}
void BST::insert(int el)
{
node* curr=root;
node* p;
if(curr==0)
root=new node(el);
else
{
while(curr!=0)
{
p=curr;
if(curr->info<el)
curr=curr->right;
else
curr=curr->left;
}
if(p->info<el)
p->right=new node(el);
else
p->left=new node(el);
}
}
void BST::VLR_preorder(node* curr)
{
if(curr!=0)
{
cout<<curr->info<<" ";
VLR_preorder(curr->left);
VLR_preorder(curr->right);
}
}
void BST::LRV_postorder(node* curr)
{
if(curr!=0)
{
LRV_postorder(curr->left);
LRV_postorder(curr->right);
cout<<curr->info<<" ";
}
}
void BST::LVR_inorder(node* curr)
{
if(curr!=0)
{
LVR_inorder(curr->left);
cout<<curr->info<<" ";
LVR_inorder(curr->right);
}
}
void BST::level()
{
queue q;
node* p=root;
if(p!=0)
q.enq(p);
while(!q.isempty())
{
p=q.deq();
cout<<p->info<<" ";
if(p->left!=0)
q.enq(p->left);
if(p->right!=0)
q.enq(p->right);
}
}
void BST::mirror()
{
queue q;
node* p=root;
if(p!=0)
q.enq(p);
while(!q.isempty())
{
p=q.deq();
node* temp=p->left;
p->left=p->right;
p->right=temp;
if(p->left!=0)
q.enq(p->left);
if(p->right!=0)
q.enq(p->right);
}
}
int BST::nonleaf(node* curr)
{
int count=0;
if(curr!=0)
{
count+=nonleaf(curr->left);
if(curr->left!=0&&curr->right!=0)
count++;
count+=nonleaf(curr->right);
}
return count;
}
int BST::leaf(node* curr)
{
int count=0;
if(curr!=0)
{
count+=leaf(curr->left);
if(curr->left==0&&curr->right==0)
count++;
count+=leaf(curr->right);
}
return count;
}
int BST::height(node* curr)
{
int l=0,r=0;
if(curr->left)
l=height(curr->left);
if(curr->right)
r=height(curr->right);
if(l>r)
return (l+1);
else
return (r+1);
}
void BST::delbycopy(node* &ptr)
{
node* prev,*temp=ptr;
if(ptr->right==0)
ptr=ptr->left;
else if(ptr->left==0)
ptr=ptr->right;
else
{
temp=ptr->left;
prev=ptr;
while(temp->right!=0)
{
prev=temp;
temp=temp->right;
}
ptr->info=temp->info;
if(prev==ptr)
prev->left=temp->left;
else
prev->right=temp->left;
}
delete temp;
}
void main()
{
clrscr();
BST b;
int n,c;
do
{
clrscr();
cout<<"1.insertion"<<"\n";
cout<<"2.searching"<<"\n";
cout<<"3.display"<<"\n";
cout<<"4.pre-order display"<<"\n";
cout<<"5.post-order display"<<"\n";
cout<<"6.in-order display"<<"\n";
cout<<"7.level by display"<<"\n";
cout<<"8.mirror of tree"<<"\n";
cout<<"9.non-leaf nodes"<<"\n";
cout<<"10.leaf nodes"<<"\n";
cout<<"11.height"<<"\n";
cout<<"12.delete by copy"<<"\n";
cout<<"13.Exit"<<"\n";
cout<<"\n\nenter your choice"<<"\n";
cin>>c;
switch(c)
{
case 1:cout<<"enter the element to be inserted:"<<"\n";
cin>>n;
b.insert(n);
break;
case 2:cout<<"enter thr element to be searched:"<<"\n";
cin>>n;
cout<<"\n\nPRE_ORDER"<<"\n";
b.VLR_preorder(b.retroot());
int t=b.search(n);
if(t!=0)
cout<<"\n\nELEMENT FOUND"<<"\n";
else
cout<<"\n\nELEMENT NOT PRESENT"<<"\n";
getch();
break;
case 3: cout<<"\n\nPRE_ORDER"<<"\n";
b.VLR_preorder(b.retroot());
getch();
break;
case 4: cout<<"\n\nPRE_ORDER"<<"\n";
b.VLR_preorder(b.retroot());
getch();
break;
case 5: cout<<"\n\nPOST-ORDER"<<"\n";
b.LRV_postorder(b.retroot());
getch();
break;
case 6: cout<<"\n\nIN-ORDER"<<"\n";
b.LVR_inorder(b.retroot());
getch();
break;
case 7: cout<<"\n\nLEVEL BY LEVEL"<<"\n";
b.level();
getch();
case 8: cout<<"\n\nMIRROR OF TREE"<<"\n";
b.mirror();
cout<<"\n\nPRE-ORDER "<<"\n";
b.VLR_preorder(b.retroot());
getch();
break;
case 9: cout<<"\n\nPRE-ORDER"<<"\n";
b.VLR_preorder(b.retroot());
cout<<"\n\nNON LEAF NODES ARE:"<<"\n";
t=b.nonleaf(b.retroot());
cout<<t;
getch();
break;
case 10: cout<<"\n\nPRE-ORDER"<<"\n";
b.VLR_preorder(b.retroot());
cout<<"\n\nLEAF NODES ARE:"<<"\n";
t=b.leaf(b.retroot());
cout<<t;
getch();
break;
case 11: cout<<"\n\nPRE-ORDER"<<"\n";
b.VLR_preorder(b.retroot());
cout<<"\n\nHEIGHT OF TREE:"<<"\n";
t=b.height(b.retroot());
cout<<t;
getch();
break;
case 12: cout<<"\n\nPRE-ORDER"<<"\n";
b.VLR_preorder(b.retroot());
b.delbycopy(b.retroot());
cout<<"\n\nPRE-ORDER"<<"\n";
b.VLR_preorder(b.retroot());
getch();
break;
case 13: exit(0);
default:cout<<"invalid key"<<"\n";
}
}while(c<14);
getch();
}
actually i want to implement the deletion operation for example deletebymerging ,deletebycopying
my function definiton is void deletebycopy(node* &ptr) them can we call this function in void main this program is okay or not