P 10
P 10
Design, Develop and Implement a menu driven Program in C for the following operations on
Binary Search Tree(BST) of Integers
3. Search the BST for a given element (KEY) and report the appropriate message
4. Exit
#include <stdio.h>
#include <stdlib.h>
int flag=0;
int data;
} node;
/*FUNCTION PROTOTYPE*/
void main()
int choice;
int key;
node *get_node();
root = NULL;
do
{
printf("\n1.Create");
printf("\n2.Search");
printf("\n3.Recursive Traversals");
printf("\n4.Exit");
scanf("%d", &choice);
switch (choice)
case 1:
do
new_node = get_node();
scanf("%d", &new_node->data);
root = new_node;
else
insert(root, new_node);
scanf("%d",&ans);
} while (ans);
break;
case 2:
scanf("%d", &key);
if(flag==1)
else
{
flag=0;
break;
case 3:
if (root == NULL)
else
inorder(root);
preorder(root);
postorder(root);
break;
node *get_node()
node *temp;
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
/*This function is for creating a binary search tree */
if(root->lchild==NULL)
root->lchild=new_node;
else
insert(root->lchild, new_node);
if (root->rchild == NULL)
root->rchild = new_node;
else
insert(root->rchild, new_node);
/*This function is for searching the node from binary Search Tree*/
node *temp;
temp = root;
if (temp->data == key)
flag=1;
return temp;
}
*parent = temp;
temp = temp->lchild;
else
temp = temp->rchild;
return NULL;
if (temp != NULL)
inorder(temp->lchild);
printf("%d\t", temp->data);
inorder(temp->rchild);
if (temp != NULL)
printf("%d\t", temp->data);
preorder(temp->lchild);
preorder(temp->rchild);
{
if (temp != NULL)
postorder(temp->lchild);
postorder(temp->rchild);
printf("%d\t", temp->data);