0% found this document useful (0 votes)
5 views6 pages

DCS Lab Program No 10

The document provides a C program for performing operations on a Binary Search Tree (BST) of integers, including creating the BST with a predefined set of integers, traversing it in Inorder, Preorder, and Postorder, and searching for a specific key. The program features a menu-driven interface that allows users to select operations and interact with the BST. It includes functions for node creation, insertion, and traversal, as well as a search function to find elements within the tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

DCS Lab Program No 10

The document provides a C program for performing operations on a Binary Search Tree (BST) of integers, including creating the BST with a predefined set of integers, traversing it in Inorder, Preorder, and Postorder, and searching for a specific key. The program features a menu-driven interface that allows users to select operations and interact with the BST. It includes functions for node creation, insertion, and traversal, as well as a search function to find elements within the tree.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

10.

Develop 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, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
d. Exit
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *lchild;
struct node *rchild;
};
struct node *root, *temp;
struct node* create (int num)
{
temp = (struct node *) malloc (sizeof (struct node));
temp->data = num;
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
void insert (struct node *root, struct node *temp)
{
if (temp -> data < root -> data)
{
if (root -> lchild == NULL)
root -> lchild = temp;
else
insert(root -> lchild, temp);
}
if (temp -> data > root -> data)
{
if (root -> rchild == NULL)
root -> rchild = temp;
else
insert(root -> rchild, temp);
}
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->lchild);
printf("%d ", root->data);
inorder(root->rchild);
}
}
void preorder(struct node *root)
{
if (root != NULL)
{
printf("%d ", root -> data);
preorder(root -> lchild);
preorder(root -> rchild);
}
}
void postorder(struct node *root)
{
if (root != NULL)
{
postorder(root -> lchild);
postorder(root -> rchild);
printf("%d ", root -> data);
}
}
int search (struct node *root, int key)
{
if(root == NULL) return 0;
else if(key == root->data) return 1;
else if(key < root->data) return search(root->lchild, key);
else return search(root->rchild, key);
}
int main()
{
int choice, n, num, i, key, res;
while(1)
{
printf("\nBinary Search Tree (BST) Operations \n");
printf("_________________\n");
printf("1: Create a BST node \n");
printf("2: Inorder Traversal \n");
printf("3: Preorder Traversal \n");
printf("4: Postorder Traversal \n");
printf("5: Search a Key Element \n");
printf("6: Exit \n");
printf("Enter your choice \n");
scanf("%d", &choice);

switch(choice)
{
case 1: printf("Enter the number of elements \n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("Enter %d element\n", i);
scanf("%d", &num);
temp = create(num);
if (root == NULL)
root = temp;
else
insert(root, temp);
}
break;
case 2: printf("Inorder Traversal \n");
inorder(root);
break;
case 3: printf("Preorder Traversal \n");
preorder(root);
break;
case 4: printf("Postorder Traversal \n");
postorder(root);
break;
case 5: printf("\nEnter Element to be searched: ");
scanf("%d", & key);
res = search(root, key);
if(res)
printf("\nKey element is present in BST ");
else
printf("\nKey element is not found in the BST ");

break;
case 6: exit(0);
default: printf("Invalid Choice \n");
}
}
return 0;
}

Output1:
Output2:
Output3:

You might also like