-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself-balancing-tree.c
77 lines (68 loc) · 1.77 KB
/
self-balancing-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
/* Node is defined as :
typedef struct node
{
int val;
struct node* left;
struct node* right;
int ht;
} node; */
int get_height(node *root) {
return root ? root->ht : -1;
}
void update_height(node *root) {
int left = get_height(root->left);
int right = get_height(root->right);
root->ht = left > right ? left + 1 : right + 1;
}
int get_balance_factor(node *root) {
return get_height(root->left) - get_height(root->right);
}
node* rotate_left(node *root) {
node *new_root = root->right;
root->right = new_root->left;
new_root->left = root;
update_height(new_root->left);
update_height(new_root);
return new_root;
}
node* rotate_right(node *root) {
node *new_root = root->left;
root->left = new_root->right;
new_root->right = root;
update_height(new_root->right);
update_height(new_root);
return new_root;
}
node* balance(node *root) {
int balance_factor = get_balance_factor(root);
node *new_root = root;
if (balance_factor > 1) {
if (get_balance_factor(root->left) < 0) {
root->left = rotate_left(root->left);
}
new_root = rotate_right(root);
} else if (balance_factor < -1) {
if (get_balance_factor(root->right) > 0) {
root->right = rotate_right(root->right);
}
new_root = rotate_left(root);
}
return new_root;
}
node* insert(node * root,int val)
{
if (!root) {
node *temp = (node*) malloc(sizeof(node));
memset(temp, 0, sizeof(node));
temp->val = val;
return temp;
}
if (root->val < val) {
root->right = insert(root->right, val);
} else {
root->left = insert(root->left, val);
}
update_height(root);
root = balance(root);
return root;
}