Design, develop and implement a menu-driven program in C for the following operations on BINARY SEARCH TREE(BST) of integers

Design, develop and implement a menu-driven program in C for the following operations on BINARY SEARCH TREE(BST) of integers

Design, develop and implement a menu-driven program in C for the following operations on BINARY SEARCH TREE(BST) of integers

a. Create a BST of n integers: 6,9,5,2,8,15,24,14,7,8,5,2
b. Traverse the BST in inorder,pre-order and post-order
c. Search the BST for a given element (KEY) and report appropriate message
d. Exit

Program:- 

// PROGRAM A IS ITERATIVE METHOD AND B IS RECURSIVE METHOD. //ANYONE CAN BE CHOSEN FOR EXECUTION.

//A.

#include<stdio.h>
#include<conio.h>
struct BST
{
int data;
struct BST *left,*right;
};
typedef struct BST node;
node *root=NULL;
node *insert(node *root)
{
node *newnode,*temp,*par;
newnode=(node*)malloc(sizeof(node));
printf(“\n Enter the value”);
scanf(“%d”,&newnode->data);
newnode->left=newnode->right=NULL;
if(root==NULL)
{
root=newnode;
return root;
}
temp=root;
while(temp!=NULL)
{
par=temp;
if(newnode->data > temp->data)
temp=temp->right;
else
temp=temp->left;
}
if(newnode->data > par->data)
par->right=newnode;
else
par->left=newnode;
return root;
}
void inorder(node *root)
{
if(root!=NULL)
{
inorder(root->left);
printf(“%d\t”,root->data);
inorder(root->right);
}
}
void preorder(node *root)
{
if(root!=NULL)
{
printf(“%d\t”,root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf(“%d\t”,root->data);
}
}
void search(node *root, int key)
{
node *temp;
temp=root;
int flag=0;
while(temp!=NULL)
{
if(temp->data==key)
{
flag=1;break;
}
else if(key < temp->data)
temp=temp->left;
else
temp=temp->right;
}
if(flag==1)
printf(“\n Key found”);
else
printf(“\n Key not found”);
}
int main()
{
node *root=NULL;
int ch,key;
while(1)
{
printf(“\n Enter\n 1:insert\t 2:traverse\t 3:search\t 4:delete\t 5:exit”);
scanf(“%d”,&ch);
switch(ch)
{
case1: printf(“\n Enter no of terms”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
root=insert(root);
break;
case 2: if(root==NULL)
printf(“\n Tree empty”);
else
{
printf(“Inorder traversal\n”);
inorder(root);
printf(“Preorder traversal\n”);
preorder(root);
printf(“Postorder traversal\n”);
postorder(root);
}
break;
case 3: if(root==NULL)
printf(“\n Tree empty”);
else
{
printf(“\n Enter the key”);
scanf(“%d”,&key);
search(root,key);
}
break;
case 4: exit(0);
break;
}
}
return 0;
}

OR

B.

 

#include<stdio.h>
#include<conio.h>
struct BST
{
int data;
struct bst *left,*right;
};
typedef struct BST node;
node *root=NULL;
node* insert(node *root,int key)
{
if(root == NULL)
{
root=(node *)malloc(sizeof(node));
root->left= root->right = NULL;
root->data=key;
return root;
}
if( key < root->data )
root->left=insert(root->left,key);
else if( key > root->data )
root->right=insert(root->right,key);
else
printf(" Duplicate Element !! Not Allowed !!!");
return root;
}
void inorder(node *root)
{
if (root!=NULL)
{
inorder(root->left);
printf("%d ",root->data);
inorder(root->right);
}
}
void preorder(node *root)
{
if(root!=NULL)
{
printf(“%d\t”,root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf(“%d\t”,root->data);
}
}
int search(node *root,int key)
{
while(root != NULL)
{
if (key < root->data)
return search(root->left,key);
else if (key > root->data)
return search(root->right,key);
else
return 1;
}
return 0;
}
int main()
{
int ch, key, flag;
while(1)
{
printf("Program for BST operations\n");
printf("1.Insert\t2 2. Traversal \t 3.search \t 4. Exit\n");
printf("enter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("enter the key to be inserted\n");
scanf("%d",&key);
root=insert(root,key);
break;
case 2:
if(root == NULL)
printf("tree empty ");
else
{
printf(“Inorder traversal\n”);
inorder(root);
printf(“Preorder traversal\n”);
preorder(root);
printf(“Postorder traversal\n”);
postorder(root);
}
break;
case 3:
printf("enetr the ele to be searched\n");
scanf("%d",&key);
flag=search(root,key);
if (flag==1)
printf("ele found\n");
else
printf("not found\n");
break;
case 4: exit(0);
default:printf("invalid key");
}
}
}

Click to rate this post!
[Total: 0 Average: 0]

We will be happy to hear your thoughts

Leave a reply

EducationTrick
Logo
Enable registration in settings - general