0% found this document useful (0 votes)
92 views7 pages

Prog_10 Binary Search Tree

Uploaded by

bheemanna171147
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)
92 views7 pages

Prog_10 Binary Search Tree

Uploaded by

bheemanna171147
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/ 7

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>

// Define the structure for a node in the BST

struct Node {

int data;

struct Node *left;

struct Node *right;

};

typedef struct Node NODE;

// Function to create a new node

struct Node* createNode(int key) {

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

newNode->data = key;

newNode->left = newNode->right = NULL;

return newNode;

// Function to insert a key into the BST

NODE* insert(struct Node* root, int key) {

if (root == NULL)

return createNode(key);
if (key < root->data)

root->left = insert(root->left, key);

else if (key > root->data)

root->right = insert(root->right, key);

return root;

// Function to perform inorder traversal of the BST

void inorder(NODE* root) {

if (root != NULL) {

inorder(root->left);

printf("%d ", root->data);

inorder(root->right);

// Function to perform preorder traversal of the BST

void preorder(NODE* root) {

if (root != NULL) {

printf("%d ", root->data);

preorder(root->left);

preorder(root->right);

// Function to perform postorder traversal of the BST

void postorder(NODE* root) {

if (root != NULL) {
postorder(root->left);

postorder(root->right);

printf("%d ", root->data);

// Function to search for a key in the BST

void search(NODE* root, int key) {

if (root == NULL) {

printf("Element %d not found in the BST.\n", key);

return;

if (key == root->data) {

printf("Element %d found in the BST.\n", key);

} else if (key < root->data) {

search(root->left, key);

} else {

search(root->right, key);

// Function to free the memory allocated for the BST nodes

void freeBST(struct Node* root) {

if (root != NULL) {

freeBST(root->left);

freeBST(root->right);

free(root);

}
int main() {

NODE *root = NULL;

int choice, key;

do {

printf("\nMenu:\n");

printf("1. Create a Binary Search Tree by Passing Keys\n");

printf("2. Traverse the BST in Inorder, Preorder, and Postorder\n");

printf("3. Search for a key in the BST\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

printf("Enter the key to insert: ");

scanf("%d", &key);

root = insert(root, key);

break;

case 2:

printf("Inorder Traversal: ");

inorder(root);

printf("\nPreorder Traversal: ");

preorder(root);

printf("\nPostorder Traversal: ");

postorder(root);

printf("\n");

break;
case 3:

printf("Enter the key to search: ");

scanf("%d", &key);

search(root, key);

break;

case 4:

freeBST(root);

printf("Exiting the program.\n");

break;

default:

printf("Invalid choice. Please enter a valid option.\n");

} while (choice != 4);

return 0;

Output:

Menu:

1. Create a Binary Search Tree by Passing Keys

2. Traverse the BST in Inorder, Preorder, and Postorder

3. Search for a key in the BST

4. Exit

Enter your choice: 1

Enter the key to insert: 6


Enter your choice: 1

Enter the key to insert: 9

Enter your choice: 1

Enter the key to insert: 5

Enter your choice: 1

Enter the key to insert: 2

Enter your choice: 1

Enter the key to insert: 8

Enter your choice: 1

Enter the key to insert: 15

Enter your choice: 1

Enter the key to insert: 24

Enter your choice: 1

Enter the key to insert: 14

Enter your choice: 1

Enter the key to insert: 7

Enter your choice: 1

Enter the key to insert: 8

Enter your choice: 1

Enter the key to insert: 5


Enter your choice: 1

Enter the key to insert: 2

Menu:

1. Create a Binary Search Tree by Passing Keys

2. Traverse the BST in Inorder, Preorder, and Postorder

3. Search for a key in the BST

4. Exit

Enter your choice: 2

Inorder Traversal: 2 5 6 7 8 9 14 15 24

Preorder Traversal: 6 5 2 9 8 7 15 14 24

Postorder Traversal: 2 5 7 8 14 24 15 9 6

Menu:

1. Create a Binary Search Tree by Passing Keys

2. Traverse the BST in Inorder, Preorder, and Postorder

3. Search for a key in the BST

4. Exit

Enter your choice: 3

Enter the key to search: 24

Element 24 found in the BST.

Menu:

1. Create a Binary Search Tree by Passing Keys

2. Traverse the BST in Inorder, Preorder, and Postorder

3. Search for a key in the BST

4. Exit

Enter your choice: 4

You might also like