-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst.c
490 lines (442 loc) · 12.3 KB
/
bst.c
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
/*
NAME - RAJENDRA SINGH
ROLL NO.- 111601017
PROGRAM: A menu based program to implement a (unique key) binary search tree
*/
//----------------------------------------header-------------------------------------------------------//
#include<stdio.h>
#include<stdlib.h>///for malloc
#include<string.h>
#include <limits.h>//for INT_MAX
//------------------------------------all structures and there creating function-------------------------//
typedef struct bstnode// a structure to represent a bstnode
{
int key;
struct bstnode *lptr, *rptr, *pptr;
}bstnode;
bstnode* newbstnode(int key)
{
bstnode *p;
p = (bstnode *)malloc(sizeof(bstnode));
p->key = key;
p->lptr = NULL;
p->rptr = NULL;
p->pptr = NULL;
return(p);
}
typedef struct BST// a structure to represent a BST
{
int size;
struct bstnode *root; //root
}BST;
BST* newBST()
{
BST *T;
T = (BST *)malloc(sizeof(BST));
T->root = NULL;
T->size = 0;
return(T);
}
//--------------------------------------------------FUNCTION DECLARATION---------------------------------------//
void InsertKey(BST*, int);
bstnode* SearchKey(BST*, int);
bstnode* FindMinKey(bstnode*);
bstnode* FindMaxKey(bstnode*);
bstnode* DeleteKey(BST*, int);
void Deletenode(BST *, bstnode *);
void InOrderTravelsal(bstnode*);
void PostOrderTravelsal(bstnode*);
bstnode* FindInOrderPredecessorKey(BST*, int);
bstnode* FindInOrderPredecessorNode(BST*, bstnode* );
void PrintTree(bstnode*, int);
//---------------------------------------------------main function--------------------------------------------//
int main()
{
int choice,key;
BST *T= newBST();
while(1)//MENU BASED CALL
{
printf("\n\n\nChoose from options below:\n");
printf("[1]. Insert a key\n");
printf("[2]. Search for a key\n");
printf("[3]. find minimum a key\n");
printf("[4]. delete a key\n");
printf("[5]. InOrderTravelsal\n");
printf("[6]. PostOrderTravelsal\n");
printf("[7]. FindInOrderPredecessor\n");
printf("[8]. PrintTree\n");
printf("\nChoice :");
scanf("%d", &choice);
switch (choice)
{
case 1: printf("\nEnter Key to Insert:");
scanf("%d", &key);
InsertKey(T, key);
break;
case 2: printf("\nEnter Key to search:");
scanf("%d", &key);
bstnode *searchednode = SearchKey(T, key);
if(searchednode != NULL)
printf("\n Searched Key Present!!\n");
break;
case 3: printf("\n");
bstnode *minKeynode = FindMinKey(T->root);
if(minKeynode != NULL)
printf("\n min Key is : %d\n", minKeynode->key);
break;
case 4: printf("\nEnter Key to Delete:");
scanf("%d", &key);
bstnode *deletednode = DeleteKey(T, key);
if(deletednode != NULL)
{
printf("\n Deleted Key is : %d\n", deletednode->key);
}
break;
case 5: InOrderTravelsal(T->root);
break;
case 6: PostOrderTravelsal(T->root);
break;
case 7: printf("Enter Key whose predecessor needed:");
scanf("%d", &key);
bstnode *pred = FindInOrderPredecessorKey(T, key);
if(pred != NULL)
printf("\n predecessor of %d in InOrder is : %d\n", key, pred->key);
else
printf("Hence predecessor!\n");
break;
case 8: printf("\nPRINTING BST : \n\n\n");
PrintTree(T->root, 0);
break;
default: printf("Invalid choice! Please enter valid choice\n");
}
}
}
//-------------------------------------INSERT FUNCTION------------------------------------------//
void InsertKey(BST *T, int key)
{
bstnode *p = newbstnode(key);
// printf("flag1\n");
bstnode *curr = T->root;
int flag = 0;
if(curr == NULL)// if there no bstnode in T i.e BST is empty
{
T->root = p;
T->root->pptr = NULL;
T->size++;
}
else
{
bstnode *parent = curr->pptr;
while(curr != NULL)//travelling to exact location of key
{
if(curr->key < key)//travel in right sub tree of curr
{
parent = curr;
curr = curr->rptr;
flag = 0;
}
else if(curr->key > key)//travel in left sub tree of curr
{
parent = curr;
curr = curr->lptr;
flag = 1;
}
else//i.e when duplicate key present
{
printf("Entered a duplicate key!!!, Please enter a valid key!\n");
return;
}
}
// if(parent->key == key)//avoiding duplicate
// {
// printf("Entered a duplicate key!!!, Please enter a valid key!\n");
// return;
// }
if(flag == 0 )//key is right child of parent
{
printf("Right child of parent %d\n", parent->key);
parent->rptr = p;
p->pptr = parent;
}
else//key is left child of parent
{
printf("Left child of parent %d\n", parent->key);
parent->lptr = p;
p->pptr = parent;
}
T->size++;
}
}
//-------------------------------------SEARCH FUNCTION------------------------------------------//
bstnode* SearchKey(BST *T, int key)
{
bstnode *curr = T->root;
if(curr == NULL)// if there no bstnode in T i.e BST is empty
{
printf("\nBST is empty\n");
return(curr);
}
else
{
while(curr != NULL)
{
if(curr->key == key)//key is Present at curr
{
//printf("\n Present!!!\n", );
return(curr);
}
else if(curr->key < key)//travel in right sub tree of curr
{
curr = curr->rptr;
}
else//travel in left sub tree of curr
{
curr = curr->lptr;
}
}
if(curr == NULL)//key is Absent and BST is not empty
{
printf("\n Key= %d, is Absent!!!! \n", key);
return(curr);
}
}
}
//-------------------------------------FIND MIN KEY FUNCTION------------------------------------------//
bstnode* FindMinKey(bstnode *root)
{
bstnode *curr = root;
bstnode *parent = curr->pptr;
if(curr == NULL)// if there no bstnode in T i.e BST is empty
{
printf("\nBST(of given root) is empty\n");
return(curr);
}
else
{
while(curr != NULL)
{
parent = curr;
curr = curr->lptr;
}
return(parent);
}
}
bstnode* FindMaxKey(bstnode *root)
{
bstnode *curr = root;
bstnode *parent = curr->pptr;
if(curr == NULL)// if there no bstnode in T i.e BST is empty
{
printf("\nBST(of given root) is empty\n");
return(curr);
}
else
{
while(curr != NULL)
{
parent = curr;
curr = curr->rptr;
}
return(parent);
}
}
//-------------------------------------DELETE BY KEY FUNCTION------------------------------------------//
bstnode* DeleteKey(BST *T, int key)
{
bstnode *curr = SearchKey(T, key);
Deletenode(T,curr);
return(curr);
}
//-------------------------------------DELETE BY NODE ADDRESS FUNCTION------------------------------------------//
void Deletenode(BST *T, bstnode *y)
{
bstnode *curr = y;
if(curr == NULL)// if there no bstnode in T i.e BST is empty
{
if(T->root == NULL)// if y is root node
printf("BST is empty\n");
else
printf("\n No node at given node address(given node is not root)\n");
}
// else if(curr == T->root)// if y is root node
// {
// T->root = NULL;
// }
else//curr is position of bstnode to delete
{
bstnode *parent = curr->pptr;
if(curr->lptr == NULL && curr->rptr == NULL)//if curr have no child
{
if(parent == NULL)//curr is root node
{
T->root = NULL;
}
else//curr is not root node
{
if(parent->lptr = curr)//curr is parent's left child
{
parent->lptr = NULL;
}
else//curr is parent's right child
{
parent->rptr = NULL;
}
}
}
else if(curr->lptr != NULL && curr->rptr == NULL)//if curr have only left child
{
if(parent == NULL)//curr is root node
{
T->root = curr->lptr;
curr->lptr->pptr = NULL;//since it will become root node so its parent = NULL
//return(curr);
}
else//curr is not root node
{
if(parent->lptr == curr)//curr is parent's left child
{
parent->lptr = curr->lptr;
curr->lptr->pptr = parent;
//return(curr);
}
else//curr is parent's right child
{
parent->rptr = curr->lptr;
curr->lptr->pptr = parent;
//return(curr);
}
}
}
else if(curr->lptr == NULL && curr->rptr != NULL)//if curr have only right child
{
if(parent == NULL)//curr is root node
{
T->root = curr->rptr;
curr->rptr->pptr = NULL;//since it will become root node so its parent = NULL
//return(curr);
}
else//curr is not root node
{
if(parent->lptr == curr)//curr is parent's left child
{
parent->lptr = curr->rptr;
curr->rptr->pptr = parent;
//return(curr);
}
else//curr is parent's right child
{
parent->rptr = curr->rptr;
curr->rptr->pptr = parent;
//return(curr);
}
}
}
else//if curr have both child
{
// if(parent == NULL)//curr is root
// {
// bstnode *pred = FindInOrderPredecessorNode(T, curr);
// if(pred != NULL)
// {
// Deletenode(T,pred);
// curr->key = pred->key;
// curr->pptr = NULL; //since curr is root
// }
// }
//{
bstnode *pred = FindInOrderPredecessorNode(T, curr);
if(pred != NULL)
{
Deletenode(T,pred);
curr->key = pred->key;
}
//}
}
T->size--;
}
}
//-------------------------------------RECURSIVE INORDER TRAVELSAL FUNCTION------------------------------------------//
void InOrderTravelsal(bstnode *r)
{
if(r == NULL)
return;
InOrderTravelsal(r->lptr);
printf("%d, ", r->key);
InOrderTravelsal(r->rptr);
}
//-------------------------------------RECURSIVE POSTORDER TRAVELSAL FUNCTION------------------------------------------//
void PostOrderTravelsal(bstnode *r)
{
if(r == NULL)
return;
InOrderTravelsal(r->lptr);
InOrderTravelsal(r->rptr);
printf("%d, ", r->key);
}
//-------------------------------------FIND PREDECESSOR IN INORDER FUNCTION------------------------------------------//
bstnode* FindInOrderPredecessorKey(BST *T, int key)
{
bstnode *x = SearchKey(T, key);
bstnode *pred = FindInOrderPredecessorNode(T,x);
return(pred);
}
bstnode* FindInOrderPredecessorNode(BST *T, bstnode* x)
{
//bstnode *x = SearchKey(T, key);
if(x == NULL)//return NULL if x is NULL
{
return(NULL);
}
else
{
if(x->lptr != NULL)
{
bstnode *max = FindMaxKey(x->lptr);//max element in left sub tree of given key
return(max);
}
else
{
if(x == FindMinKey(T->root))//if x is minimum, then it have no predecessor
{
printf("Dont have predecessor, since given node is min!!\n");
return(NULL);
}
bstnode * curr = x;
bstnode *p = x->pptr;
while (p != NULL && curr == p->lptr)//move up in rightward or until curr is leftchild of parent(p) && p is not root
{
curr = p;//updating curr
p = p->pptr;//updating parent
}
return(p);
}
}
}
//-------------------------------------PRINT BST IN TREE FUNCTION------------------------------------------//
void PrintTree(bstnode *currentNode, int ofset)
{
//printf("flag2\n");
if(currentNode == NULL)
{
printf("\nBST is empty\n");
return;
}
else
{
//printf("flag,((%d))\n", currentNode->key);
//printf("\nPRINTING BST : \n\n\n");
if(currentNode->rptr != NULL)//checking if right child is there or not
{
PrintTree(currentNode->rptr,ofset+2);
}
for(int i=0; i < ofset; i++)
{
printf(" ");//ofset for non child node
}
printf("%d\n",currentNode->key);//non child node printed
if(currentNode->lptr != NULL)//checking if left child is there or not
{
PrintTree(currentNode->lptr,ofset+2);
}
}
}