-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathbinary_search_tree.c
550 lines (482 loc) · 14.8 KB
/
binary_search_tree.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/* C Program for Non recursive operations in Binary Search Tree */
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
struct node
{
struct node *lchild;
int info;
struct node *rchild;
};
struct node *search_nrec(struct node *root, int skey);
struct node *min_nrec(struct node *root);
struct node *max_nrec(struct node *root);
struct node *insert_nrec(struct node *root, int ikey );
struct node *del_nrec(struct node *root, int dkey);
struct node *case_c(struct node *root, struct node *par,struct node *ptr);
struct node *case_b(struct node *root,struct node *par,struct node *ptr);
struct node *case_a(struct node *root, struct node *par,struct node *ptr );
struct node *del_nrec1(struct node *root, int item);
void nrec_pre(struct node *root);
void nrec_in(struct node *root);
void nrec_post(struct node *root);
void level_trav(struct node *root);
void display(struct node *ptr,int level);
struct node *queue[MAX];
int front=-1,rear=-1;
void insert_queue(struct node *item);
struct node *del_queue();
int queue_empty();
struct node *stack[MAX];
int top=-1;
void push_stack(struct node *item);
struct node *pop_stack();
int stack_empty();
int main( )
{
struct node *root=NULL, *ptr;
int choice,k;
printf("\n");
printf("1.Search\n");
printf("2.Insert\n");
printf("3.Delete\n");
printf("4.Preorder Traversal\n");
printf("5.Inorder Traversal\n");
printf("6.Postorder Traversal\n");
printf("7.Level order traversal\n");
printf("8.Find minimum and maximum\n");
printf("9.Display\n");
printf("10.Height\n");
printf("11.Quit\n");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the key to be searched : ");
scanf("%d",&k);
ptr = search_nrec(root, k);
if(ptr==NULL)
printf("\nKey not present\n");
else
printf("\nKey present\n");
break;
case 2:
printf("\nEnter the key to be inserted : ");
scanf("%d",&k);
root = insert_nrec(root, k);
break;
case 3:
printf("\nEnter the key to be deleted : ");
scanf("%d",&k);
root = del_nrec(root, k);
break;
case 4:
nrec_pre(root);
break;
case 5:
nrec_in(root);
break;
case 6:
nrec_post(root);
break;
case 7:
level_trav(root);
break;
case 8:
ptr = min_nrec(root);
if(ptr!=NULL)
printf("\nMinimum key is %d\n", ptr->info );
ptr = max_nrec(root);
if(ptr!=NULL)
printf("\nMaximum key is %d\n", ptr->info );
break;
case 9:
printf("\n");
display(root,0);
printf("\n");
break;
case 10:
printf("\n");
int h = height(root);
printf("The height of the Binary tree is: %d\n",h);
break;
case 11:
exit(1);
default:
printf("\nWrong choice\n");
}/*End of switch*/
}/*End of while */
return 0;
}/*End of main( )*/
struct node *search_nrec(struct node *ptr, int skey)
{
while(ptr!=NULL)
{
if(skey < ptr->info)
ptr = ptr->lchild; /*Move to left child*/
else if(skey > ptr->info)
ptr = ptr->rchild; /*Move to right child */
else /*skey found*/
return ptr;
}
return NULL;
}/*End of search_nrec()*/
struct node *insert_nrec(struct node *root, int ikey)
{
struct node *tmp,*par,*ptr;
ptr = root;
par = NULL;
while( ptr!=NULL)
{
par = ptr;
if(ikey < ptr->info)
ptr = ptr->lchild;
else if( ikey > ptr->info )
ptr = ptr->rchild;
else
{
printf("\nDuplicate key");
return root;
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=ikey;
tmp->lchild=NULL;
tmp->rchild=NULL;
if(par==NULL)
root=tmp;
else if( ikey < par->info )
par->lchild=tmp;
else
par->rchild=tmp;
return root;
}/*End of insert_nrec( )*/
struct node *del_nrec1(struct node *root, int dkey)
{
struct node *par,*ptr, *child, *succ, *parsucc;
ptr = root;
par = NULL;
while( ptr!=NULL)
{
if( dkey == ptr->info)
break;
par = ptr;
if(dkey < ptr->info)
ptr = ptr->lchild;
else
ptr = ptr->rchild;
}
if(ptr==NULL)
{
printf("\ndkey not present in tree");
return root;
}
/*Case C: 2 children*/
if(ptr->lchild!=NULL && ptr->rchild!=NULL)
{
parsucc = ptr;
succ = ptr->rchild;
while(succ->lchild!=NULL)
{
parsucc = succ;
succ = succ->lchild;
}
ptr->info = succ->info;
ptr = succ;
par = parsucc;
}
/*Case B and Case A : 1 or no child*/
if(ptr->lchild!=NULL) /*node to be deleted has left child */
child=ptr->lchild;
else /*node to be deleted has right child */
child=ptr->rchild;
if(par==NULL ) /*node to be deleted is root node*/
root=child;
else if( ptr==par->lchild)/*node is left child of its parent*/
par->lchild=child;
else /*node is right child of its parent*/
par->rchild=child;
free(ptr);
return root;
}
int height(struct node *root)
{
if(root==NULL)
{
return;
}
int leftHeight = height(root->lchild);
int rightHeight = height(root->rchild);
if(leftHeight>rightHeight)
{
return 1+leftHeight;
}
else
{
return 1+rightHeight;
}
}
struct node *del_nrec(struct node *root, int dkey)
{
struct node *par,*ptr;
ptr = root;
par = NULL;
while(ptr!=NULL)
{
if( dkey == ptr->info)
break;
par = ptr;
if(dkey < ptr->info)
ptr = ptr->lchild;
else
ptr = ptr->rchild;
}
if(ptr==NULL)
printf("dkey not present in tree\n");
else if(ptr->lchild!=NULL && ptr->rchild!=NULL)/*2 children*/
root = case_c(root,par,ptr);
else if(ptr->lchild!=NULL )/*only left child*/
root = case_b(root, par,ptr);
else if(ptr->rchild!=NULL)/*only right child*/
root = case_b(root, par,ptr);
else /*no child*/
root = case_a(root,par,ptr);
return root;
}/*End of del_nrec( )*/
struct node *case_a(struct node *root, struct node *par,struct node *ptr )
{
if(par==NULL) /*root node to be deleted*/
root=NULL;
else if(ptr==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL;
free(ptr);
return root;
}/*End of case_a( )*/
struct node *case_b(struct node *root,struct node *par,struct node *ptr)
{
struct node *child;
/*Initialize child*/
if(ptr->lchild!=NULL) /*node to be deleted has left child */
child=ptr->lchild;
else /*node to be deleted has right child */
child=ptr->rchild;
if(par==NULL ) /*node to be deleted is root node*/
root=child;
else if( ptr==par->lchild) /*node is left child of its parent*/
par->lchild=child;
else /*node is right child of its parent*/
par->rchild=child;
free(ptr);
return root;
}/*End of case_b( )*/
struct node *case_c(struct node *root, struct node *par,struct node *ptr)
{
struct node *succ,*parsucc;
/*Find inorder successor and its parent*/
parsucc = ptr;
succ = ptr->rchild;
while(succ->lchild!=NULL)
{
parsucc = succ;
succ = succ->lchild;
}
ptr->info = succ->info;
if(succ->lchild==NULL && succ->rchild==NULL)
root = case_a(root, parsucc,succ);
else
root = case_b(root, parsucc,succ);
return root;
}/*End of case_c( )*/
struct node *min_nrec(struct node *ptr)
{
if(ptr!=NULL)
while(ptr->lchild!=NULL)
ptr=ptr->lchild;
return ptr;
}/*End of min_nrec()*/
struct node *max_nrec(struct node *ptr)
{
if(ptr!=NULL)
while(ptr->rchild!=NULL)
ptr=ptr->rchild;
return ptr;
}/*End of max_nrec()*/
void nrec_pre(struct node *root)
{
struct node *ptr = root;
if( ptr==NULL )
{
printf("Tree is empty\n");
return;
}
push_stack(ptr);
while( !stack_empty() )
{
ptr = pop_stack();
printf("%d ",ptr->info);
if(ptr->rchild!=NULL)
push_stack(ptr->rchild);
if(ptr->lchild!=NULL)
push_stack(ptr->lchild);
}
printf("\n");
}/*End of nrec_pre*/
void nrec_in(struct node *root)
{
struct node *ptr=root;
if( ptr==NULL )
{
printf("Tree is empty\n");
return;
}
while(1)
{
while(ptr->lchild!=NULL )
{
push_stack(ptr);
ptr = ptr->lchild;
}
while( ptr->rchild==NULL )
{
printf("%d ",ptr->info);
if(stack_empty())
return;
ptr = pop_stack();
}
printf("%d ",ptr->info);
ptr = ptr->rchild;
}
printf("\n");
}/*End of nrec_in( )*/
void nrec_post(struct node *root)
{
struct node *ptr = root;
struct node *q;
if( ptr==NULL )
{
printf("Tree is empty\n");
return;
}
q = root;
while(1)
{
while(ptr->lchild!=NULL)
{
push_stack(ptr);
ptr=ptr->lchild;
}
while( ptr->rchild==NULL || ptr->rchild==q )
{
printf("%d ",ptr->info);
q = ptr;
if( stack_empty() )
return;
ptr = pop_stack();
}
push_stack(ptr);
ptr = ptr->rchild;
}
printf("\n");
}/*End of nrec_post( )*/
void level_trav(struct node *root)
{
struct node *ptr = root;
if( ptr==NULL )
{
printf("Tree is empty\n");
return;
}
insert_queue(ptr);
while( !queue_empty() ) /*Loop until queue is not empty*/
{
ptr=del_queue();
printf("%d ",ptr->info);
if(ptr->lchild!=NULL)
insert_queue(ptr->lchild);
if(ptr->rchild!=NULL)
insert_queue(ptr->rchild);
}
printf("\n");
}/*End of level_trav( )*/
/*Functions for implementation of queue*/
void insert_queue(struct node *item)
{
if(rear==MAX-1)
{
printf("Queue Overflow\n");
return;
}
if(front==-1) /*If queue is initially empty*/
front=0;
rear=rear+1;
queue[rear]=item ;
}/*End of insert()*/
struct node *del_queue()
{
struct node *item;
if(front==-1 || front==rear+1)
{
printf("Queue Underflow\n");
return 0;
}
item=queue[front];
front=front+1;
return item;
}/*End of del_queue()*/
int queue_empty()
{
if(front==-1 || front==rear+1)
return 1;
else
return 0;
}
/*Functions for implementation of stack*/
void push_stack(struct node *item)
{
if(top==(MAX-1))
{
printf("Stack Overflow\n");
return;
}
top=top+1;
stack[top]=item;
}/*End of push_stack()*/
struct node *pop_stack()
{
struct node *item;
if(top==-1)
{
printf("Stack Underflow....\n");
exit(1);
}
item=stack[top];
top=top-1;
return item;
}/*End of pop_stack()*/
int stack_empty()
{
if(top==-1)
return 1;
else
return 0;
} /*End of stack_empty*/
void display(struct node *ptr,int level)
{
int i;
if(ptr == NULL )/*Base Case*/
return;
else
{
display(ptr->rchild, level+1);
printf("\n");
for (i=0; i<level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}
}/*End of display()*/