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>
typedef struct node {
int data;
struct node *left, *right;
} NODE;
NODE* createNode(int key) {
NODE* temp = (NODE*)malloc(sizeof(NODE));
temp->data = key;
temp->left = temp->right = NULL;
return temp;
}
NODE* insert(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;
}
void inorder(NODE* root) {
if (root) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
void preorder(NODE* root) {
if (root) {
printf("%d ", root->data);
preorder(root->left);
preorder(root->right);
}
}
void postorder(NODE* root) {
if (root) {
postorder(root->left);
postorder(root->right);
printf("%d ", root->data);
}
}
void search(NODE* root, int key) {
if (root == NULL)
printf("Key %d not found.\n", key);
else if (key == root->data)
printf("Key %d found!\n", key);
else if (key < root->data)
search(root->left, key);
else
search(root->right, key);
}
int main() {
NODE* root = NULL;
int choice, key;
int elements[] = {6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2};
while (1) {
printf("\nMenu:\n1. Create BST\n2. Inorder\n3. Preorder\n4. Postorder\n5. Search\n6. Exit\nEnter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
for (int i = 0; i < 12; i++)
root = insert(root, elements[i]);
printf("BST created.\n");
break;
case 2:
printf("Inorder: ");
inorder(root);
printf("\n");
break;
case 3:
printf("Preorder: ");
preorder(root);
printf("\n");
break;
case 4:
printf("Postorder: ");
postorder(root);
printf("\n");
break;
case 5:
printf("Enter key to search: ");
scanf("%d", &key);
search(root, key);
break;
case 6:
exit(0);
}
}
return 0;
}
Menu:
1. Create BST
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter choice: 1
BST created.
Menu:
1. Create BST
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter choice: 2
Inorder: 2 5 6 7 8 9 14 15 24
Menu:
1. Create BST
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter choice: 5
Enter key to search: 8
Key 8 found!
Menu:
1. Create BST
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter choice: 3
Preorder: 6 5 2 9 8 7 15 14 24
Menu:
1. Create BST
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter choice: 5
Enter key to search: 9
Key 9 found!
Menu:
1. Create BST
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter choice: 4
Postorder: 2 5 7 8 14 24 15 9 6
Menu:
1. Create BST
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter choice: 5
Enter key to search: 24
Key 24 found!
Menu:
1. Create BST
2. Inorder
3. Preorder
4. Postorder
5. Search
6. Exit
Enter choice: 6
Replay !
Share Your Thoughts