#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
struct node *leftchild;
int data;
struct node *rightchild;
}NODE;
NODE* get_node()
{
NODE *t;
t = (NODE*)malloc(sizeof(NODE));
printf("\nEnter Info: ");
scanf("%d",&t->data);
t->leftchild = t->rightchild = NULL;
return t;
}
NODE * create_tree()
{
NODE *temp,*ptr,*root=NULL;
char direction,ch;
do
{
temp = get_node();
if(root==NULL)
root = temp;
else
{
ptr = root;
while(1)
{
printf("The current node is %d\n",ptr->data);
printf("Enter Direction (L)eft/(R)ight: ");
direction = getche();
if(direction=='L')
{
if(ptr->leftchild!=NULL)
ptr=ptr->leftchild;
else
{
ptr->leftchild = temp;
break;
}
}
else if(direction == 'R')
{
if(ptr->rightchild!=NULL)
ptr=ptr->rightchild;
else
{
ptr->rightchild = temp;
break;
}
}
}
}
printf("\nAdd More Y/N?");
ch = getche();
}while(ch=='Y');
return root;
}
void inorder(NODE *h)
{
if(h!=NULL)
{
inorder(h->leftchild);
printf("%d ",h->data);
inorder(h->rightchild);
}
}
void preorder(NODE *h)
{
if(h!=NULL)
{
printf("%d ",h->data);
preorder(h->leftchild);
preorder(h->rightchild);
}
}
void postorder(NODE *h)
{
if(h!=NULL)
{
postorder(h->leftchild);
postorder(h->rightchild);
printf("%d ",h->data);
}
}
void main()
{
NODE *root=NULL;
clrscr();
root=create_tree();
printf("\nRecursive Inorder : ");
inorder(root);
printf("\nRecursive Preorder : ");
preorder(root);
printf("\nRecursive Postorder : ");
postorder(root);
getch();
}
Output-
Enter Info: 3 4 5 6 7 8
Add More Y/N?Y
Enter Info: The current node is 3
Enter Direction (L)eft/(R)ight: R
Add More Y/N?Y
Enter Info: The current node is 3
Enter Direction (L)eft/(R)ight: L
Add More Y/N?N
Recursive Inorder : 5 3 4
Recursive Preorder : 3 5 4
Recursive Postorder : 5 4 3