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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
#include <malloc.h>
#include <debug.h>
#include <btree.h>
typedef struct btree_item {
void *key, *val;
int height;
struct btree_item *left, *right;
} btree_item_t;
struct btree {
key_cmp_fun_t cf;
kv_iter_fun_t releasef;
btree_item_t *root;
int nitems;
};
btree_t* create_btree(key_cmp_fun_t cf, kv_iter_fun_t relf) {
btree_t* t = (btree_t*)malloc(sizeof(btree_t));
if (t == 0) return 0;
t->cf = cf;
t->releasef = relf;
t->root = 0;
t->nitems = 0;
return t;
}
void delete_btree(btree_t *t) {
void delete_item_rec(btree_item_t *i,kv_iter_fun_t relf) {
if (i == 0) return;
delete_item_rec(i->left, relf);
delete_item_rec(i->right, relf);
relf(i->key, i->val);
free(i);
}
delete_item_rec(t->root, t->releasef);
free(t);
}
size_t btree_count(btree_t *i) {
return i->nitems;
}
// ============== //
// TREE ROTATIONS //
// ============== //
static int height(btree_item_t *i) {
if (i == 0) return 0;
return i->height;
}
static void recalc_height(btree_item_t *i) {
if (i == 0) return;
i->height = MAX(height(i->left), height(i->right)) + 1;
}
static btree_item_t* rotate_left(btree_item_t *i) {
// I(X(a, b), Y) -> a X b I Y -> X(a, I(b, Y))
btree_item_t *x = i->left;
if (x == 0) return i;
btree_item_t *b = x->right;
x->right = i;
i->left = b;
recalc_height(i);
recalc_height(x);
return x;
}
static btree_item_t* rotate_right(btree_item_t *i) {
// I(X, Y(a,b)) -> X I a Y b -> Y(I(X, a), b)
btree_item_t *y = i->right;
if (y == 0) return i;
btree_item_t *a = y->left;
y->left = i;
i->right = a;
recalc_height(i);
recalc_height(y);
return y;
}
static btree_item_t* equilibrate(btree_item_t *i) {
if (i == 0) return 0;
while (height(i->left) - height(i->right) >= 2)
i = rotate_left(i);
while (height(i->right) - height(i->left) >= 2)
i = rotate_right(i);
return i;
}
// =================== //
// ADDING AND DELETING //
// =================== //
bool btree_add(btree_t *t, void* key, void* val) {
btree_item_t* insert(btree_t *t, btree_item_t *root, btree_item_t *i) {
if (root == 0) return i;
if (t->cf(i->key, root->key) <= 0) {
root->left = insert(t, root->left, i);
} else {
root->right = insert(t, root->right, i);
}
recalc_height(root);
return equilibrate(root);
}
btree_item_t *x = (btree_item_t*)malloc(sizeof(btree_item_t));
if (x == 0) return false;
x->key = key;
x->val = val;
x->left = x->right = 0;
recalc_height(x);
t->root = insert(t, t->root, x);
t->nitems++;
return true;
}
void btree_remove(btree_t *t, const void* key) {
btree_item_t *extract_smallest(btree_item_t *i, btree_item_t **out_smallest) {
ASSERT(i != 0);
if (i->left == 0) {
*out_smallest = i;
return i->right;
} else {
i->left = extract_smallest(i->left, out_smallest);
recalc_height(i);
return equilibrate(i);
}
}
btree_item_t *remove_aux(btree_t *t, btree_item_t *i, const void* key) {
if (i == 0) return 0;
int c = t->cf(key, i->key);
if (c < 0) {
i->left = remove_aux(t, i->left, key);
return equilibrate(i);
} else if (c > 0) {
i->right = remove_aux(t, i->right, key);
return equilibrate(i);
} else {
// remove item i
btree_item_t *new_i;
if (i->right == 0 || i->left == 0) {
new_i = (i->right == 0 ? i->left : i->right);
} else {
btree_item_t *new_i_right = extract_smallest(i->right, &new_i);
new_i->left = i->left;
new_i->right = new_i_right;
recalc_height(new_i);
new_i = equilibrate(new_i);
}
t->releasef(i->key, i->val);
free(i);
t->nitems--;
return remove_aux(t, new_i, key); // loop because several elements may correspond
}
}
t->root = remove_aux(t, t->root, key);
}
// ======================== //
// LOOKING UP AND ITERATING //
// ======================== //
void* btree_find(btree_t *t, const void* key) {
btree_item_t *find_aux(btree_t *t, btree_item_t *i, const void* key) {
if (i == 0) return 0;
int c = t->cf(key, i->key);
if (c == 0) {
return i;
} else if (c < 0) {
return find_aux(t, i->left, key);
} else {
return find_aux(t, i->right, key);
}
}
btree_item_t *i = find_aux(t, t->root, key);
if (i == 0) return 0;
return i->val;
}
void* btree_lower(btree_t *t, const void* key) {
btree_item_t *find_aux(btree_t *t, btree_item_t *i, const void* key) {
if (i == 0) return 0;
int c = t->cf(key, i->key);
if (c == 0) {
return i;
} else if (c < 0) {
return find_aux(t, i->left, key);
} else {
btree_item_t *r = find_aux(t, i->right, key);
if (r == 0) r = i;
return r;
}
}
btree_item_t *i = find_aux(t, t->root, key);
if (i == 0) return 0;
return i->val;
}
void* btree_upper(btree_t *t, const void* key) {
btree_item_t *find_aux(btree_t *t, btree_item_t *i, const void* key) {
if (i == 0) return 0;
int c = t->cf(key, i->key);
if (c == 0) {
return i;
} else if (c < 0) {
btree_item_t *r = find_aux(t, i->left, key);
if (r == 0) r = i;
return r;
} else {
return find_aux(t, i->right, key);
}
}
btree_item_t *i = find_aux(t, t->root, key);
if (i == 0) return 0;
return i->val;
}
void btree_iter(btree_t *t, kv_iter_fun_t f) {
void iter_aux(btree_item_t *i, kv_iter_fun_t f) {
if (i == 0) return;
iter_aux(i->left, f);
f(i->key, i->val);
iter_aux(i->right, f);
}
iter_aux(t->root, f);
}
void btree_iter_on(btree_t *t, const void* key, kv_iter_fun_t f) {
void iter_aux(btree_t *t, btree_item_t *i, const void* key, kv_iter_fun_t f) {
if (i == 0) return;
int c = t->cf(key, i->key);
if (c == 0) {
iter_aux(t, i->left, key, f);
f(i->key, i->val);
iter_aux(t, i->right, key, f);
} else if (c < 0) {
iter_aux(t, i->left, key, f);
} else {
iter_aux(t, i->right, key, f);
}
}
iter_aux(t, t->root, key, f);
}
/* vim: set ts=4 sw=4 tw=0 noet :*/
|