#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class NODE
{
public:
int data;
NODE *lc,*rc;
void create(NODE*);
void inorder(NODE*);
void preorder(NODE*);
void postorder(NODE*);
};
NODE *root,*nw;
void
NODE::create(NODE *root)
{
char ch;
cout<<"\nDo you want to insert
to the left?"<<root->data<<"y/n";
cin>>ch;
if(ch=='y'||ch=='Y')
{
nw=new(NODE);
nw->lc=NULL;
nw->rc=NULL;
cout<<"\nEnter left child
data:";
cin>>nw->data;
root->lc=nw;
create(root->lc);
}
cout<<"\nDo you want to insert
to the Right?"<<root->data<<"y/n";
cin>>ch;
if(ch=='y'||ch=='Y')
{
nw=new(NODE);
nw->lc=NULL;
nw->rc=NULL;
cout<<"\nEnter left child
data:";
cin>>nw->data;
root->rc=nw;
create(root->rc);
}
}
void
NODE::inorder(NODE *root)
{
if(root==NULL)
return;
inorder(root->lc);
cout<<root->data;
inorder(root->rc);
}
void
NODE::preorder(NODE *root)
{
if(root==NULL)
return;
cout<<root->data;
preorder(root->lc);
preorder(root->rc);
}
void
NODE::postorder(NODE *root)
{
if(root==NULL)
return;
postorder(root->lc);
postorder(root->rc);
cout<<root->data;
}
int main()
{
int sw;
NODE a;
clrscr();
do
{
cout<<"\nMenu\n1.Create
root\n2.Inorder\n3.Preorder\n4.Postorder\n5.Exit\nEnter Choice:";
cin>>sw;
switch(sw)
{
case 1:
root=new(NODE);
root->lc=NULL;
root->rc=NULL;
cout<<"\nEnter
root data:";
cin>>root->data;
a.create(root);
break;
case
2:
a.inorder(root);
break;
case 3:
a.preorder(root);
break;
case 4:
a.postorder(root);
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:1
Do you want to
insert to the Left of 1 y/n:y
Enter left child
data:2
Do you want to
insert to the Left of 2 y/n:y
Enter left child
data:3
Do you want to
insert to the Left of 3 y/n:n
Do you want to
insert to the Right of 3 y/n:n
Do you want to
insert to the Right of 2 y/n:n
Do you want to
insert to the Right of 1 y/n:y
Enter right child
data:4
Do you want to
insert to the Left of 4 y/n:n
Do you want to
insert to the Right of 4 y/n:n
Menu
1.Create root
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter Choice:2
3214
Menu
1.Create root
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter Choice:3
1234
Menu
1.Create root
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter Choice:4
3241
Menu
1.Create root
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter Choice:5