You are on page 1of 2

#include<stdio.

h>
#include<conio.h>

struct node
{
char ch;
struct node *left,*right;
}*root=NULL,*p;

void create();
void createlr(struct node *);
void preorder(struct node *);
void inorder(struct node *);
void postorder(struct node *);

void main()
{
clrscr();
create();
printf("\nPreorder Traversal:");
preorder(root);
printf("\nInorder Traversal:");
inorder(root);
printf("\nPostorder Traversal:");
postorder(root);
getch();
}

void create()
{
char c;
printf("Enter the value of root (Enter $ for null)");
fflush(stdin);
scanf("%c",&c);
if(c!='$')
{
p=(struct node *)malloc(sizeof(struct node));
p->ch=c;
p->left=NULL;
p->right=NULL;
root=p;
createlr(root);
}
}

void createlr(struct node *t)


{
char cr,cl;
printf("Enter the left child of %c (Enter $ for null)",t->ch);
fflush(stdin);
scanf("%c",&cl);
if(cl!='$')
{
p=(struct node *)malloc(sizeof(struct node));
p->ch=cl;
p->left=NULL;
p->right=NULL;
t->left=p;
}
printf("Enter the right child of %c (Enter $ for null)",t->ch);
fflush(stdin);
scanf("%c",&cr);
if(cr!='$')
{
p=(struct node *)malloc(sizeof(struct node));
p->ch=cr;
p->left=NULL;
p->right=NULL;
t->right=p;
}
if(cl!='$')
createlr(t->left);
if(cr!='$')
createlr(t->right);
}

void preorder(struct node *t)


{
if(t==NULL)
return;
print("%c",t->ch);
preorder(t->left);
preorder(t->right);
}

void inorder(struct node *t)


{
if(t==NULL)
return;
inorder(t->left);
print("%c",t->ch);
inorder(t->right);
}

void postorder(struct node *t)


{
if(t==NULL)
return;
postorder(t->left);
postorder(t->right);
print("%c",t->ch);
}

You might also like