The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / deps / libebb / rbtree.c @ 40c0f755
History | View | Annotate | Download (11.1 KB)
1 |
/* Copyright (c) 2008 Derrick Coetzee
|
---|---|
2 |
* http://en.literateprograms.org/Red-black_tree_(C)?oldid=7982
|
3 |
*
|
4 |
* Permission is hereby granted, free of charge, to any person obtaining a
|
5 |
* copy of this software and associated documentation files (the
|
6 |
* "Software"), to deal in the Software without restriction, including
|
7 |
* without limitation the rights to use, copy, modify, merge, publish,
|
8 |
* distribute, sublicense, and/or sell copies of the Software, and to permit
|
9 |
* persons to whom the Software is furnished to do so, subject to the
|
10 |
* following conditions:
|
11 |
*
|
12 |
* The above copyright notice and this permission notice shall be included
|
13 |
* in all copies or substantial portions of the Software.
|
14 |
*
|
15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
16 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
18 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
19 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
|
20 |
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
|
21 |
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22 |
*/
|
23 |
|
24 |
#include "rbtree.h" |
25 |
#include <assert.h> |
26 |
|
27 |
#ifndef NULL |
28 |
# define NULL ((void*)0) |
29 |
#endif
|
30 |
|
31 |
|
32 |
typedef rbtree_node node;
|
33 |
typedef enum rbtree_node_color color; |
34 |
|
35 |
|
36 |
static node grandparent(node n);
|
37 |
static node sibling(node n);
|
38 |
static node uncle(node n);
|
39 |
static void verify_properties(rbtree t); |
40 |
static void verify_property_1(node root); |
41 |
/* static void verify_property_2(node root); */
|
42 |
static color node_color(node n);
|
43 |
static void verify_property_4(node root); |
44 |
/* static void verify_property_5(node root); */
|
45 |
static void verify_property_5_helper(node n, int black_count, int* black_count_path); |
46 |
|
47 |
static node lookup_node(rbtree t, void* key); |
48 |
static void rotate_left(rbtree t, node n); |
49 |
static void rotate_right(rbtree t, node n); |
50 |
|
51 |
static void replace_node(rbtree t, node oldn, node newn); |
52 |
static void insert_case1(rbtree t, node n); |
53 |
static void insert_case2(rbtree t, node n); |
54 |
static void insert_case3(rbtree t, node n); |
55 |
static void insert_case4(rbtree t, node n); |
56 |
static void insert_case5(rbtree t, node n); |
57 |
static node maximum_node(node root);
|
58 |
static void delete_case1(rbtree t, node n); |
59 |
static void delete_case2(rbtree t, node n); |
60 |
static void delete_case3(rbtree t, node n); |
61 |
static void delete_case4(rbtree t, node n); |
62 |
static void delete_case5(rbtree t, node n); |
63 |
static void delete_case6(rbtree t, node n); |
64 |
|
65 |
node grandparent(node n) { |
66 |
assert (n != NULL);
|
67 |
assert (n->parent != NULL); /* Not the root node */ |
68 |
assert (n->parent->parent != NULL); /* Not child of root */ |
69 |
return n->parent->parent;
|
70 |
} |
71 |
|
72 |
node sibling(node n) { |
73 |
assert (n != NULL);
|
74 |
assert (n->parent != NULL); /* Root node has no sibling */ |
75 |
if (n == n->parent->left)
|
76 |
return n->parent->right;
|
77 |
else
|
78 |
return n->parent->left;
|
79 |
} |
80 |
|
81 |
node uncle(node n) { |
82 |
assert (n != NULL);
|
83 |
assert (n->parent != NULL); /* Root node has no uncle */ |
84 |
assert (n->parent->parent != NULL); /* Children of root have no uncle */ |
85 |
return sibling(n->parent);
|
86 |
} |
87 |
|
88 |
void verify_properties(rbtree t) {
|
89 |
#ifdef VERIFY_RBTREE
|
90 |
verify_property_1(t->root); |
91 |
verify_property_2(t->root); |
92 |
/* Property 3 is implicit */
|
93 |
verify_property_4(t->root); |
94 |
verify_property_5(t->root); |
95 |
#endif
|
96 |
} |
97 |
|
98 |
void verify_property_1(node n) {
|
99 |
assert(node_color(n) == RED || node_color(n) == BLACK); |
100 |
if (n == NULL) return; |
101 |
verify_property_1(n->left); |
102 |
verify_property_1(n->right); |
103 |
} |
104 |
|
105 |
/*
|
106 |
void verify_property_2(node root) {
|
107 |
assert(node_color(root) == BLACK);
|
108 |
}
|
109 |
*/
|
110 |
|
111 |
color node_color(node n) { |
112 |
return n == NULL ? BLACK : n->color; |
113 |
} |
114 |
|
115 |
void verify_property_4(node n) {
|
116 |
if (node_color(n) == RED) {
|
117 |
assert (node_color(n->left) == BLACK); |
118 |
assert (node_color(n->right) == BLACK); |
119 |
assert (node_color(n->parent) == BLACK); |
120 |
} |
121 |
if (n == NULL) return; |
122 |
verify_property_4(n->left); |
123 |
verify_property_4(n->right); |
124 |
} |
125 |
|
126 |
/*
|
127 |
void verify_property_5(node root) {
|
128 |
int black_count_path = -1;
|
129 |
verify_property_5_helper(root, 0, &black_count_path);
|
130 |
}
|
131 |
*/
|
132 |
|
133 |
void verify_property_5_helper(node n, int black_count, int* path_black_count) { |
134 |
if (node_color(n) == BLACK) {
|
135 |
black_count++; |
136 |
} |
137 |
if (n == NULL) { |
138 |
if (*path_black_count == -1) { |
139 |
*path_black_count = black_count; |
140 |
} else {
|
141 |
assert (black_count == *path_black_count); |
142 |
} |
143 |
return;
|
144 |
} |
145 |
verify_property_5_helper(n->left, black_count, path_black_count); |
146 |
verify_property_5_helper(n->right, black_count, path_black_count); |
147 |
} |
148 |
|
149 |
void rbtree_init(rbtree t, rbtree_compare_func compare) {
|
150 |
t->root = NULL;
|
151 |
t->compare = compare; |
152 |
verify_properties(t); |
153 |
} |
154 |
|
155 |
node lookup_node(rbtree t, void* key) {
|
156 |
node n = t->root; |
157 |
while (n != NULL) { |
158 |
int comp_result = t->compare(key, n->key);
|
159 |
if (comp_result == 0) { |
160 |
return n;
|
161 |
} else if (comp_result < 0) { |
162 |
n = n->left; |
163 |
} else {
|
164 |
assert(comp_result > 0);
|
165 |
n = n->right; |
166 |
} |
167 |
} |
168 |
return n;
|
169 |
} |
170 |
|
171 |
void* rbtree_lookup(rbtree t, void* key) { |
172 |
node n = lookup_node(t, key); |
173 |
return n == NULL ? NULL : n->value; |
174 |
} |
175 |
|
176 |
void rotate_left(rbtree t, node n) {
|
177 |
node r = n->right; |
178 |
replace_node(t, n, r); |
179 |
n->right = r->left; |
180 |
if (r->left != NULL) { |
181 |
r->left->parent = n; |
182 |
} |
183 |
r->left = n; |
184 |
n->parent = r; |
185 |
} |
186 |
|
187 |
void rotate_right(rbtree t, node n) {
|
188 |
node L = n->left; |
189 |
replace_node(t, n, L); |
190 |
n->left = L->right; |
191 |
if (L->right != NULL) { |
192 |
L->right->parent = n; |
193 |
} |
194 |
L->right = n; |
195 |
n->parent = L; |
196 |
} |
197 |
|
198 |
void replace_node(rbtree t, node oldn, node newn) {
|
199 |
if (oldn->parent == NULL) { |
200 |
t->root = newn; |
201 |
} else {
|
202 |
if (oldn == oldn->parent->left)
|
203 |
oldn->parent->left = newn; |
204 |
else
|
205 |
oldn->parent->right = newn; |
206 |
} |
207 |
if (newn != NULL) { |
208 |
newn->parent = oldn->parent; |
209 |
} |
210 |
} |
211 |
|
212 |
void rbtree_insert(rbtree t, rbtree_node inserted_node) {
|
213 |
inserted_node->color = RED; |
214 |
inserted_node->left = NULL;
|
215 |
inserted_node->right = NULL;
|
216 |
inserted_node->parent = NULL;
|
217 |
|
218 |
if (t->root == NULL) { |
219 |
t->root = inserted_node; |
220 |
} else {
|
221 |
node n = t->root; |
222 |
while (1) { |
223 |
int comp_result = t->compare(inserted_node->key, n->key);
|
224 |
if (comp_result == 0) { |
225 |
n->value = inserted_node->value; |
226 |
return;
|
227 |
} else if (comp_result < 0) { |
228 |
if (n->left == NULL) { |
229 |
n->left = inserted_node; |
230 |
break;
|
231 |
} else {
|
232 |
n = n->left; |
233 |
} |
234 |
} else {
|
235 |
assert (comp_result > 0);
|
236 |
if (n->right == NULL) { |
237 |
n->right = inserted_node; |
238 |
break;
|
239 |
} else {
|
240 |
n = n->right; |
241 |
} |
242 |
} |
243 |
} |
244 |
inserted_node->parent = n; |
245 |
} |
246 |
insert_case1(t, inserted_node); |
247 |
verify_properties(t); |
248 |
} |
249 |
|
250 |
void insert_case1(rbtree t, node n) {
|
251 |
if (n->parent == NULL) |
252 |
n->color = BLACK; |
253 |
else
|
254 |
insert_case2(t, n); |
255 |
} |
256 |
|
257 |
void insert_case2(rbtree t, node n) {
|
258 |
if (node_color(n->parent) == BLACK)
|
259 |
return; /* Tree is still valid */ |
260 |
else
|
261 |
insert_case3(t, n); |
262 |
} |
263 |
|
264 |
void insert_case3(rbtree t, node n) {
|
265 |
if (node_color(uncle(n)) == RED) {
|
266 |
n->parent->color = BLACK; |
267 |
uncle(n)->color = BLACK; |
268 |
grandparent(n)->color = RED; |
269 |
insert_case1(t, grandparent(n)); |
270 |
} else {
|
271 |
insert_case4(t, n); |
272 |
} |
273 |
} |
274 |
|
275 |
void insert_case4(rbtree t, node n) {
|
276 |
if (n == n->parent->right && n->parent == grandparent(n)->left) {
|
277 |
rotate_left(t, n->parent); |
278 |
n = n->left; |
279 |
} else if (n == n->parent->left && n->parent == grandparent(n)->right) { |
280 |
rotate_right(t, n->parent); |
281 |
n = n->right; |
282 |
} |
283 |
insert_case5(t, n); |
284 |
} |
285 |
|
286 |
void insert_case5(rbtree t, node n) {
|
287 |
n->parent->color = BLACK; |
288 |
grandparent(n)->color = RED; |
289 |
if (n == n->parent->left && n->parent == grandparent(n)->left) {
|
290 |
rotate_right(t, grandparent(n)); |
291 |
} else {
|
292 |
assert (n == n->parent->right && n->parent == grandparent(n)->right); |
293 |
rotate_left(t, grandparent(n)); |
294 |
} |
295 |
} |
296 |
|
297 |
rbtree_node rbtree_delete(rbtree t, void* key) {
|
298 |
node child; |
299 |
node n = lookup_node(t, key); |
300 |
if (n == NULL) return NULL; /* Key not found, do nothing */ |
301 |
if (n->left != NULL && n->right != NULL) { |
302 |
/* Copy key/value from predecessor and then delete it instead */
|
303 |
node pred = maximum_node(n->left); |
304 |
n->key = pred->key; |
305 |
n->value = pred->value; |
306 |
n = pred; |
307 |
} |
308 |
|
309 |
assert(n->left == NULL || n->right == NULL); |
310 |
child = n->right == NULL ? n->left : n->right;
|
311 |
if (node_color(n) == BLACK) {
|
312 |
n->color = node_color(child); |
313 |
delete_case1(t, n); |
314 |
} |
315 |
replace_node(t, n, child); |
316 |
|
317 |
verify_properties(t); |
318 |
return n;
|
319 |
} |
320 |
|
321 |
static node maximum_node(node n) {
|
322 |
assert (n != NULL);
|
323 |
while (n->right != NULL) { |
324 |
n = n->right; |
325 |
} |
326 |
return n;
|
327 |
} |
328 |
|
329 |
void delete_case1(rbtree t, node n) {
|
330 |
if (n->parent == NULL) |
331 |
return;
|
332 |
else
|
333 |
delete_case2(t, n); |
334 |
} |
335 |
|
336 |
void delete_case2(rbtree t, node n) {
|
337 |
if (node_color(sibling(n)) == RED) {
|
338 |
n->parent->color = RED; |
339 |
sibling(n)->color = BLACK; |
340 |
if (n == n->parent->left)
|
341 |
rotate_left(t, n->parent); |
342 |
else
|
343 |
rotate_right(t, n->parent); |
344 |
} |
345 |
delete_case3(t, n); |
346 |
} |
347 |
|
348 |
void delete_case3(rbtree t, node n) {
|
349 |
if (node_color(n->parent) == BLACK &&
|
350 |
node_color(sibling(n)) == BLACK && |
351 |
node_color(sibling(n)->left) == BLACK && |
352 |
node_color(sibling(n)->right) == BLACK) |
353 |
{ |
354 |
sibling(n)->color = RED; |
355 |
delete_case1(t, n->parent); |
356 |
} |
357 |
else
|
358 |
delete_case4(t, n); |
359 |
} |
360 |
|
361 |
void delete_case4(rbtree t, node n) {
|
362 |
if (node_color(n->parent) == RED &&
|
363 |
node_color(sibling(n)) == BLACK && |
364 |
node_color(sibling(n)->left) == BLACK && |
365 |
node_color(sibling(n)->right) == BLACK) |
366 |
{ |
367 |
sibling(n)->color = RED; |
368 |
n->parent->color = BLACK; |
369 |
} |
370 |
else
|
371 |
delete_case5(t, n); |
372 |
} |
373 |
|
374 |
void delete_case5(rbtree t, node n) {
|
375 |
if (n == n->parent->left &&
|
376 |
node_color(sibling(n)) == BLACK && |
377 |
node_color(sibling(n)->left) == RED && |
378 |
node_color(sibling(n)->right) == BLACK) |
379 |
{ |
380 |
sibling(n)->color = RED; |
381 |
sibling(n)->left->color = BLACK; |
382 |
rotate_right(t, sibling(n)); |
383 |
} |
384 |
else if (n == n->parent->right && |
385 |
node_color(sibling(n)) == BLACK && |
386 |
node_color(sibling(n)->right) == RED && |
387 |
node_color(sibling(n)->left) == BLACK) |
388 |
{ |
389 |
sibling(n)->color = RED; |
390 |
sibling(n)->right->color = BLACK; |
391 |
rotate_left(t, sibling(n)); |
392 |
} |
393 |
delete_case6(t, n); |
394 |
} |
395 |
|
396 |
void delete_case6(rbtree t, node n) {
|
397 |
sibling(n)->color = node_color(n->parent); |
398 |
n->parent->color = BLACK; |
399 |
if (n == n->parent->left) {
|
400 |
assert (node_color(sibling(n)->right) == RED); |
401 |
sibling(n)->right->color = BLACK; |
402 |
rotate_left(t, n->parent); |
403 |
} |
404 |
else
|
405 |
{ |
406 |
assert (node_color(sibling(n)->left) == RED); |
407 |
sibling(n)->left->color = BLACK; |
408 |
rotate_right(t, n->parent); |
409 |
} |
410 |
} |
411 |
|
412 |
|