#include<stdio.h>
#include<malloc.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
}*temp;
void input(struct tree *root)
{
int a=0,ch;
struct tree *tmp,*p;
printf("Enter the data (enter '-1' to terminate):\n");
scanf("%d",&a);
root->data=a;
root->left=NULL;
root->right=NULL;
while(a!=-1)
{
scanf("%d",&a);
if(a==-1)break;
tmp=malloc(sizeof(struct tree));
tmp->data=a;
tmp->left=NULL;
tmp->right=NULL;
p=root;
while(1)
{
if(a<p->data)
{
if(p->left!=NULL)
p=p->left;
else{
p->left=tmp;
break;
}
}
else
{
if(p->right!=NULL)
p=p->right;
else{
p->right=tmp;
break;
}
}
}
}
}
void inorder(struct tree *tmp)
{
if(tmp->left!=NULL)
inorder(tmp->left);
printf("%d ",tmp->data);
if(tmp->right!=NULL)
inorder(tmp->right);
}
void preorder(struct tree *tmp)
{
printf("%d ",tmp->data);
if(tmp->left!=NULL)
preorder(tmp->left);
if(tmp->right!=NULL)
preorder(tmp->right);
}
void postorder(struct tree *tmp)
{
if(tmp->left!=NULL)
postorder(tmp->left);
if(tmp->right!=NULL)
postorder(tmp->right);
printf("%d ",tmp->data);
}
main()
{
struct tree *head=malloc(sizeof(struct tree));
input(head);
printf("\nInorder:-\n ");
inorder(head);
printf("\n ");
printf("\nPreorder:-\n ");
preorder(head);
printf("\n ");
printf("\nPostorder:-\n ");
postorder(head);
printf("\n ");
}
#include<malloc.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
}*temp;
void input(struct tree *root)
{
int a=0,ch;
struct tree *tmp,*p;
printf("Enter the data (enter '-1' to terminate):\n");
scanf("%d",&a);
root->data=a;
root->left=NULL;
root->right=NULL;
while(a!=-1)
{
scanf("%d",&a);
if(a==-1)break;
tmp=malloc(sizeof(struct tree));
tmp->data=a;
tmp->left=NULL;
tmp->right=NULL;
p=root;
while(1)
{
if(a<p->data)
{
if(p->left!=NULL)
p=p->left;
else{
p->left=tmp;
break;
}
}
else
{
if(p->right!=NULL)
p=p->right;
else{
p->right=tmp;
break;
}
}
}
}
}
void inorder(struct tree *tmp)
{
if(tmp->left!=NULL)
inorder(tmp->left);
printf("%d ",tmp->data);
if(tmp->right!=NULL)
inorder(tmp->right);
}
void preorder(struct tree *tmp)
{
printf("%d ",tmp->data);
if(tmp->left!=NULL)
preorder(tmp->left);
if(tmp->right!=NULL)
preorder(tmp->right);
}
void postorder(struct tree *tmp)
{
if(tmp->left!=NULL)
postorder(tmp->left);
if(tmp->right!=NULL)
postorder(tmp->right);
printf("%d ",tmp->data);
}
main()
{
struct tree *head=malloc(sizeof(struct tree));
input(head);
printf("\nInorder:-\n ");
inorder(head);
printf("\n ");
printf("\nPreorder:-\n ");
preorder(head);
printf("\n ");
printf("\nPostorder:-\n ");
postorder(head);
printf("\n ");
}
OUTPUT:-