Write A Program To Implement I) Binary Search Tree
Write A Program To Implement I) Binary Search Tree
#include <stdio.h>
#include <stdlib.h>
// Node with two children: Get the inorder successor (smallest in the right
subtree)
struct Node* temp = findMin(root->right);
root->data = temp->data;
root->right = deleteNode(root->right, temp->data);
}
return root;
}
// Main function
int main() {
struct Node* root = NULL;
int choice, value;
do {
printf("\nBinary Search Tree Operations\n");
printf("1. Insert\n");
printf("2. In-order Traversal\n");
printf("3. Search\n");
printf("4. Delete\n");
printf("5. 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);
break;
case 2:
printf("In-order Traversal: ");
inOrder(root);
printf("\n");
break;
case 3:
printf("Enter value to search: ");
scanf("%d", &value);
if (search(root, value)) {
printf("Value found in the BST.\n");
} else {
printf("Value not found in the BST.\n");
}
break;
case 4:
printf("Enter value to delete: ");
scanf("%d", &value);
root = deleteNode(root, value);
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice! Try again.\n");
}
} while (choice != 5);
return 0;
}