-
Notifications
You must be signed in to change notification settings - Fork 0
/
1382 Balance a binary search tree.c
123 lines (106 loc) · 3.15 KB
/
1382 Balance a binary search 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
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
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
int height(struct TreeNode * root)
{
if(root==NULL)
return 0;
int left = height(root->left);
int right = height(root->right);
if(left>=right)
return left+1;
return right+1;
}
int check_balance_factor(struct TreeNode * root)
{
int balance_factor = 0;
if(root->left==NULL && root->right==NULL)
{
balance_factor = 0;
}
else if(root->left==NULL && root->right!=NULL)
{
balance_factor = 0 - height(root->right);
}
else if(root->left!=NULL && root->right==NULL)
{
balance_factor = height(root->left);
}
else
{
balance_factor = height(root->left) - height(root->right);
}
return balance_factor;
}
struct TreeNode* balance(struct TreeNode * root, bool * bal)
{
if(root == NULL)
return;
root->left = balance(root->left, bal);
root->right = balance(root->right, bal);
//Check the balance factor
int bal_factor = check_balance_factor(root);
//printf("node : %d, bf : %d\r\n", root->val, bal_factor);
if(bal_factor>=2 || bal_factor <= -2)
{
*bal = false;
//LL Rotation
if(bal_factor>=2 && check_balance_factor(root->left)>=0)
{
struct TreeNode * tmp_node = root;
struct TreeNode * left_node = root->left;
tmp_node->left = left_node->right;
left_node->right = tmp_node;
return left_node;
}
//RR Rotation
else if(bal_factor<=-2 && check_balance_factor(root->right)<=0)
{
struct TreeNode * tmp_node = root;
struct TreeNode * right_node = root->right;
tmp_node->right = right_node->left;
right_node->left = tmp_node;
return right_node;
}
//LR Rotation
else if(bal_factor>=2 && check_balance_factor(root->left)<=0)
{
struct TreeNode * tmp_node = root;
struct TreeNode * left_node = root->left;
struct TreeNode * new_node = left_node->right;
tmp_node->left = new_node->right;
left_node->right = new_node->left;
new_node->left = left_node;
new_node->right = tmp_node;
return new_node;
}
//RL Rotation
else if(bal_factor<=-2 && check_balance_factor(root->right)>=0)
{
struct TreeNode * tmp_node = root;
struct TreeNode * right_node = root->right;
struct TreeNode * new_node = right_node->left;
tmp_node->right = new_node->left;
right_node->left = new_node->right;
new_node->right = right_node;
new_node->left = tmp_node;
return new_node;
}
}
return root;
}
struct TreeNode* balanceBST(struct TreeNode* root)
{
bool bal = false;
while(bal == false)
{
bal = true;
root = balance(root, &bal);
}
return root;
}