-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtreerebalancing.c
79 lines (64 loc) · 2 KB
/
btreerebalancing.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
/* Copyright © 2021-2023 Chee Bin HOH. All rights reserved.
*
* AVL self-balancing tree.
*/
#include "btree.h"
#include <stdio.h>
void checkIfTreeIsAVLRebalanced(struct TreeNode *root) {
if (isTreeNodeBalanced(root)) {
printf("The binary tree is AVL balanced\n");
} else {
printf("The binary tree is not AVL balanced\n");
}
}
int main(int argc, char *argv[]) {
struct TreeNode *root = NULL;
printf("Binary tree topology of adding 6, 7, 8, 5, 4, 10, 9:\n");
printf("\n");
root = addTreeNode(root, 6);
root = addTreeNode(root, 7);
root = addTreeNode(root, 8);
root = addTreeNode(root, 5);
root = addTreeNode(root, 4);
root = addTreeNode(root, 10);
root = addTreeNode(root, 9);
printTreeNodeInTreeTopology(root);
printf("\n");
checkIfTreeIsAVLRebalanced(root);
printf("\n");
printf("\n");
root = delTreeNode(root, 8);
printf("After deleting 8, the tree topology:\n");
printf("\n");
printTreeNodeInTreeTopology(root);
printf("\n");
checkIfTreeIsAVLRebalanced(root);
printf("\n");
printf("\n");
root = NULL; // I do not care about memory leak for this simple test program
printf("Self balancing binary tree of adding 6, 7, 8, 5, 4, 10, 9, 11:\n");
printf("\n");
root = addTreeNodeAndRebalanceTree(root, 6);
root = addTreeNodeAndRebalanceTree(root, 7);
root = addTreeNodeAndRebalanceTree(root, 8);
root = addTreeNodeAndRebalanceTree(root, 5);
root = addTreeNodeAndRebalanceTree(root, 4);
root = addTreeNodeAndRebalanceTree(root, 10);
root = addTreeNodeAndRebalanceTree(root, 9);
root = addTreeNodeAndRebalanceTree(root, 11);
printTreeNodeInTreeTopology(root);
printf("\n");
checkIfTreeIsAVLRebalanced(root);
printf("\n");
printf("\n");
root = delTreeNodeAndRebalanceTree(root, 5);
root = delTreeNodeAndRebalanceTree(root, 6);
printf("After deleting 5 and 6, the tree topology:\n");
printf("\n");
printTreeNodeInTreeTopology(root);
printf("\n");
checkIfTreeIsAVLRebalanced(root);
printf("\n");
printf("\n");
return 0;
}