-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarysearch.c
More file actions
148 lines (125 loc) · 3.78 KB
/
binarysearch.c
File metadata and controls
148 lines (125 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <stdlib.h>
// Structure of a tree node
struct node {
int data;
struct node *left;
struct node *right;
};
// Function to create a new node
struct node* newNode(int value) {
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = value;
temp->left = temp->right = NULL;
return temp;
}
// Function to insert a node in BST
struct node* insert(struct node* root, int value) {
if (root == NULL)
return newNode(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 to search a node in BST
struct node* search(struct node* root, int key) {
if (root == NULL || root->data == key)
return root;
if (key < root->data)
return search(root->left, key);
else
return search(root->right, key);
}
// Function to find the minimum value node in a tree
struct node* findMin(struct node* root) {
while (root && root->left != NULL)
root = root->left;
return root;
}
// Function to delete a node from BST
struct node* deleteNode(struct node* root, int key) {
if (root == NULL)
return root;
if (key < root->data)
root->left = deleteNode(root->left, key);
else if (key > root->data)
root->right = deleteNode(root->right, key);
else {
// Node with only one child or no child
if (root->left == NULL) {
struct node* temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct node* temp = root->left;
free(root);
return temp;
}
// Node with two children:
// Get inorder successor (smallest in the right subtree)
struct node* temp = findMin(root->right);
// Copy the inorder successor's data to this node
root->data = temp->data;
// Delete the inorder successor
root->right = deleteNode(root->right, temp->data);
}
return root;
}
// Function for inorder traversal (sorted order)
void inorder(struct node* root) {
if (root != NULL) {
inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}
}
// Main program
int main() {
struct node* root = NULL;
int choice, value;
while (1) {
printf("\n---- Binary Search Tree Operations ----\n");
printf("1. Insert\n");
printf("2. Search\n");
printf("3. Delete\n");
printf("4. Display (Inorder Traversal)\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("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 3:
printf("Enter value to delete: ");
scanf("%d", &value);
root = deleteNode(root, value);
printf("Node deleted (if it existed).\n");
break;
case 4:
printf("Inorder Traversal: ");
inorder(root);
printf("\n");
break;
case 5:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice! Try again.\n");
}
}
return 0;
}