You are on page 1of 3

WAP TO SHOW THE IMPLEMENTATION OF BINARY

TREE

#include<conio.h>
#include<stdio.h>

struct node
{
struct node *left,*rgt;
int info;
}*root;
char a[]={‘A’,’B’,’C’,’D’,’E’,’F’,’G’,NULL, NULL,’H’, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL};

struct node *buildtree(int i) //FUNCTION TO BUILD A TREE


{
struct node *trav= NULL;
if(a[i]!= NULL)
{
temp=malloc(sizeof(struct node));
temp->left=buildtree(2*i+1);
temp->info=a[i];
temp->rgt=buildtree(2*i+2);
}
return(temp);
}

void inorder(struct node *root) //FUNCTION TO FIND THE INORDER TRAVERSAL


OF TREE
{
if(root!= NULL)
{
inorder(root->left);
printf(“\t%c”,root->info);
inorder(root->rgt);
}
}

void preorder(struct node *root) //FUNCTION TO FIND THE PREORDER


TRAVERSAL OF TREE
{
if(root!= NULL)
{
printf(“\t%c”,root->info);
preorder(root->left);
preorder(root->rgt);
}
}

void postorder(struct node *root) //FUNCTION TO FIND THE POSTORDER


TRAVERSAL OF TREE
{
if(root!= NULL)
{
postorder(root->left);
preorder(root->rgt);
printf(“\t%c”,root->info);

}
}

void main()
{
int q;
clrscr();
root=buildtree(0);
start:
clrscr();
printf(“\n TO VIEW INORDER PRESS 1.”);
printf(“\n TO VIEW POSTORDER PRESS 2.”);
printf(“\n TO VIEW PREORDER PRESS 3.”);
printf(“\n ENTER YOUR CHOICE.-:”);
scanf(“%d,&q);

switch(q)
{
case1:
inorder(root);
break;
case2:
postorder(root);
break;
case3:
preorder(root);
break;
default:
printf(“\n WRONG CHOICE ENTERED.”);
};
printf(“\n WISH TO CONTINUE(1/0).-:”);
scanf(“%d”,&d);
if(d==1)
goto start;
getch();
}

OUTPUT

TO VIEW INORDER PRESS 1.


TO VIEW POSTORDER PRESS 2.
TO VIEW PREORDER PRESS 3.
ENTER YOUR CHOICE.-:1
DBHEAFCG
WISH TO CONTINUE(1/0).-:1

ENTER YOUR CHOICE.-:2
DHEBFGCA
WISH TO CONTINUE(1/0).-:1

ENTER YOUR CHOICE.-:3
ABDEHCFG
WISH TO CONTINUE(1/0).-:….