#include
#include
#include
#include
struct node
{
int data;
node *lftchild,*rtchild;
}*root,*temp,*t,*save;
void preorder(node *);
void inorder(node *);
void postorder(node *);
void main()
{
clrscr();
root=0;
int ch=0;
char c;
while(ch==0)
{
cout<
switch(ch)
{
case 1:
{
int item;
int found;
do
{
temp=(node *)malloc(sizeof (node));
cout<
temp->data=item;
found=0; //found is false
save=0;
t=root;
while(t!=0 && found==0)
{
save=t;
if(item==t->data)
found=1;
else
{
if(item
t=t->lftchild;
else
t=t->rtchild;
}
}
if(found==1) //found is true
cout<<"item is already in the tree"<
{
if(save==0)
{
root=temp;
root->lftchild=0;
root->rtchild=0;
}
else
{
if(item
save->lftchild=temp;
else
save->rtchild=temp;
}
}
cout<<"do you want to insert again(y/n)::";
cin>>c;
}while(c=='y' || c=='Y');
if(c=='n' || c=='N')
{
ch=0;
break;
}
}
case 2:
{
t=root;
preorder(t);
ch=0;
break;
}
case 3:
{
t=root;
inorder(t);
ch=0;
break;
}
case 4:
{
t=root;
postorder(t);
ch=0;
break;
}
case 5:
{
ch=6;
exit(0);
}
}
}
getch();
}
void inorder(node *t)
{
if(t!=0)
{
inorder(t->lftchild);
cout<
inorder(t->rtchild);
}
}
void preorder(node *t)
{
if(t!=0)
{
cout<
preorder(t->lftchild);
preorder(t->rtchild);
}
}
void postorder(node *t)
{
if(t!=0)
{
postorder(t->lftchild);
postorder(t->rtchild);
cout<
}
}
No comments:
Post a Comment