-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_tree_functions.c
616 lines (522 loc) · 14.4 KB
/
binary_tree_functions.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
// #include<stdio.h>
// #include<stdlib.h>
// #include <stdarg.h>
#include "python_functions.c" // FROM "https://github.com/HappyBravo/python_functions_in_C"
struct btElement { // DEFINING ELEMENT FOR BINARY TREE
int data;
struct btElement *left;
struct btElement *right;
};
typedef struct btElement btNode;
//btNode *root = NULL; // COMMENTED THIS OUT AS IT CAN CONFLICT WITH OTHER PROGRAMS AND FUNCTIONS
// FINDING THE HEIGHT OF THE NODE
int btheight(btNode *root){
int left = 1;
int right = 1;
if(!(root)) return 0;
if (root->left){
left = 1 + btheight(root->left);
}
if (root->right){
right = 1 + btheight(root->right);
}
return (right>left) ? right : left;
}
// CHECKING IF AN ELEMENT IS AN INTERNAL NODE OR NOT
int isInternal(btNode *root, int d){
if (root){
if (d == root->data){
if ((root->right) || (root->left)){
return 1;
}
}
return (isInternal(root->left, d)+isInternal(root->right, d));
}
return 0;
}
// CHECKING IF AN ELEMENT IS AN EXTERNAL NODE OR NOT
int isExternal(btNode *root, int d){
if (root){
if (d == root->data){
if (!((root->right) || (root->left))){
return 1;
}
}
return (isExternal(root->left, d)+isExternal(root->right, d));
}
return 0;
}
btNode *searchElement(btNode *root, int d){ // better than isPresent()
if (!(root)) return root;
if (root->data == d) return root;
btNode *got = NULL;
got = searchElement(root->left, d);
if (got) return got;
got = searchElement(root->right, d);
if (got) return got;
}
int isPresent(btNode *root, int dat){
// if (isInternal(root, dat)+isExternal(root, dat)) return 1;
if(searchElement(root, dat)) return 1;
return 0;
}
// FUNCTION TO FIND THE ADDRESS OF MAXIMUM ELEMENT IN THE TREE/BRANCH
btNode *bt_findMax_main(btNode *root){
btNode *_left = NULL;
btNode *Max = root;
btNode *_right = NULL;
if (root) {
// Max = root;
_left = bt_findMax_main(root->left);
if (_left){
if(Max->data < _left->data) Max = _left;
}
_right = bt_findMax_main(root->right);
if (_right){
if (Max->data < _right->data) Max = _right;
}
}
return Max;
}
btNode *bt_findMax(btNode *root) {
if (!(root)) return root;
return bt_findMax_main(root);
}
// FUNCTION TO FIND THE ADDRESS OF MINIMUM ELEMENT IN THE TREE/BRANCH
btNode *bt_findMin_main(btNode *root){
btNode *_left = NULL;
btNode *Min = root;
btNode *_right = NULL;
if (root) {
// Max = root;
_left = bt_findMin_main(root->left);
if (_left){
if(Min->data > _left->data) Min = _left;
}
_right = bt_findMin_main(root->right);
if (_right){
if (Min->data > _right->data) Min = _right;
}
}
return Min;
}
btNode *bt_findMin(btNode *root) {
if (!(root)) return root;
return bt_findMin_main(root);
}
/*
// FUNCTION TO FIND THE MINIMUM ELEMENT IN THE BRANCH/TREE
int bt_findMin_main(btNode *root){
int left;
int Min;
int right;
if (root) {
Min = root->data;
left = bt_findMin_main(root->left);
right = bt_findMin_main(root->right);
if(Min >= left) Min = left;
else if (Min >= right) Min = right;
}
return Min;
}
int bt_findMin(btNode *root) {
if (!(root)) return -1;
return bt_findMin_main(root);
}
*/
int btNode_level_main(btNode *root, int dat){
int level = 0;
// int d = 0;
if (!(root)) return 0;
if (root->data == dat){
return 1;
}
if (root->left){
level = btNode_level_main(root->left, dat);
if(level) return level + 1;
}
if (root->right){
level = btNode_level_main(root->right, dat);
if(level) return level + 1;
}
return level;
}
int btNode_level(btNode *root, int dat){
if (isPresent(root, dat)){
return btNode_level_main(root, dat)-1;
}
return -1;
}
int bt_parent_main(btNode *root, int d){
if (!(root)) return 0;
int p = 0;
if (root->left){
if (root->left->data == d) return root->data;
p = bt_parent_main(root->left, d);
if (p) return p;
}
if (root->right){
if(root->right->data == d) return root->data;
p = bt_parent_main(root->right, d);
if (p) return p;
}
}
int bt_parent(btNode *root, int d){
if (isPresent(root, d)) return bt_parent_main(root, d);
return -1;
}
// int h;
// int di = 100;
// printf("\nPrinting Ancestors");
// h = btNode_depth(root, di);
// node *anc = create_list(0);
// while (h-- && h>0){
// di = parent(root, di);
// if (python_count(anc, di) == 0){
// anc = python_append(anc, di);
// }
// }
// printList(anc);
int isEmpty(btNode *root)
{
if(!(root))
return 1;
return 0;
}
int isRoot(btNode *root){
if (root){
if ((root->right == NULL) && (root->left == NULL))
return 1;
return 0;
}
}
node *bt_children_main(btNode *root, int d){
node *listHead = create_list(0);
if (root){
if (d == root->data){
if (root->left != NULL){
listHead = python_append(listHead, root->left->data);
}
if (root->right != NULL){
listHead = python_append(listHead, root->right->data);
}
return listHead;
}
listHead = bt_children_main(root->left, d);
if (listHead) return listHead;
listHead = bt_children_main(root->right, d);
}
return listHead;
}
node *bt_children(btNode *root, int d){
if (isPresent(root, d)) return bt_children_main(root, d);
return create_list(0);
}
btNode *makeNode(int d) {
btNode *new_node;
new_node = malloc(sizeof(btNode));
new_node->data = d;
new_node->left = new_node->right = NULL;
return new_node;
}
// %%%%%%%%%% NON-RECURSIVE TRANVERSAL FUNCTIONS %%%%%%%%%%%%%
void bt_inorder_nonrec(btNode *root){
// Morris Inorder Traversal
btNode *temp = root;
btNode *p ;
while(temp){
if (!(temp->left)){
printf("%d\t", temp->data);
temp = temp->right;
}
else{
p = temp->left;
while( (p->right) && (p->right != temp)){
p = p->right;
}
if (!(p->right)){
p->right = temp;
temp = temp->left;
}
else{
p->right = NULL;
printf("%d\t", temp->data);
temp = temp->right;
}
}
}
}
void bt_preorder_nonrec(btNode *root){
// Morris Preorder Traversal
btNode *temp = root;
btNode *p;
while(temp){
if (!(temp->left)){
printf("%d\t", temp->data);
temp = temp->right;
}
else {
p = temp->left;
while( (p->right) && (p->right != temp) ){
p = p->right;
}
if(p->right == temp){
p->right = NULL;
temp = temp->right;
}
else{
printf("%d\t", temp->data);
p->right = temp;
temp = temp->left;
}
}
}
}
node *bt_postorder_nonrec_main(btNode *root){
// similar to Morris preorder Traversal, but swapped left and right
btNode *temp = root;
node *post = create_list(0);
btNode *p;
while(temp){
if(!(temp->right)){
// printf("%d\t", temp->data);
post = python_append(post, temp->data);
temp = temp->left;
}
else{
p = temp->right;
while ((p->left) && (p->left != temp)){
p = p->left;
}
if(!(p->left)){
// printf("%d\t", temp->data);
post = python_append(post, temp->data);
p->left = temp;
temp = temp->right;
}
else{
p->left = NULL;
temp = temp->left;
}
}
}
return post;
// printf("");
}
void bt_postorder_nonrec(btNode *root){
node *temp = NULL;
temp = bt_postorder_nonrec_main(root);
temp = python_reverse(temp);
while(temp){
printf("%d\t", temp->data);
temp = temp->next;
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%% RECURSIVE TRANVERSAL FUNCTIONS %%%%%%%%%%%%%
void bt_inorder(btNode *root) {
// LEFT - DATA - RIGHT
if (root) {
bt_inorder(root->left);
printf("%d\t", root->data);
bt_inorder(root->right);
}
}
void bt_preorder(btNode *root) {
// DATA - LEFT - RIGHT
if (root) {
printf("%d\t", root->data);
bt_preorder(root->left);
bt_preorder(root->right);
}
}
void bt_postorder(btNode *root){
// LEFT - RIGHT - DATA
if (root) {
bt_postorder(root->left);
bt_postorder(root->right);
printf("%d\t", root->data);
}
}
void bt_levelorder_main(btNode *root, int l){
if(!(root)) return;
if (l == 1) printf("%d\t", root->data);
else if (l>1){
bt_levelorder_main(root->left, l-1);
bt_levelorder_main(root->right, l-1);
}
}
void bt_levelorder(btNode *root){
int h = btheight(root);
for (int i = 1; i<=h; i++){
bt_levelorder_main(root, i);
}
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// MAKING NODE AND INSERING ELEMENTS IN LEFT OR RIGHT
btNode *createNode(int value)
{
btNode *newNode = malloc(sizeof(btNode));
newNode->data = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
};
btNode *insertLeft(btNode *root, int d){
root->left = createNode(d);
return root->left;
}
btNode *insertRight(btNode *root, int d){
root->right = createNode(d);
return root->right;
}
btNode *bt_deepestNodeAddress(btNode *root, int tree_height){
int h = tree_height;
btNode *res = NULL;
if(!(root)) return root;
if(h == 1) {
// printf("%d", root->data);
return root;
}
else if (h>1){
res = bt_deepestNodeAddress(root->left, h-1);
if(res) return res;
res = bt_deepestNodeAddress(root->right, h-1);
if(res) return res;
}
// return root;
}
btNode *bt_deleteNode_main(btNode *root, int d){
btNode *temp = root;
// if (!(root->left) + !(root->right)){
// if (root->data == d) return NULL;
// else return root;
// }
// btNode *p = NULL;
// btNode *addr = NULL;
// while(!p){
// temp = root;
// if (temp->data == d) addr = temp;
// if (temp->left) p = temp->left;
// if ()
// }
if (temp->data == d){
if (!(temp->left)){
temp = root->right;
root = NULL;
return temp;
}
else if (!(temp->left)){
temp = root->right;
root = NULL;
return temp;
}
else{
temp = bt_deepestNodeAddress(root->right, btheight(root->right));
root->data = temp->data;
root->right = bt_deleteNode_main(root->right, temp->data);
// temp = root->left;
}
}
if (root->right) root->right = bt_deleteNode_main(root->right, d);
if (root->left) root->left = bt_deleteNode_main(root->left, d);
return root;
}
btNode *bt_deleteNode(btNode *root, int d){
if (!(root)) return root;
if (!(isPresent(root, d))) return root;
return bt_deleteNode_main(root, d);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*
void display(btNode *ptr, int level)
{
int i;
if(ptr != NULL)
{
display(ptr->right, level+1);
for(i = 0; i<level; i++)
printf("\t");
printf("%d\n", ptr->data);
display(ptr->left, level+1);
}
}
*/
// %%%%%%%%%% TREE DISPLAY FUNCTION %%%%%%%%%%%%%%
int _print_t(btNode *tree, int is_left, int offset, int depth, char s[20][512])
{
char b[20];
// char s[20][255];
int width = 5;
if (!tree) return 0;
sprintf(b, "(%03d)", tree->data);
int left = _print_t(tree->left, 1, offset, depth + 1, s);
int right = _print_t(tree->right, 0, offset + left + width, depth + 1, s);
#ifdef COMPACT
for (int i = 0; i < width; i++)
s[depth][offset + left + i] = b[i];
if (depth && is_left) {
for (int i = 0; i < width + right; i++)
s[depth - 1][offset + left + width/2 + i] = '-';
s[depth - 1][offset + left + width/2] = '.';
} else if (depth && !is_left) {
for (int i = 0; i < left + width; i++)
s[depth - 1][offset - width/2 + i] = '-';
s[depth - 1][offset + left + width/2] = '.';
}
#else
for (int i = 0; i < width; i++)
s[2 * depth][offset + left + i] = b[i];
if (depth && is_left) {
for (int i = 0; i < width + right; i++)
s[2 * depth - 1][offset + left + width/2 + i] = '-';
s[2 * depth - 1][offset + left + width/2] = '+';
s[2 * depth - 1][offset + left + width + right + width/2] = '+';
}
else if (depth && !is_left) {
for (int i = 0; i < left + width; i++)
s[2 * depth - 1][offset - width/2 + i] = '-';
s[2 * depth - 1][offset + left + width/2] = '+';
s[2 * depth - 1][offset - width/2 - 1] = '+';
}
#endif
return left + width + right;
}
void print_t(btNode *tree)
{
int h = btheight(tree)+6;
char s[h][512]; //s[h][255]
for (int i = 0; i < h; i++)
sprintf(s[i], "%255s", " ");
_print_t(tree, 0, 0, 0, s);
for (int i = 0; i < h; i++)
printf("%s\n", s[i]);
}
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// %%%%%%%%%% CREATING BINARY TREE FROM ARRAY %%%%%%%%%%%%%%
btNode *makeBtfromArr(btNode *root, node *head){
node *queue = NULL;
btNode *temp = NULL;
if (!head) return NULL;
root = createNode(head->data); // FIRST ELEMENT IN ARRAY IS ROOT ELEMENT OF TREE
queue = python_append(queue, (unsigned int)root); // STORING ADDRESS OF ROOT IN QUEUE
head = head->next; // NEXT ELEMENT IN ARRAY
while(head){
temp = (btNode*)python_atindex(queue, 0); // STORING ADDRESS OF NEW ROOT FOR NEW SUB-TREE
queue = python_remove(queue, 0); // REMOVING FROM QUEUE
btNode *left = NULL; // LEFT CHILD
btNode *right = NULL; // RIGHT CHILD
left = createNode(head->data); // MAKING LEFT NODE OF SUB-TREE
queue = python_append(queue,(unsigned int)left); // ADDING ADDRESS OF LEFT CHILD IN QUEUE
head = head->next; // ADVANCING POINTER IN THE ARRAY
if(head){
right = createNode(head->data); // MAKING RIGHT NODE OF SUB-TREE
queue = python_append(queue, (unsigned int)right); // ADDING ADDRESS OF RIGHT CHILD IN QUEUE
head = head->next; // ADVANCING POINTER IN THE ARRAY
}
temp->left = left; // ADDING LEFT SUB-TREE IN THE MAIN TREE
temp->right = right; // ADDING RIGHT SUB-TREE IN THE MAIN TREE
// queue = python_remove(queue, 0); // REMOVING ROOT (OF SUB-TREE) FROM THE QUEUE
}
return root;
}