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 void rb_node_setdata(struct rbnode *node, void *data)
241 static int cmpaddr(const void *ap, const void *bp)
243 return ap < bp ? -1 : (ap > bp ? 1 : 0);
246 static int cmpint(const void *ap, const void *bp)
248 return PTR2INT(ap) - PTR2INT(bp);
252 /* ---- left-leaning 2-3 red-black implementation ---- */
254 /* helper prototypes */
255 static int is_red(struct rbnode *tree);
256 static void color_flip(struct rbnode *tree);
257 static struct rbnode *rot_left(struct rbnode *a);
258 static struct rbnode *rot_right(struct rbnode *a);
259 static struct rbnode *find_min(struct rbnode *tree);
260 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree);
261 /*static struct rbnode *move_red_right(struct rbnode *tree);*/
262 static struct rbnode *move_red_left(struct rbnode *tree);
263 static struct rbnode *fix_up(struct rbnode *tree);
265 static int count_nodes(struct rbnode *node)
270 return 1 + count_nodes(node->left) + count_nodes(node->right);
273 static void del_tree(struct rbnode *node, rb_del_func_t delfunc, void *cls)
278 del_tree(node->left, delfunc, cls);
279 del_tree(node->right, delfunc, cls);
287 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data)
292 struct rbnode *node = rb->alloc(sizeof *node);
296 node->left = node->right = 0;
300 cmp = rb->cmp(key, tree->key);
303 tree->left = insert(rb, tree->left, key, data);
305 tree->right = insert(rb, tree->right, key, data);
310 /* fix right-leaning reds */
311 if(is_red(tree->right)) {
312 tree = rot_left(tree);
314 /* fix two reds in a row */
315 if(is_red(tree->left) && is_red(tree->left->left)) {
316 tree = rot_right(tree);
319 /* if 4-node, split it by color inversion */
320 if(is_red(tree->left) && is_red(tree->right)) {
327 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key)
335 cmp = rb->cmp(key, tree->key);
338 if(!is_red(tree->left) && !is_red(tree->left->left)) {
339 tree = move_red_left(tree);
341 tree->left = delete(rb, tree->left, key);
343 /* need reds on the right */
344 if(is_red(tree->left)) {
345 tree = rot_right(tree);
348 /* found it at the bottom (XXX what certifies left is null?) */
349 if(cmp == 0 && !tree->right) {
351 rb->del(tree, rb->del_cls);
357 if(!is_red(tree->right) && !is_red(tree->right->left)) {
358 tree = move_red_left(tree);
361 if(key == tree->key) {
362 struct rbnode *rmin = find_min(tree->right);
363 tree->key = rmin->key;
364 tree->data = rmin->data;
365 tree->right = del_min(rb, tree->right);
367 tree->right = delete(rb, tree->right, key);
374 /*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key)
381 if((cmp = rb->cmp(key, node->key)) == 0) {
384 return find(rb, cmp < 0 ? node->left : node->right, key);
387 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls)
392 traverse(node->left, func, cls);
394 traverse(node->right, func, cls);
399 static int is_red(struct rbnode *tree)
401 return tree && tree->red;
404 static void color_flip(struct rbnode *tree)
406 tree->red = !tree->red;
407 tree->left->red = !tree->left->red;
408 tree->right->red = !tree->right->red;
411 static struct rbnode *rot_left(struct rbnode *a)
413 struct rbnode *b = a->right;
421 static struct rbnode *rot_right(struct rbnode *a)
423 struct rbnode *b = a->left;
431 static struct rbnode *find_min(struct rbnode *tree)
442 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree)
446 rb->del(tree->left, rb->del_cls);
448 rb->free(tree->left);
452 /* make sure we've got red (3/4-nodes) at the left side so we can delete at the bottom */
453 if(!is_red(tree->left) && !is_red(tree->left->left)) {
454 tree = move_red_left(tree);
456 tree->left = del_min(rb, tree->left);
458 /* fix right-reds, red-reds, and split 4-nodes on the way up */
463 /* push a red link on this node to the right */
464 static struct rbnode *move_red_right(struct rbnode *tree)
466 /* flipping it makes both children go red, so we have a red to the right */
469 /* if after the flip we've got a red-red situation to the left, fix it */
470 if(is_red(tree->left->left)) {
471 tree = rot_right(tree);
478 /* push a red link on this node to the left */
479 static struct rbnode *move_red_left(struct rbnode *tree)
481 /* flipping it makes both children go red, so we have a red to the left */
484 /* if after the flip we've got a red-red on the right-left, fix it */
485 if(is_red(tree->right->left)) {
486 tree->right = rot_right(tree->right);
487 tree = rot_left(tree);
493 static struct rbnode *fix_up(struct rbnode *tree)
495 /* fix right-leaning */
496 if(is_red(tree->right)) {
497 tree = rot_left(tree);
499 /* change invalid red-red pairs into a proper 4-node */
500 if(is_red(tree->left) && is_red(tree->left->left)) {
501 tree = rot_right(tree);
504 if(is_red(tree->left) && is_red(tree->right)) {