-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtreeisbalanced.c
42 lines (32 loc) · 912 Bytes
/
btreeisbalanced.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
/* Copyright © 2021-2023 Chee Bin HOH. All rights reserved.
*
* Determine if a tree is AVL balanced.
*/
#include "btree.h"
#include <stdio.h>
int main(int argc, char *argv[]) {
struct TreeNode *root = NULL;
printf("Binary tree of adding 3, 2, 4, 5, 6, the tree topology:\n");
root = addTreeNode(root, 3);
root = addTreeNode(root, 2);
root = addTreeNode(root, 4);
root = addTreeNode(root, 5);
root = addTreeNode(root, 6);
printf("\n");
printTreeNodeInTreeTopology(root);
printf("\n");
printf("\n");
if (!isTreeNodeBalanced(root)) {
printf("The tree is not AVL balanced, rebalancing the tree, the tree "
"topology:\n");
printf("\n");
root = treeRebalance(root);
printTreeNodeInTreeTopology(root);
printf("\n");
if (isTreeNodeBalanced(root))
printf("The tree is now AVL balanced\n");
printf("\n");
}
printf("\n");
return 0;
}