#include
#include
Struct node
{
int info;
struct node *link,*right;
}*btree;
main()
{
int ele,opt;
char ans;
void inoder(struct node*);
void preorder(struct node*);
void postorder(struct node*);
struct node*insert(struct node*,int);
btree=NULL;
while(1)
{
printf("\n Main menu ");
printf("\n\t1. Create");
printf("\n\t2. inoder");
printf("\n\t3. Preorder");
printf("\n\t4. postorder");
printf("\n\t5. Exit");
printf("\n Enter ur choice");
scanf("%d",&opt);
switch(opt)
{
case 1:
while(1)
{
printf("\n Enter element");
scanf("%d",&ele);
btree=insert(btree,ele);
printf("\n want to continue(y/n)");
fflush(stdin);
scanf(“%c”,&ans);
if(ans==’y’||ans==’Y’)
continue;
else
break;
}
break;
case 2:
printf(“\n inorder traversal”);
inorder(btree);
break;
case 3:
printf(“\n preorder traversal”);
preorder(btree);
break;
case 4:
printf(“\n postorder traversal”);
postorder(btree);
break;
case 5:
exit(0);
break;
default :
printf("\n Wrong choice");
break;
}
}
}
struct node *insert(struct node *btree,int ele)
{
if(btree==NULL)
{
btree=(struct node*)malloc(sizeof(struct node));
btree->info=ele;
btree->left=NULL;
}
else
if(ele>btree->info)
btree->right=insert(btree->right,ele);
else
if(eleinfo)
btree->left=insert(btree->left,ele);
else
printf(“\n duplicate node”);
return(btree);
}
void inorder(struct node *btree)
{
if(btree!=NULL)
{
inorder(btree->left);
printf(“\t %d”,btree->info);
inorder(btree->right);
}
}
{
if(btree!=NULL)
{
printf(“\t %d”,btree->info);
preorder(btree->left);
preorder(btree->right);
}
}
{
if(btree!=NULL)
{
postrder(btree->left);
postorder(btree->right);
printf(“\t %d”,btree0->info);
}
}
Output:
Main menu
1. Create
2. inoder
3. Preorder
4. postorder
5. Exit
Enter ur choice1
Enter element 424
want to continue(y/n)y
Enter element410
want to continue(y/n)y
Enter element440
want to continue(y/n)n
Main menu
1. Create
2. inoder
3. Preorder
4. postorder
5. Exit
Enter ur choice2
inorder traversal 410 424 440
Main menu
1. Create
2. inoder
3. Preorder
4. postorder
5. Exit
Enter ur choice3
Preorder traversal 424 410 440
Main menu
1. Create
2. inoder
3. Preorder
4. postorder
5. Exit
Enter ur choice4
postorder traversal 410 440 424
Main menu
1. Create
2. inoder
3. Preorder
4. postorder
5. Exit
Enter ur choice5
No comments:
Post a Comment