ASSIGNMENT 7
NAME – SUMAN SUBRAJEET
ROLL NO – 05
DIV – C
Write a menu driven program to create a Binary Search Tree and perform following non-recursive
operations on it.
1. Preorder traversal, 2. Display mirror image of a tree without creating new tree, 3. Display height of
a tree.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Node structure for Binary Search Tree
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->left = newNode->right = NULL;
return newNode;
}
// Function to insert a value into the BST
struct Node* insert(struct Node* root, int value) {
if (root == NULL) {
return createNode(value);
}
if (value < root->data) {
root->left = insert(root->left, value);
} else if (value > root->data) {
root->right = insert(root->right, value);
}
return root;
}
// Function for non-recursive preorder traversal
void preorderTraversal(struct Node* root) {
if (root == NULL) {
return;
}
struct Node* stack[100];
int top = -1;
stack[++top] = root;
while (top >= 0) {
struct Node* current = stack[top--];
printf("%d ", current->data);
if (current->right != NULL) {
stack[++top] = current->right;
}
if (current->left != NULL) {
stack[++top] = current->left;
}
}
}
// Function to display the mirror image of a tree
void displayMirror(struct Node* root) {
if (root == NULL) {
return;
}
struct Node* stack[100];
int top = -1;
stack[++top] = root;
while (top >= 0) {
struct Node* current = stack[top--];
struct Node* temp = current->left;
current->left = current->right;
current->right = temp;
if (current->left != NULL) {
stack[++top] = current->left;
}
if (current->right != NULL) {
stack[++top] = current->right;
}
}
}
// Function to calculate the height of a tree
int calculateHeight(struct Node* root) {
if (root == NULL) {
return 0;
}
int height = 0;
struct Node* stack[100];
int top = -1;
stack[++top] = root;
while (top >= 0) {
struct Node* current = stack[top--];
if (current->right != NULL) {
stack[++top] = current->right;
}
if (current->left != NULL) {
stack[++top] = current->left;
}
if (current->left == NULL && current->right == NULL && top + 1 > height) {
height = top + 1;
}
}
return height;
}
// Function to display the menu
void displayMenu() {
printf("\nMenu:\n");
printf("1. Insert into BST\n");
printf("2. Preorder Traversal\n");
printf("3. Display Mirror Image\n");
printf("4. Display Height of Tree\n");
printf("5. Exit\n");
}
int main() {
struct Node* root = NULL;
int choice, value;
do {
displayMenu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("Preorder Traversal: ");
preorderTraversal(root);
printf("\n");
break;
case 3:
printf("Displaying Mirror Image\n");
displayMirror(root);
break;
case 4:
printf("Height of Tree: %d\n", calculateHeight(root));
break;
case 5:
printf("Exiting program\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 5);
return 0;
}