#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class NODE
{
public:
int data,lb,rb;
NODE *lc,*rc;
void create(NODE*);
void inorder(NODE*);
void preorder(NODE*);
void postorder(NODE*);
};
NODE
*root,*nw,*temp,*head;
int a[10];
void
NODE::create(NODE *head)
{
root=new(NODE);
cout<<"\nEnter root
data:";
cin>>root->data;
head->lc=root;
head->lb=1;
root->lc=head;
root->rc=head;
root->lb=0;
root->rb=0;
char ch,ch1;
do
{
nw=new(NODE);
cout<<"Enter new
data:";
cin>>nw->data;
nw->lb=0;
nw->rb=0;
temp=root;
while(1)
{
cout<<"\nDo you
want to insert to the left/right of
"<<temp->data<<"?l/r:";
cin>>ch;
if(ch=='l'||ch=='L')
{
if(temp->lb==1)
{
temp=temp->lc;
}
else
{
nw->lc=temp->lc;
temp->lc=nw;
nw->rc=temp;
temp->lb=1;
break;
}
}
else
{
if(temp->rb==1)
{
temp=temp->rc;
}
else
{
nw->rc=temp->rc;
temp->rc=nw;
nw->lc=temp;
temp->rb=1;
break;
}
}
}
cout<<"Do you want to
continue?";
cin>>ch1;
}while(ch1=='y'||ch1=='Y');
}
void
NODE::inorder(NODE *head)
{
temp=head->lc;
while(temp!=head)
{
while(temp->lb!=0)
temp=temp->lc;
while(temp->rb!=1)
{
cout<<temp->data;
temp=temp->rc;
}
if(temp==head)
break;
cout<<temp->data;
temp=temp->rc;
}
}
void
NODE::preorder(NODE *head)
{
temp=head->lc;
while(temp!=head)
{
while(1)
{
cout<<temp->data;
if(temp->lb==0)
break;
else
temp=temp->lc;
}
while(temp->rb!=1)
{
temp=temp->rc;
}
temp=temp->rc;
}
}
void
NODE::postorder(NODE *head)
{
int i=-1;
temp=head->lc;
while(1)
{
while(1)
{
i++;
a[i]=temp->data;
if(temp->rb==0)
break;
temp=temp->rc;
}
while(temp->lb!=1)
temp=temp->lc;
if(temp==head)
break;
temp=temp->lc;
}
while(i!=-1)
{
cout<<a[i];
i--;
}
}
void main()
{
int sw;
NODE a;
clrscr();
do
{
cout<<"\n\n\n\n\n\tMenu\n1.Create
root\n2.Inorder\n3.Preorder\n4.Postorder\n5.Exit\nEnter Choice:";
cin>>sw;
switch(sw)
{
case 1:
head=new(NODE);
head->lc=head;
head->rc=head;
head->data=999;
head->lb=0;
head->rb=1;
a.create(head);
break;
case 2:
a.inorder(head);
break;
case 3:
a.preorder(head);
break;
case 4:
a.postorder(head);
break;
case 5:
exit(0);
break;
default:
cout<<"\nEnter
correct choice";
break;
}
}while(sw<5);
}
----------------------Output-----------------------
Menu
1.Create root
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter Choice:1
Enter root data:5
Enter new data:7
Do you want to
insert to the left/right of 5?l/r:r
Do you want to
continue?y
Enter new data:2
Do you want to insert
to the left/right of 5?l/r:l
Do you want to
continue?y
Enter new data:4
Do you want to
insert to the left/right of 5?l/r:l
Do you want to
insert to the left/right of 2?l/r:l
Do you want to
continue?y
Enter new data:9
Do you want to
insert to the left/right of 5?l/r:r
Do you want to
insert to the left/right of 7?l/r:r
Do you want to
continue?y
Enter new data:6
Do you want to
insert to the left/right of 5?l/r:r
Do you want to
insert to the left/right of 7?l/r:l
Do you want to
continue?n
Menu
1.Create root
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter Choice:2
425679
Menu
1.Create root
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter Choice:3
524769
Menu
1.Create root
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter Choice:4
426975
Menu
1.Create root
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter Choice:5