-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbltree.c
261 lines (211 loc) · 5.88 KB
/
bltree.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
* Author: peter
* Balanced Tree Implemenation
*/
#include "bltree.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define max(a, b) (a > b ? a : b)
void BLTNodeInit(BLNode_t a_node)
{
a_node->height = 1;
a_node->left = NULL;
a_node->right = NULL;
}
BLNode_t BLTRotateRight(BLNode_t parent, BLNode_t a_node)
{
if(!a_node)
return parent;
/*
parent a_node
/ \ -> / \
a_node Y X parent
/ \ / \
X Z Z Y
*/
parent->left = a_node->right;
a_node->right = parent;
parent->height = 1 + max(BLNHEIGHT(parent->left), BLNHEIGHT(parent->right));
a_node->height = 1 + max(BLNHEIGHT(a_node->left), BLNHEIGHT(a_node->right));
return a_node;
}
BLNode_t BLTRotateLeft(BLNode_t parent, BLNode_t a_node)
{
if(!a_node)
return parent;
/*
parent a_node
/ \ / \
X a_node -> parent Z
/ \ / \
Y Z X Y
*/
parent->right = a_node->left;
a_node->left = parent;
parent->height = 1 + max(BLNHEIGHT(parent->left), BLNHEIGHT(parent->right));
a_node->height = 1 + max(BLNHEIGHT(a_node->left), BLNHEIGHT(a_node->right));
return a_node;
}
BLNode_t BLTInsert(BLNode_t root, BLNode_t a_node, int (*cmp)(BLNode_t, BLNode_t))
{
int64_t balance;
int res;
if(!root || !a_node){
// empty tree
return a_node;
}
res = cmp(a_node, root);
if(res < 0){
root->left = BLTInsert(root->left, a_node, cmp);
} else if(res > 0){
root->right = BLTInsert(root->right, a_node, cmp);
} else {
// don't insert equal node to this tree
return root;
}
// update root
root->height = 1 + max(BLNHEIGHT(root->left), BLNHEIGHT(root->right));
balance = BLNBALANCE(root);
res = cmp(a_node, root->left);
if(balance > 1 && res < 0){
// case 1: left's height > right's height and root->left's value > a_node's value
root = BLTRotateRight(root, root->left);
}
if(balance > 1 && res >= 0){
// case 3: left's height > right's height and root->left's value <= a_node's value
root->left = BLTRotateLeft(root->left, root->left->right);
root = BLTRotateRight(root, root->left);
}
res = cmp(a_node, root->right);
if(balance < -1 && res > 0) {
// case 2: left's height < right's height and root->right's value < a_node's value
root = BLTRotateLeft(root, root->right);
}
if(balance < -1 && res <= 0){
// case 4: left's height < right's height and root->right's value >= a_node's value
root->right = BLTRotateRight(root->right, root->right->left);
root = BLTRotateLeft(root, root->right);
}
return root;
}
BLNode_t findMinValueNode(BLNode_t node)
{
BLNode_t cur = node;
while(cur->left != NULL)
cur = cur->left;
return cur;
}
BLNode_t BLTDelete(BLNode_t root, BLNode_t a_node,
int (*cmp)(BLNode_t, BLNode_t), void (*deallocObject)(BLNode_t),
void (*swap)(BLNode_t, BLNode_t))
{
BLNode_t select_node;
int64_t balance;
int res;
if(!root)
return NULL;
if(!a_node)
return root;
res = cmp(a_node, root);
if(res < 0){
root->left = BLTDelete(root->left, a_node, cmp, deallocObject, swap);
}else if(res > 0){
root->right = BLTDelete(root->right, a_node, cmp, deallocObject, swap);
} else {
// found node
if(!root->left && !root->right){
// this node is the bottom node
deallocObject(root);
return NULL;
}
if(root->left && root->right){
// delete a node has 2 childs
select_node = findMinValueNode(root->right);
swap(select_node, root);
root->right = BLTDelete(root->right, select_node, cmp, deallocObject, swap);
} else {
// delete a node only has one child
select_node = root->left != NULL ? root->left : root->right;
deallocObject(root);
return select_node;
}
}
// re-balance the tree after delete a node
root->height = 1 + max(BLNHEIGHT(root->left), BLNHEIGHT(root->right));
balance = BLNBALANCE(root);
if(balance < -1 && BLNBALANCE(root->right) < 0){
// case 1: balance root < -1 and BLNBALANCE(root->right) > 0
root = BLTRotateLeft(root, root->right);
}
if(balance > 1 && BLNBALANCE(root->left) > 0){
// case 2: balance root > 1 and BLNBALANCE(root->left) > 0
root = BLTRotateRight(root, root->left);
}
if(balance < -1 && BLNBALANCE(root->right) > 0){
// case 3: balance root < -1 and BLNBALANCE(root->right) < 0
root->right = BLTRotateRight(root->right, root->right->left);
root = BLTRotateLeft(root, root->right);
}
if(balance > 1 && BLNBALANCE(root->left) < 0){
// case 4: balance root > 1 and BLNBALANCE(root->left) < 0
root->left = BLTRotateLeft(root->left, root->left->right);
root = BLTRotateRight(root, root->left);
}
return root;
}
BLNode_t BLTSearch(BLNode_t root, BLNode_t a_node, int (*cmp)(BLNode_t, BLNode_t))
{
int res = 0;
if(!a_node || !root)
return NULL;
res = cmp(a_node, root);
if(res < 0){
return BLTSearch(root->left, a_node, cmp);
}else if(res > 0){
return BLTSearch(root->right, a_node, cmp);
} else {
return root;
}
}
void BLTWalk(BLNode_t root, void (*callback)(BLNode_t))
{
if(!root){
return;
}
callback(root);
if(root->left)
BLTWalk(root->left, callback);
if(root->right)
BLTWalk(root->right, callback);
}
uint32_t BLTIsBalance(BLNode_t root)
{
int64_t balance;
if(!root)
return 0;
if(!root->left && !root->right){
return 0;
}
balance = BLTIsBalance(root->left);
if(balance)
return balance;
balance = BLTIsBalance(root->right);
if(balance)
return balance;
balance = BLNHEIGHT(root->left) - BLNHEIGHT(root->right);
if(balance > 1 && balance < -1)
return balance;
return 0;
}
BLNode_t BLTDestroy(BLNode_t root, void (*deallocObject)(BLNode_t))
{
if(!root)
return NULL;
if(root->left)
root->left = BLTDestroy(root->left, deallocObject);
if(root->right)
root->right = BLTDestroy(root->right, deallocObject);
deallocObject(root);
return NULL;
}