Advanced Coding Assignment -2(0404)
Advanced Coding Assignment -2(0404)
K CHANDRASEKHAR
VU21CSEN0100404
2. Write a program to implement binary search tree using
linked list.
#CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* left;
struct Node* right;
};
if (root == NULL) {
return createNode(data);
}
return root;
}
int main() {
struct Node* root = NULL;
int choice, value, key;
while (1) {
printf("\n1. Insert a node\n2. Search a node\n3. Display inorder\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
printf("Node inserted.\n");
break;
case 2:
printf("Enter value to search: ");
scanf("%d", &key);
if (search(root, key)) {
printf("Node found.\n");
} else {
printf("Node not found.\n");
}
break;
case 3:
printf("Inorder traversal of the binary search tree: ");
inorder(root);
printf("NULL\n");
break;
case 4:
freeTree(root);
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 0;
}
OUTPUT: