Treetraversals
Treetraversals
h>
#include<stdlib.h>
struct BST
{
int data;
struct BST *lchild;
struct BST *rchild;
};
typedef struct BST * NODE;
NODE create()
{
NODE temp;
temp = (NODE) malloc(sizeof(struct BST));
printf("\nEnter The value: ");
scanf("%d", &temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
void main()
{
int ch, key, val, i, n;
NODE root = NULL, newnode;
while(1)
{
printf("\n~~~~BST MENU~~~~");
printf("\n1.Create a BST");
printf("\n2.Search");
printf("\n3.BST Traversals: ");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1: printf("\nEnter the number
of elements: ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
newnode =
create();
if (root ==
NULL)
root = newnode;
else
insert(root, newnode);
}
break;
case 2: if (root == NULL)
printf("\
nTree Is Not Created");
else
{
printf("\
nThe Preorder display : ");
preorder(root);
printf("\
nThe Inorder display : ");
inorder(root);
printf("\
nThe Postorder display : ");
postorder(root);
}
break;
case 3: search(root);
break;
case 4: exit(0);
}
}
}