#include <stdio.
h>
#include <stdlib.h>
struct Node {
int data;
struct Node *left;
struct Node *right;
};
struct Node *root = NULL;
int search(struct Node *root, int ele) {
if (root == NULL)
return 0;
if (root->data == ele) {
return 1;
if (ele < root->data) {
return search(root->left, ele);
return search(root->right, ele);
void inorder(struct Node *root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
void insert(int data) {
struct Node *ptr, *nodeptr, *parentptr;
ptr = (struct Node*) malloc(sizeof(struct Node));
ptr->data = data;
ptr->left = NULL;
ptr->right = NULL;
if (root == NULL) {
root = ptr;
} else {
parentptr = NULL;
nodeptr = root;
while (nodeptr != NULL) {
parentptr = nodeptr;
if (data < nodeptr->data) {
nodeptr = nodeptr->left;
} else {
nodeptr = nodeptr->right;
if (data < parentptr->data) {
parentptr->left = ptr;
} else {
parentptr->right = ptr;
struct Node* FindMin(struct Node* root) {
while (root->left != NULL) {
root = root->left;
return root;
}
struct Node* delete(struct Node *root, int data) {
if (root == NULL) {
return root;
} else if (data < root->data) {
root->left = delete(root->left, data);
} else if (data > root->data) {
root->right = delete(root->right, data);
} else {
if (root->left == NULL && root->right == NULL) {
free(root);
root = NULL;
} else if (root->left == NULL) {
struct Node *temp = root;
root = root->right;
free(temp);
} else if (root->right == NULL) {
struct Node *temp = root;
root = root->left;
free(temp);
} else {
struct Node *temp = FindMin(root->right);
root->data = temp->data;
root->right = delete(root->right, temp->data);
return root;
int main() {
int choice, value;
while (1) {
printf("\nMenu:\n");
printf("1. Insert\n");
printf("2. Delete\n");
printf("3. In-order Traversal\n");
printf("4. Search\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
insert(value);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &value);
root = delete(root, value);
break;
case 3:
printf("In-order Traversal: ");
inorder(root);
printf("\n");
break;
case 4:
printf("Enter value to search: ");
scanf("%d", &value);
if (search(root, value)) {
printf("%d found in the tree.\n", value);
} else {
printf("%d not found in the tree.\n", value);
break;
case 5:
exit(0);
default:
printf("Invalid choice. Please try again.\n");
return 0;
Output:-
Menu:
1. Insert
2. Delete
3. In-order Traversal
4. Search
5. Exit
Enter your choice: 1
Enter value to insert: 22
Menu:
1. Insert
2. Delete
3. In-order Traversal
4. Search
5. Exit
Enter your choice: 1
Enter value to insert: 10
Menu:
1. Insert
2. Delete
3. In-order Traversal
4. Search
5. Exit
Enter your choice: 1
Enter value to insert: 55
Menu:
1. Insert
2. Delete
3. In-order Traversal
4. Search
5. Exit
Enter your choice: 3
In-order Traversal: 10 22 55
Menu:
1. Insert
2. Delete
3. In-order Traversal
4. Search
5. Exit
Enter your choice: 2
Enter value to delete: 22
Menu:
1. Insert
2. Delete
3. In-order Traversal
4. Search
5. Exit
Enter your choice: 3
In-order Traversal: 10 55