Program
#include "stdio.h"
#include "conio.h"
#include "alloc.h"
#include "stdlib.h"
struct node
{
struct node *lc;
int data;
struct node *rc;
};
struct node *tree,*btree=NULL,*newnode,*temp;
struct node *getnode();
struct node *createbtree();
struct node *insertnode(struct node *btree,struct node *temp);
void inorder(struct node *btree);
void preorder(struct node *btree);
void postorder(struct node *btree);
void main()
{
int cho;
clrscr();
printf("\n\t\tBinary Tree Traversal\n");
printf("\n\t\t---------------------------\n");
btree=createbtree();
do{
printf("\nBinary Tree Traversal\n");
printf("1.Preorder Traversal\n2.Inorder Traversal\n");
printf("3.Postorder Traversal\n4.Exit");
printf("\nEnter
scanf("%d",&cho);
if(btree==NULL)
printf("\nBinary tree is empty\n");
switch(cho)
{
case 1:
printf("\nPreorder Traversal is:");
preorder(btree);
break;
case 2:
printf("\nInorder Traversal is:");
inorder(btree);
break;
case 3:
printf("\nPostorder Traversal is:");
postorder(btree);
break;
case 4:
exit(0);
getch();
}
}while(cho!=4);
free(btree);
getch();
}
struct node *getnode()
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter node's data:");
scanf("%d",&newnode- >data);
newnode- >lc=NULL;
newnode- >rc=NULL;
return(newnode);
}
struct node *createbtree()
{
char ch;
do{
temp=getnode();
btree=insertnode(btree,temp);
fflush(stdin);
printf("\nDo u wish to add data(y/n):");
scanf("%c",&ch);
}while((ch=='Y')||(ch=='y'));
return(btree);
}
struct node *insertnode(struct node *btree,struct node *temp)
{
if(btree==NULL)
return temp;
else if(temp- >data < btree- >data)
btree- >lc=insertnode(btree- >lc,temp);
else if(temp- >data > btree- >data)
btree- >rc=insertnode(btree- >rc,temp);
else if(temp- >data == btree- >data)
{
printf("\nData already exists");
return(btree);
}
return(btree);
}
void inorder(struct node *btree)
{
if(btree!=NULL)
{
inorder(btree- >lc);
printf("%d ",btree- >data);
inorder(btree- >rc);
}
}
void preorder(struct node *btree)
{
if(btree!=NULL)
{
printf("%d ",btree- >data);
preorder(btree- >lc);
preorder(btree- >rc);
}
}
void postorder(struct node *btree)
{
if(btree!=NULL)
{
postorder(btree- >lc);
postorder(btree- >rc);
printf("%d",btree- >data);
}
}
Output
Enter node's data: 5
Do u wish to add data(y/n): y
Enter node's data: 4
Do u wish to add data(y/n): y
Enter node's data: 7
Do u wish to add data(y/n): y
Enter node's data: 3
Do u wish to add data(y/n): y
Enter node's data: 1
Do u wish to add data(y/n): n
Binary Tree Traversal
1.Preorder Traversal
2.Inorder Traversal
3.Postorder Traversal
4.Exit
Enter your choice: 1
Preorder Traversal is: 5 4 3 1 7
Binary Tree Traversal
1.Preorder Traversal
2.Inorder Traversal
3.Postorder Traversal
4.Exit
Enter your choice: 2
Inorder Traversal is: 1 3 4 5 7
Binary Tree Traversal
1.Preorder Traversal
2.Inorder Traversal
3.Postorder Traversal
4.Exit
Enter your choice: 3
Postorder Traversal is: 1 3 4 7 5
Binary Tree Traversal
1.Preorder Traversal
2.Inorder Traversal
3.Postorder Traversal
4.Exit
Enter your choice: 4