0% found this document useful (0 votes)
23 views5 pages

Program 10

The document provides a C program that implements a menu-driven Binary Search Tree (BST) for integers. It includes functionalities to create a BST, traverse it in different orders (preorder, inorder, postorder), and search for a specific element. The program allows users to insert integers, view traversals, and search for keys interactively.

Uploaded by

Amogh B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views5 pages

Program 10

The document provides a C program that implements a menu-driven Binary Search Tree (BST) for integers. It includes functionalities to create a BST, traverse it in different orders (preorder, inorder, postorder), and search for a specific element. The program allows users to insert integers, view traversals, and search for keys interactively.

Uploaded by

Amogh B
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the appropriate message
e. Exit

#include <stdio.h>
#include <stdlib.h>

struct node {
int info;
struct node *llink, *rlink;
};

typedef struct node *NODE;

// Allocate node
NODE getnode() {
NODE x = (NODE)malloc(sizeof(struct node));
if (x == NULL) {
printf("Out of memory!\n");
exit(0);
}
return x;
}

// Insert into BST


NODE insert(int item, NODE root) {
NODE temp = getnode();
temp->info = item;
temp->llink = temp->rlink = NULL;

if (root == NULL)
return temp;

NODE cur = root, prev = NULL;

while (cur != NULL) {


prev = cur;
cur = (item < cur->info) ? cur->llink : cur->rlink;
}

if (item < prev->info)


prev->llink = temp;
else
prev->rlink = temp;

return root;
}

// Preorder
void pre(NODE root) {
if (root != NULL) {
printf("%d ", root->info);
pre(root->llink);
pre(root->rlink);
}
}

// Inorder
void in(NODE root) {
if (root != NULL) {
in(root->llink);
printf("%d ", root->info);
in(root->rlink);
}
}

// Postorder
void post(NODE root) {
if (root != NULL) {
post(root->llink);
post(root->rlink);
printf("%d ", root->info);
}
}

// Traversal wrapper
void traversal(NODE root) {
if (root == NULL) {
printf("Empty tree\n");
return;
}

printf("\nPreorder traversal: ");


pre(root);

printf("\nInorder traversal: ");


in(root);

printf("\nPostorder traversal: ");


post(root);

printf("\n");
}

// Search BST
void search(NODE root) {
int item;
printf("Enter element to search: ");
scanf("%d", &item);

NODE cur = root;

while (cur != NULL) {


if (item == cur->info) {
printf("Key found!\n");
return;
}
cur = (item < cur->info) ? cur->llink : cur->rlink;
}

printf("Key not found.\n");


}

// Main
int main() {
int choice, item;
NODE root = NULL;

while (1) {
printf("\n1. Insert\n2. Traverse\n3. Search\n4. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter item: ");
scanf("%d", &item);
root = insert(item, root);
break;

case 2:
traversal(root);
break;

case 3:
search(root);
break;

case 4:
exit(0);

default:
printf("Invalid choice!\n");
}
}
return 0;
}

Output:
1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 6

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 9

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 5

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 2

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 8

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 15

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 24

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 14

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 7
1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 8

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 5

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 1
Enter item: 2

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 2

Preorder traversal: 6 5 2 2 5 9 8 7 8 15 14 24
Inorder traversal: 2 2 5 5 6 7 8 8 9 14 15 24
Postorder traversal: 2 2 5 5 7 8 8 14 24 15 9 6

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 3
Enter element to search: 7
Key found!

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 3
Enter element to search: 78
Key not found.

1. Insert
2. Traverse
3. Search
4. Exit
Enter choice: 4

=== Code Execution Successful ===

You might also like