-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Search_Tree_operations.c
More file actions
331 lines (305 loc) · 8.69 KB
/
Copy pathBinary_Search_Tree_operations.c
File metadata and controls
331 lines (305 loc) · 8.69 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<limits.h>
struct BST
{
int data;
struct BST* left;
struct BST* right;
};
struct queue
{
struct BST *pointer;
struct queue *next;
};
struct queue *front = NULL, *rear = NULL;
struct queue* createqueuenode()
{
struct queue* temp = (struct queue*)malloc(sizeof(struct queue));
temp -> pointer = NULL;
temp -> next = NULL;
return temp;
}
void push(struct BST* ptr)
{
if(rear == NULL)
{
rear = createqueuenode();
rear -> pointer = ptr;
front = rear;
return;
}
rear -> next = createqueuenode();
rear = rear -> next;
rear -> pointer = ptr;
}
struct BST* pop()
{
if(front == NULL) return NULL;
struct queue* temp = front;
front = front -> next;
if(front == NULL) rear = front;
return temp -> pointer;
}
int isqueueempty()
{
if(front == NULL && rear == NULL) return 1;
else return 0;
}
struct BST* createnode()
{
struct BST* temp = (struct BST*)malloc(sizeof(struct BST));
temp -> data = 0;
temp -> left = NULL;
temp -> right = NULL;
return temp;
}
struct BST* insert(struct BST* root, int value)
{
if(root == NULL)
{
root = createnode();
root -> data = value;
}
else if(value <= root -> data) root -> left = insert(root -> left, value);
else root -> right = insert(root -> right, value);
return root;
}
struct BST* find_min(struct BST* root)
{
if(root -> left == NULL) return root;
return find_min(root -> left);
}
struct BST* find_max(struct BST* root)
{
if(root -> right == NULL) return root;
return find_max(root -> right);
}
int find_height(struct BST* root)
{
if(root == NULL) return -1;
int left = find_height(root -> left);
int right = find_height(root -> right);
return (left > right ? left + 1 : right + 1);
}
void pre_order_print(struct BST* root)
{
if(root == NULL) return;
printf("%d\t", root -> data);
pre_order_print(root -> left);
pre_order_print(root -> right);
}
void in_order_print(struct BST* root)
{
if(root == NULL) return;
in_order_print(root -> left);
printf("%d\t", root -> data);
in_order_print(root -> right);
}
void post_order_print(struct BST* root)
{
if(root == NULL) return;
post_order_print(root -> left);
post_order_print
(root -> right);
printf("%d\t", root -> data);
}
void level_order_print(struct BST* root)
{
struct BST* temp = (struct BST*)malloc(sizeof(struct BST));
push(root);
while(!isqueueempty())
{
temp = pop();
printf("%d\t", temp -> data);
if(temp -> left != NULL) push(temp -> left);
if(temp -> right != NULL) push(temp -> right);
}
free(temp);
}
bool isBST(struct BST* root, int min, int max)
{
if(root == NULL) return true;
if(root -> data < min || root -> data > max) return false;
return(isBST(root -> left, min, root -> data) && isBST(root -> right, root -> data, max));
}
struct BST* find_node(struct BST* root, int value_to_find)
{
if(root == NULL) return root;
if(value_to_find < root -> data) return(find_node(root -> left, value_to_find));
else if(value_to_find > root -> data) return(find_node(root -> right, value_to_find));
else return root;
}
struct BST* delete_node(struct BST* root, int value_to_delete)
{
if(root == NULL) return root;
// if(value_to_delete < root -> data) root -> left = delete_node(root -> left, value_to_delete);
// else if(value_to_delete > root -> data) root -> right = delete_node(root -> right, value_to_delete);
if(find_node(root,value_to_delete) != NULL)
{
// Handing case where node to be deleted is a Leaf node
if(root -> left == NULL && root -> right == NULL)
{
free(root);
root = NULL;
}
// Handing case where node to be deleted only has RIGHT node
else if(root -> left == NULL)
{
struct BST* temp = root;
root = root -> right;
free(temp);
}
// Handing case where node to be deleted only has LEFT node
else if(root -> right == NULL)
{
struct BST* temp = root;
root = root -> left;
free(temp);
}
// Handing case where node to be deleted has both left and right nodes
else
{
struct BST* temp = find_max(root -> left);
root -> data = temp -> data;
root -> left = delete_node(root -> left, temp -> data);
}
}
return root;
}
struct BST* in_order_successor(struct BST* root, int value)
{
struct BST* current = find_node(root, value);
if(current == NULL) return NULL;
//If current node has right subtree, then the left-most node in the right subtree is the successor
if(current -> right != NULL)
return find_min(current -> right);
else
{
struct BST* successor = NULL;
struct BST* ancestor = root;
while(ancestor != current)
{
if(current -> data < ancestor -> data)
{
successor = ancestor;
ancestor = ancestor -> left;
}
else ancestor = ancestor -> right;
}
return successor;
}
}
struct BST* in_order_predecessor(struct BST* root, int value)
{
struct BST* current = find_node(root, value);
if(current == NULL) return NULL;
//If current node has left subtree, then the right-most node in the left subtree is the predecessor
if(current -> left != NULL)
return find_max(current -> left);
else
{
struct BST* predecessor = NULL;
struct BST* ancestor = root;
while(ancestor != current)
{
if(current -> data > ancestor -> data)
{
predecessor = ancestor;
ancestor = ancestor -> right;
}
else ancestor = ancestor -> left;
}
return predecessor;
}
}
void main()
{
int option = 0, value = 0, find_successor = 0, find_predecessor = 0;
struct BST *root = NULL;
while(option != 13)
{
printf("\nSelect an option:\n1. Insert into the Binary Search Tree\n2. Find Minimum Value\n3. Find Maximum Value\n");
printf("4. Find Height of BST\n5. Print Binary Search Tree - Pre-Order\n6. Print Binary Search Tree - In-Order\n");
printf("7. Print Binary Search Tree - Post-Order\n8. Print Binary Search Tree - Level-Order\n9. Delete a node\n");
printf("10. Check if BST is valid\n11. In-order Successor\n12. In-order Predecessor\n13. Exit\n");
scanf("%d",&option);
switch(option)
{
case 1:
printf("\nEnter value to insert:");
scanf("%d", &value);
root = insert(root,value);
break;
case 2:
if(root == NULL)
{
printf("\nBST is empty!!\n");
}
else printf("%s\t%d\n","\nMinimum value is:",find_min(root) -> data);
break;
case 3:
if(root == NULL)
{
printf("\nBST is empty!!\n");
}
else printf("%s\t%d\n","\nMaximum value is:",find_max(root) -> data);
break;
case 4:
if(root == NULL)
{
printf("\nBST is empty!!\n");
}
else printf("%s\t%d\n","\nHeight of BST is:",find_height(root));
break;
case 5:
pre_order_print(root);
break;
case 6:
in_order_print(root);
break;
case 7:
post_order_print(root);
break;
case 8:
level_order_print(root);
break;
case 9:
if(root == NULL)
{
printf("\nBST is empty!!\n");
}
else
{
int delete_value = 0;
printf("\nEnter value to delete:\n");
scanf("%d",&delete_value);
root = delete_node(root,delete_value);
}
break;
case 10:
if(isBST(root, INT_MIN, INT_MAX)) printf("\nBST is valid\n");
else printf("\nBST is invalid\n");
break;
case 11:
printf("\nEnter value to find successor for :\n");
scanf("%d",&find_successor);
struct BST *successor = in_order_successor(root, find_successor);
successor == NULL ? printf("\nSuccessor not found\n") : printf("%s\t%d\n","\nSuccessor is :",successor -> data);
break;
case 12:
printf("\nEnter value to find predecessor for :\n");
scanf("%d",&find_predecessor);
struct BST *predecessor = in_order_predecessor(root, find_predecessor);
predecessor == NULL ? printf("\nPredecessor not found\n") : printf("%s\t%d\n","\nPredecessor is :",predecessor -> data);
break;
case 13:
printf("Exiting!!");
exit(0);
default:
printf("Incorrect option");
break;
}
}
}