Program of B.S.T.

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

(8)WAP in C to implement Binary Search Tree.

#include<stdio.h>

#include<conio.h>

#include<alloc.h>

struct tree

int head;

//int a[4];

struct tree *l, *r;

};

struct tree *r,*t;

int ch1,n,flag=0,in,n=1;

void main()

char ch='y';

r=NULL;

clrscr();

while (ch=='y')

clrscr();

printf("\n 1.Creat tree");

printf("\n 2.Traverse In-Order");

printf("\n 3.Traverse Pre-Order");


printf("\n 4.Traverse Post-Order");

printf("\n 5.Exit");

printf("\n Enter chioce");

scanf("%d",&ch1);

switch (ch1)

case 1:

if (insert(&r)==0)

n++;

break;

case 2:

printf("\n\n Left,Root,Right");

inorder(r);

getch();

break;

case 3:

printf("\n \n Root,Left,Right");

preorder(r);

getch();

break;

case 4:
printf("\n\n Left,Right,Root");

postorder(r);

getch();

break;

case 5:

exit();

default :

printf(" Wrong Choice ");

}// switch close

}// while clsoe

getch();

} //main close

insert(struct tree **ptr)

if (*ptr==NULL)

*ptr=malloc (sizeof(struct tree));

printf("\n Enter the element ");

scanf("%d",&n);

(*ptr)->head=n;

(*ptr)->l=NULL;

(*ptr)->r=NULL;
return(0);

//flag=1;}

else

clrscr();

printf("\n Left Element");

printf("\n Right Element");

printf("\n Exit");

printf("\n Enter choice");

scanf("%d",&in);

switch(in)

case 1:

insert(&(*ptr)->l);

break;

case 2:

insert(&(*ptr)->r);

break;

case 3:

break;

default:

printf("\n Choice is wrong");

}// switch
return(0);

} //fun close

inorder(struct tree *ptr)

if(ptr!=NULL)

inorder(ptr->l);

printf("%d",ptr->head);

inorder(ptr->r);

} // if close

return(0);

} //fun close

preorder(struct tree *ptr)

if (ptr!=NULL)

printf("%d",ptr->head);

preorder(ptr->l);

preorder(ptr->r);

} // if close

return(0);
} //fun close

postorder(struct tree *ptr)

if (ptr!=NULL)

postorder(ptr->l);

postorder(ptr->r);

printf("%d",ptr->head);

} // if close

return(0);

} //fun close

You might also like