#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
class NODE
{
public:
int data;
NODE *rchild,*lchild;
void create(NODE *root);
void inorder(NODE *root);
void preorder(NODE *root);
void postorder(NODE *root);
};
NODE *root,*nw;
void NODE::create(NODE
*root)
{
char ch;
cout<<"Do you want to insert at the left
of"<<root->data<<"(y/n)";
cin>>ch;
if(ch=='y'|| ch=='Y')
{
nw=new(NODE);
nw->lchild=NULL;
nw->rchild=NULL;
cout<<"Enter a new data";
cin>>nw->data;
root->lchild=nw;
create(root->lchild);
}
cout<<"Do you want to insert at the right
of"<<root->data<<"(y/n)";
cin>>ch;
if(ch=='y'|| ch=='Y')
{
nw=new(NODE);
nw->lchild=NULL;
nw->rchild=NULL;
cout<<"Enter a new data";
cin>>nw->data;
root->rchild=nw;
create(root->rchild);
}
}
void NODE::inorder(NODE
*root)
{
if(root==NULL)
return;
inorder(root->lchild);
cout<<"\n"<<root->data;
inorder(root->rchild);
}
void NODE::preorder(NODE
*root)
{
if(root==NULL)
return;
cout<<"\n"<<root->data;
preorder(root->lchild);
preorder(root->rchild);
}
void NODE::postorder(NODE
*root)
{
if(root==NULL)
return;
postorder(root->lchild);
postorder(root->rchild);
cout<<"\n"<<root->data;
}
void main()
{
NODE d;
int ch;
do
{
cout<<"\n\n\n1.create\n2.Inorder\n3.preorder\n4.postorder\n5.Exit"
;
cout<<"\nEnter Your
choice\n";
cin>>ch;
switch(ch)
{
case 1:
root=new(NODE);
root->lchild=NULL;
root->rchild=NULL;
cout<<"Enter
a new data";
cin>>root->data;
d.create(root);
break;
case 2:
cout<<"\n\ninorder
traversal ";
d.inorder(root);
break;
case 3:
cout<<"\n\npreorder
traversal";
d.preorder(root);
break;
case 4:
cout<<"\n\npostorder
traversal";
d.postorder(root);
break;
case 5:
exit(0);
break;
default:
cout<<"\ninvalid
choice";
break;
}
}
while(ch!=6);
getch();
}
-----------------------------------------/*OUTPUT*/---------------------------------------------
1.create
2.Inorder
3.preorder
4.postorder
5.Exit
Enter Your choice
1
Enter a new data32
Do you want to insert at
the left of32(y/n)y
Enter a new data48
Do you want to insert at
the left of48(y/n)y
Enter a new data56
Do you want to insert at
the left of56(y/n)n
Do you want to insert at
the right of56(y/n)n
Do you want to insert at
the right of48(y/n)y
Enter a new data62
Do you want to insert at
the left of62(y/n)n
Do you want to insert at
the right of62(y/n)n
Do you want to insert at
the right of32(y/n)y
Enter a new data59
Do you want to insert at
the left of59(y/n)y
Enter a new data8
Do you want to insert at
the left of8(y/n)n
Do you want to insert at
the right of8(y/n)n
Do you want to insert at
the right of59(y/n)y
Enter a new data72
Do you want to insert at
the left of72(y/n)n
Do you want to insert at
the right of72(y/n)n
1.create
2.Inorder
3.preorder
4.postorder
5.Exit
Enter Your choice
2
inorder traversal
56
48
62
32
8
59
72
1.create
2.Inorder
3.preorder
4.postorder
5.Exit
Enter Your choice
3
preorder traversal
32
48
56
62
59
8
72
1.create
2.Inorder
3.preorder
4.postorder
5.Exit
Enter Your choice
4
postorder traversal
56
62
48
8
72
59
32
1.create
2.Inorder
3.preorder
4.postorder
5.Exit
Enter Your choice