#include<stdio.h>
#include<malloc.h>
struct tree
{
int data;
struct tree *left;
struct tree *right;
}*temp;
void input(struct tree *root)
{
int a,ch;
printf("Enter the data:");
scanf("%d",&a);
root->data=a;
printf("Do %d hav left child(1/0):",root->data);
scanf("%d",&ch);
if(ch==1)
{
temp=malloc(sizeof(struct tree));
root->left=temp;
input(temp);
}
else{
root->left=NULL;
}
printf("Do %d hav right child(1/0):",root->data);
scanf("%d",&ch);
if(ch==1)
{
temp=malloc(sizeof(struct tree));
root->right=temp;
input(temp);
}
else{
root->right=NULL;
}
}
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,ch;
printf("Enter the data:");
scanf("%d",&a);
root->data=a;
printf("Do %d hav left child(1/0):",root->data);
scanf("%d",&ch);
if(ch==1)
{
temp=malloc(sizeof(struct tree));
root->left=temp;
input(temp);
}
else{
root->left=NULL;
}
printf("Do %d hav right child(1/0):",root->data);
scanf("%d",&ch);
if(ch==1)
{
temp=malloc(sizeof(struct tree));
root->right=temp;
input(temp);
}
else{
root->right=NULL;
}
}
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:-