#include<conio.h>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
struct node
{
struct node *lchild,*rchild;
int info;
}*root;
void find(int,struct node **,struct node **);
void insert(int);
void del(int);
void case_a(struct node*,struct node*);
void case_b(struct node*,struct node*);
void case_c(struct node*,struct node*);
void preorder(struct node *);
void inorder(struct node *);
void postorder(struct node *);
main()
{
int choice,num;
root=NULL;
while(1)
{
cout<<"\n";
cout<<"1.INSERT"<<endl;
cout<<"2. DELETE"<<endl;
cout<<"3. INORDER TRAVERSAL"<<endl;
cout<<"4. PREORDER TRAVERSAL"<<endl;
cout<<"5. POSTORDER TRAVERSAL"<<endl;
cout<<"6. QUIT"<<endl;
cout<<"enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the number to be inserted"<<endl;
cin>>num;
insert(num);
break;
case 2:
cout<<"Enter the number to be deleted"<<endl;
cin>>num;
del(num);
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(1);
default:
cout<<"WRONG CHOICE"<<endl;
}
}
}
void find(int item,struct node**par,struct node**loc)
{
struct node *ptr,*ptrsave;
if(root==NULL)
{
*loc=NULL;
*par=NULL;
return;
}
if(item==root->info)
{
*loc=root;
*par=NULL;
return;
}
if(item<root->info)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;
while(ptr!=NULL)
{
if(item==ptr->info)
{
*loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}
*loc=NULL;
*par=ptrsave;
}
void insert(int item)
{
struct node *tmp,*parent,*location;
find(item,&parent,&location);
if(location!=NULL)
{
cout<<"Item Is Already Present"<<endl;
return;
}
tmp=new(struct node);
tmp->info=item;
tmp->lchild=NULL;
tmp->rchild=NULL;
if(parent==NULL)
root=tmp;
else
if(item<parent->info)
parent->lchild=tmp;
else
parent->rchild=tmp;
}
void del(int item)
{
struct node *parent,*location;
if(root==NULL)
{
cout<<"TREE EMPTY"<<endl;
return;
}
find(item,&parent,&location);
if(location==NULL)
{
cout<<"item not presented in the tree"<<endl;
return;
}
if(location->lchild==NULL&&location->rchild==NULL)
case_a(parent,location);
if(location->lchild!=NULL&&location->rchild==NULL)
/*case_b(parent,location);
if(location->lchild==NULL&&location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL&&location->rchild!=NULL)
case_c(parent,location);*/
free(location);
}
void case_a(struct node *par,struct node *loc)
{
if(par==NULL)
root=NULL;
else
if(loc==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL;
}
void case_b(struct node *par,struct node *loc)
{
struct node *child;
if(loc->lchild!=NULL)
child=loc->lchild;
else
child=loc->rchild;
if(par==NULL)
root=child;
else
if(loc==par->lchild)
par->lchild=child;
else
par->rchild=child;
}
void case_c(struct node *par,struct node *loc)
{
struct node *ptr,*ptrsave,*suc,*parsuc;
ptrsave=loc;
ptr=loc->rchild;
while(ptr->lchild!=NULL)
{
ptrsave=ptr;
ptr=ptr->lchild;
}
suc=ptr;
parsuc=ptrsave;
if(suc->lchild==NULL&&suc->rchild==NULL)
case_a(parsuc,suc);
else
case_b(parsuc,suc);
if(par==NULL)
root=suc;
else
if(loc==par->lchild)
par->lchild=suc;
else
par->rchild=suc;
suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}
void preorder(struct node *ptr)
{
if(root==NULL)
{
cout<<"TREE IS EMPTY"<<endl;
return;
}
if(ptr!=NULL)
{
cout<<ptr->info<<"\t";
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}
void inorder(struct node *ptr)
{
if(root==NULL)
{
cout<<"TREE IS EMPTY"<<endl;
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
cout<<ptr->info<<"\t";
inorder(ptr->rchild);
}
}
void postorder(struct node *ptr)
{
if(root==NULL)
{
cout<<"TREE IS EMPTY"<<endl;
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
cout<<ptr->info<<"\t";
}
}
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
struct node
{
struct node *lchild,*rchild;
int info;
}*root;
void find(int,struct node **,struct node **);
void insert(int);
void del(int);
void case_a(struct node*,struct node*);
void case_b(struct node*,struct node*);
void case_c(struct node*,struct node*);
void preorder(struct node *);
void inorder(struct node *);
void postorder(struct node *);
main()
{
int choice,num;
root=NULL;
while(1)
{
cout<<"\n";
cout<<"1.INSERT"<<endl;
cout<<"2. DELETE"<<endl;
cout<<"3. INORDER TRAVERSAL"<<endl;
cout<<"4. PREORDER TRAVERSAL"<<endl;
cout<<"5. POSTORDER TRAVERSAL"<<endl;
cout<<"6. QUIT"<<endl;
cout<<"enter your choice"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the number to be inserted"<<endl;
cin>>num;
insert(num);
break;
case 2:
cout<<"Enter the number to be deleted"<<endl;
cin>>num;
del(num);
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(1);
default:
cout<<"WRONG CHOICE"<<endl;
}
}
}
void find(int item,struct node**par,struct node**loc)
{
struct node *ptr,*ptrsave;
if(root==NULL)
{
*loc=NULL;
*par=NULL;
return;
}
if(item==root->info)
{
*loc=root;
*par=NULL;
return;
}
if(item<root->info)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;
while(ptr!=NULL)
{
if(item==ptr->info)
{
*loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item<ptr->info)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}
*loc=NULL;
*par=ptrsave;
}
void insert(int item)
{
struct node *tmp,*parent,*location;
find(item,&parent,&location);
if(location!=NULL)
{
cout<<"Item Is Already Present"<<endl;
return;
}
tmp=new(struct node);
tmp->info=item;
tmp->lchild=NULL;
tmp->rchild=NULL;
if(parent==NULL)
root=tmp;
else
if(item<parent->info)
parent->lchild=tmp;
else
parent->rchild=tmp;
}
void del(int item)
{
struct node *parent,*location;
if(root==NULL)
{
cout<<"TREE EMPTY"<<endl;
return;
}
find(item,&parent,&location);
if(location==NULL)
{
cout<<"item not presented in the tree"<<endl;
return;
}
if(location->lchild==NULL&&location->rchild==NULL)
case_a(parent,location);
if(location->lchild!=NULL&&location->rchild==NULL)
/*case_b(parent,location);
if(location->lchild==NULL&&location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL&&location->rchild!=NULL)
case_c(parent,location);*/
free(location);
}
void case_a(struct node *par,struct node *loc)
{
if(par==NULL)
root=NULL;
else
if(loc==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL;
}
void case_b(struct node *par,struct node *loc)
{
struct node *child;
if(loc->lchild!=NULL)
child=loc->lchild;
else
child=loc->rchild;
if(par==NULL)
root=child;
else
if(loc==par->lchild)
par->lchild=child;
else
par->rchild=child;
}
void case_c(struct node *par,struct node *loc)
{
struct node *ptr,*ptrsave,*suc,*parsuc;
ptrsave=loc;
ptr=loc->rchild;
while(ptr->lchild!=NULL)
{
ptrsave=ptr;
ptr=ptr->lchild;
}
suc=ptr;
parsuc=ptrsave;
if(suc->lchild==NULL&&suc->rchild==NULL)
case_a(parsuc,suc);
else
case_b(parsuc,suc);
if(par==NULL)
root=suc;
else
if(loc==par->lchild)
par->lchild=suc;
else
par->rchild=suc;
suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}
void preorder(struct node *ptr)
{
if(root==NULL)
{
cout<<"TREE IS EMPTY"<<endl;
return;
}
if(ptr!=NULL)
{
cout<<ptr->info<<"\t";
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}
void inorder(struct node *ptr)
{
if(root==NULL)
{
cout<<"TREE IS EMPTY"<<endl;
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
cout<<ptr->info<<"\t";
inorder(ptr->rchild);
}
}
void postorder(struct node *ptr)
{
if(root==NULL)
{
cout<<"TREE IS EMPTY"<<endl;
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
cout<<ptr->info<<"\t";
}
}