#include<stdio.h>
#include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root=NULL;
struct node *insert(struct node*,int);
void displayPreorder(struct node*);
void displayInorder(struct node*);
void displayPostorder(struct node*);
int count=1;
void main()
{
int value,choice;
while(1)
{
printf("\n1.Insert");
printf("\n2.Display in pre order");
printf("\n3.Display in In-order");
printf("\n4.Display in Post order");
printf("\n5.Exit");
printf("\nEnter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter Value\n");
scanf("%d",&value);
root=insert(root, value);
break;
case 2:
displayPreorder(root);
break;
case 3:
displayInorder(root);
break;
case 4:
displayPostorder(root);
break;
case 5:
exit(1);
break;
default:
printf("\nInvalid Input");
break;
}
}
}
struct node *insert(struct node *r, int v)
{
if(r==NULL)
{
r=(struct node*)malloc(sizeof(struct node));
r->left=NULL;
r->right=NULL;
r->data=v;
count++;
}
else{
if(count%2==0)
{
r->left=insert(r->left,v);
}
else{
r->right=insert(r->right,v);
}
}
return r;
}
void displayPreorder(struct node *r)
{
if(r!=NULL)
{
printf("%d\n",r->data);
displayPreorder(r->left);
displayPreorder(r->right);
}
}
void displayInorder(struct node *r)
{
if(r!=NULL)
{
displayInorder(r->left);
printf("%d\n",r->data);
displayInorder(r->right);
}
}
void displayPostorder(struct node *r)
{
if(r!=NULL)
{
displayPostorder(r->left);
displayPostorder(r->right);
printf("%d\n",r->data);
}
}
#include<conio.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
struct node *root=NULL;
struct node *insert(struct node*,int);
void displayPreorder(struct node*);
void displayInorder(struct node*);
void displayPostorder(struct node*);
int count=1;
void main()
{
int value,choice;
while(1)
{
printf("\n1.Insert");
printf("\n2.Display in pre order");
printf("\n3.Display in In-order");
printf("\n4.Display in Post order");
printf("\n5.Exit");
printf("\nEnter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter Value\n");
scanf("%d",&value);
root=insert(root, value);
break;
case 2:
displayPreorder(root);
break;
case 3:
displayInorder(root);
break;
case 4:
displayPostorder(root);
break;
case 5:
exit(1);
break;
default:
printf("\nInvalid Input");
break;
}
}
}
struct node *insert(struct node *r, int v)
{
if(r==NULL)
{
r=(struct node*)malloc(sizeof(struct node));
r->left=NULL;
r->right=NULL;
r->data=v;
count++;
}
else{
if(count%2==0)
{
r->left=insert(r->left,v);
}
else{
r->right=insert(r->right,v);
}
}
return r;
}
void displayPreorder(struct node *r)
{
if(r!=NULL)
{
printf("%d\n",r->data);
displayPreorder(r->left);
displayPreorder(r->right);
}
}
void displayInorder(struct node *r)
{
if(r!=NULL)
{
displayInorder(r->left);
printf("%d\n",r->data);
displayInorder(r->right);
}
}
void displayPostorder(struct node *r)
{
if(r!=NULL)
{
displayPostorder(r->left);
displayPostorder(r->right);
printf("%d\n",r->data);
}
}