2 rbtree - simple balanced binary search tree (red-black tree) library.
3 Copyright (C) 2011-2014 John Tsiombikas <nuclear@member.fsf.org>
5 rbtree is free software, feel free to use, modify, and redistribute it, under
6 the terms of the 3-clause BSD license. See COPYING for details.
14 #define INT2PTR(x) ((void*)(intptr_t)(x))
15 #define PTR2INT(x) ((int)(intptr_t)(x))
20 rb_alloc_func_t alloc;
27 struct rbnode *rstack, *iter;
30 static int cmpaddr(const void *ap, const void *bp);
31 static int cmpint(const void *ap, const void *bp);
33 static int count_nodes(struct rbnode *node);
34 static void del_tree(struct rbnode *node, void (*delfunc)(struct rbnode*, void*), void *cls);
35 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data);
36 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key);
37 /*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key);*/
38 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls);
40 struct rbtree *rb_create(rb_cmp_func_t cmp_func)
44 if(!(rb = malloc(sizeof *rb))) {
47 if(rb_init(rb, cmp_func) == -1) {
54 void rb_free(struct rbtree *rb)
61 int rb_init(struct rbtree *rb, rb_cmp_func_t cmp_func)
63 memset(rb, 0, sizeof *rb);
67 } else if(cmp_func == RB_KEY_INT) {
69 } else if(cmp_func == RB_KEY_STRING) {
70 rb->cmp = (rb_cmp_func_t)strcmp;
80 void rb_destroy(struct rbtree *rb)
82 del_tree(rb->root, rb->del, rb->del_cls);
85 void rb_set_allocator(struct rbtree *rb, rb_alloc_func_t alloc, rb_free_func_t free)
92 void rb_set_compare_func(struct rbtree *rb, rb_cmp_func_t func)
97 void rb_set_delete_func(struct rbtree *rb, rb_del_func_t func, void *cls)
104 void rb_clear(struct rbtree *rb)
106 del_tree(rb->root, rb->del, rb->del_cls);
110 int rb_copy(struct rbtree *dest, struct rbtree *src)
116 while((node = rb_next(src))) {
117 if(rb_insert(dest, node->key, node->data) == -1) {
124 int rb_size(struct rbtree *rb)
126 return count_nodes(rb->root);
129 int rb_insert(struct rbtree *rb, void *key, void *data)
131 rb->root = insert(rb, rb->root, key, data);
136 int rb_inserti(struct rbtree *rb, int key, void *data)
138 rb->root = insert(rb, rb->root, INT2PTR(key), data);
144 int rb_delete(struct rbtree *rb, void *key)
146 if((rb->root = delete(rb, rb->root, key))) {
152 int rb_deletei(struct rbtree *rb, int key)
154 if((rb->root = delete(rb, rb->root, INT2PTR(key)))) {
161 struct rbnode *rb_find(struct rbtree *rb, void *key)
163 struct rbnode *node = rb->root;
166 int cmp = rb->cmp(key, node->key);
170 node = cmp < 0 ? node->left : node->right;
175 struct rbnode *rb_findi(struct rbtree *rb, int key)
177 return rb_find(rb, INT2PTR(key));
181 void rb_foreach(struct rbtree *rb, void (*func)(struct rbnode*, void*), void *cls)
183 traverse(rb->root, func, cls);
187 struct rbnode *rb_root(struct rbtree *rb)
192 void rb_begin(struct rbtree *rb)
198 #define push(sp, x) ((x)->next = (sp), (sp) = (x))
199 #define pop(sp) ((sp) = (sp)->next)
202 struct rbnode *rb_next(struct rbtree *rb)
204 struct rbnode *res = 0;
206 while(rb->rstack || rb->iter) {
208 push(rb->rstack, rb->iter);
209 rb->iter = rb->iter->left;
211 rb->iter = top(rb->rstack);
214 rb->iter = rb->iter->right;
221 void *rb_node_key(struct rbnode *node)
223 return node ? node->key : 0;
226 int rb_node_keyi(struct rbnode *node)
228 return node ? PTR2INT(node->key) : 0;
231 void *rb_node_data(struct rbnode *node)
233 return node ? node->data : 0;
236 static int cmpaddr(const void *ap, const void *bp)
238 return ap < bp ? -1 : (ap > bp ? 1 : 0);
241 static int cmpint(const void *ap, const void *bp)
243 return PTR2INT(ap) - PTR2INT(bp);
247 /* ---- left-leaning 2-3 red-black implementation ---- */
249 /* helper prototypes */
250 static int is_red(struct rbnode *tree);
251 static void color_flip(struct rbnode *tree);
252 static struct rbnode *rot_left(struct rbnode *a);
253 static struct rbnode *rot_right(struct rbnode *a);
254 static struct rbnode *find_min(struct rbnode *tree);
255 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree);
256 /*static struct rbnode *move_red_right(struct rbnode *tree);*/
257 static struct rbnode *move_red_left(struct rbnode *tree);
258 static struct rbnode *fix_up(struct rbnode *tree);
260 static int count_nodes(struct rbnode *node)
265 return 1 + count_nodes(node->left) + count_nodes(node->right);
268 static void del_tree(struct rbnode *node, rb_del_func_t delfunc, void *cls)
273 del_tree(node->left, delfunc, cls);
274 del_tree(node->right, delfunc, cls);
282 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data)
287 struct rbnode *node = rb->alloc(sizeof *node);
291 node->left = node->right = 0;
295 cmp = rb->cmp(key, tree->key);
298 tree->left = insert(rb, tree->left, key, data);
300 tree->right = insert(rb, tree->right, key, data);
305 /* fix right-leaning reds */
306 if(is_red(tree->right)) {
307 tree = rot_left(tree);
309 /* fix two reds in a row */
310 if(is_red(tree->left) && is_red(tree->left->left)) {
311 tree = rot_right(tree);
314 /* if 4-node, split it by color inversion */
315 if(is_red(tree->left) && is_red(tree->right)) {
322 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key)
330 cmp = rb->cmp(key, tree->key);
333 if(!is_red(tree->left) && !is_red(tree->left->left)) {
334 tree = move_red_left(tree);
336 tree->left = delete(rb, tree->left, key);
338 /* need reds on the right */
339 if(is_red(tree->left)) {
340 tree = rot_right(tree);
343 /* found it at the bottom (XXX what certifies left is null?) */
344 if(cmp == 0 && !tree->right) {
346 rb->del(tree, rb->del_cls);
352 if(!is_red(tree->right) && !is_red(tree->right->left)) {
353 tree = move_red_left(tree);
356 if(key == tree->key) {
357 struct rbnode *rmin = find_min(tree->right);
358 tree->key = rmin->key;
359 tree->data = rmin->data;
360 tree->right = del_min(rb, tree->right);
362 tree->right = delete(rb, tree->right, key);
369 /*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key)
376 if((cmp = rb->cmp(key, node->key)) == 0) {
379 return find(rb, cmp < 0 ? node->left : node->right, key);
382 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls)
387 traverse(node->left, func, cls);
389 traverse(node->right, func, cls);
394 static int is_red(struct rbnode *tree)
396 return tree && tree->red;
399 static void color_flip(struct rbnode *tree)
401 tree->red = !tree->red;
402 tree->left->red = !tree->left->red;
403 tree->right->red = !tree->right->red;
406 static struct rbnode *rot_left(struct rbnode *a)
408 struct rbnode *b = a->right;
416 static struct rbnode *rot_right(struct rbnode *a)
418 struct rbnode *b = a->left;
426 static struct rbnode *find_min(struct rbnode *tree)
437 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree)
441 rb->del(tree->left, rb->del_cls);
443 rb->free(tree->left);
447 /* make sure we've got red (3/4-nodes) at the left side so we can delete at the bottom */
448 if(!is_red(tree->left) && !is_red(tree->left->left)) {
449 tree = move_red_left(tree);
451 tree->left = del_min(rb, tree->left);
453 /* fix right-reds, red-reds, and split 4-nodes on the way up */
458 /* push a red link on this node to the right */
459 static struct rbnode *move_red_right(struct rbnode *tree)
461 /* flipping it makes both children go red, so we have a red to the right */
464 /* if after the flip we've got a red-red situation to the left, fix it */
465 if(is_red(tree->left->left)) {
466 tree = rot_right(tree);
473 /* push a red link on this node to the left */
474 static struct rbnode *move_red_left(struct rbnode *tree)
476 /* flipping it makes both children go red, so we have a red to the left */
479 /* if after the flip we've got a red-red on the right-left, fix it */
480 if(is_red(tree->right->left)) {
481 tree->right = rot_right(tree->right);
482 tree = rot_left(tree);
488 static struct rbnode *fix_up(struct rbnode *tree)
490 /* fix right-leaning */
491 if(is_red(tree->right)) {
492 tree = rot_left(tree);
494 /* change invalid red-red pairs into a proper 4-node */
495 if(is_red(tree->left) && is_red(tree->left->left)) {
496 tree = rot_right(tree);
499 if(is_red(tree->left) && is_red(tree->right)) {