-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_tree_balanced.c
119 lines (96 loc) · 2.32 KB
/
is_tree_balanced.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define GET_MAX(a, b) (a > b) ? a : b
typedef struct _tree {
int val;
struct _tree *right;
struct _tree *left;
} tree;
int check_if_tree_is_balanced(tree *);
int measure_height(tree *);
void insert_tree(tree **, int);
void create_node(tree **, int *);
void print_inorder(tree *);
int main() {
int items[]= {45, 23, 98, 38, 2, 12, 98, 101, 36, 100};
tree *head = NULL;
int len = 0;
int i = 0;
len = (sizeof(items)/sizeof(int));
printf("Size is: %d\n", len);
for (i = 0; i <= len - 1; i++) {
insert_tree(&head, items[i]);
}
print_inorder(head);
if (check_if_tree_is_balanced(head))
printf("\nYes\n");
else
printf("No\n");
}
int check_if_tree_is_balanced(tree *head) {
int left_h = 0;
int right_h = 0;
int height = 0;
if (!head)
return 1;
left_h += measure_height(head->left);
right_h += measure_height(head->right);
printf("left_h: %d right_h: %d\n", left_h, right_h);
height = abs(left_h - right_h);
printf("height: %d", height);
return ((height > 1) ? 0 : 1);
}
int measure_height(tree *node) {
int left_h = 0;
int right_h = 0;
if (!node)
return 0;
if (node->left)
left_h = 1;
if (node->right)
right_h = 1;
left_h += measure_height(node->left);
right_h += measure_height(node->right);
return(GET_MAX(left_h, right_h));
}
void print_inorder(tree *node) {
if (!node)
return;
print_inorder(node->left);
printf("%d ", node->val);
print_inorder(node->right);
}
void insert_tree(tree **head, int item) {
tree *node = NULL;
tree *prevNode = NULL;
if (!*head) {
create_node(&node, &item);
*head = node;
return;
}
node = *head;
while (node) {
prevNode = node;
if (node->val >= item) {
node = node->left;
} else {
node = node->right;
}
}
create_node(&node, &item);
if (prevNode->val >= item) {
prevNode->left = node;
} else {
prevNode->right = node;
}
return;
}
void create_node(tree **node, int *item) {
if (!item)
return;
*node = malloc(sizeof(tree));
(*node)->val = *item;
(*node)->right = NULL;
(*node)->left = NULL;
}