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)
133 if(abs((uintptr_t)&stack_var - (uintptr_t)key) < 0x80000) {
134 fprintf(stderr, "rb_insert warning: key seems to point to the stack\n");
137 rb->root = insert(rb, rb->root, key, data);
142 int rb_inserti(struct rbtree *rb, int key, void *data)
144 rb->root = insert(rb, rb->root, INT2PTR(key), data);
150 int rb_delete(struct rbtree *rb, void *key)
152 if((rb->root = delete(rb, rb->root, key))) {
158 int rb_deletei(struct rbtree *rb, int key)
160 if((rb->root = delete(rb, rb->root, INT2PTR(key)))) {
167 struct rbnode *rb_find(struct rbtree *rb, void *key)
169 struct rbnode *node = rb->root;
172 int cmp = rb->cmp(key, node->key);
176 node = cmp < 0 ? node->left : node->right;
181 struct rbnode *rb_findi(struct rbtree *rb, int key)
183 return rb_find(rb, INT2PTR(key));
187 void rb_foreach(struct rbtree *rb, void (*func)(struct rbnode*, void*), void *cls)
189 traverse(rb->root, func, cls);
193 struct rbnode *rb_root(struct rbtree *rb)
198 void rb_begin(struct rbtree *rb)
204 #define push(sp, x) ((x)->next = (sp), (sp) = (x))
205 #define pop(sp) ((sp) = (sp)->next)
208 struct rbnode *rb_next(struct rbtree *rb)
210 struct rbnode *res = 0;
212 while(rb->rstack || rb->iter) {
214 push(rb->rstack, rb->iter);
215 rb->iter = rb->iter->left;
217 rb->iter = top(rb->rstack);
220 rb->iter = rb->iter->right;
227 void *rb_node_key(struct rbnode *node)
229 return node ? node->key : 0;
232 int rb_node_keyi(struct rbnode *node)
234 return node ? PTR2INT(node->key) : 0;
237 void *rb_node_data(struct rbnode *node)
239 return node ? node->data : 0;
242 void rb_node_setdata(struct rbnode *node, void *data)
247 static int cmpaddr(const void *ap, const void *bp)
249 return ap < bp ? -1 : (ap > bp ? 1 : 0);
252 static int cmpint(const void *ap, const void *bp)
254 return PTR2INT(ap) - PTR2INT(bp);
258 /* ---- left-leaning 2-3 red-black implementation ---- */
260 /* helper prototypes */
261 static int is_red(struct rbnode *tree);
262 static void color_flip(struct rbnode *tree);
263 static struct rbnode *rot_left(struct rbnode *a);
264 static struct rbnode *rot_right(struct rbnode *a);
265 static struct rbnode *find_min(struct rbnode *tree);
266 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree);
267 /*static struct rbnode *move_red_right(struct rbnode *tree);*/
268 static struct rbnode *move_red_left(struct rbnode *tree);
269 static struct rbnode *fix_up(struct rbnode *tree);
271 static int count_nodes(struct rbnode *node)
276 return 1 + count_nodes(node->left) + count_nodes(node->right);
279 static void del_tree(struct rbnode *node, rb_del_func_t delfunc, void *cls)
284 del_tree(node->left, delfunc, cls);
285 del_tree(node->right, delfunc, cls);
293 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data)
298 struct rbnode *node = rb->alloc(sizeof *node);
302 node->left = node->right = 0;
306 cmp = rb->cmp(key, tree->key);
309 tree->left = insert(rb, tree->left, key, data);
311 tree->right = insert(rb, tree->right, key, data);
314 /* The key passed in was allocated in a way that would be cleaned by the
315 * user-supplied delete function. We can't just assign the data and ignore
316 * key in this case, or we'll leak memory. But we also can't make a dummy
317 * node and pass that to rb->del, because it might also expect to free data.
318 * So we must instead delete the existing node's contents, and use the new ones.
320 rb->del(tree, rb->del_cls);
326 /* fix right-leaning reds */
327 if(is_red(tree->right)) {
328 tree = rot_left(tree);
330 /* fix two reds in a row */
331 if(is_red(tree->left) && is_red(tree->left->left)) {
332 tree = rot_right(tree);
335 /* if 4-node, split it by color inversion */
336 if(is_red(tree->left) && is_red(tree->right)) {
343 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key)
351 cmp = rb->cmp(key, tree->key);
354 if(!is_red(tree->left) && !is_red(tree->left->left)) {
355 tree = move_red_left(tree);
357 tree->left = delete(rb, tree->left, key);
359 /* need reds on the right */
360 if(is_red(tree->left)) {
361 tree = rot_right(tree);
364 /* found it at the bottom (XXX what certifies left is null?) */
365 if(cmp == 0 && !tree->right) {
367 rb->del(tree, rb->del_cls);
373 if(!is_red(tree->right) && !is_red(tree->right->left)) {
374 tree = move_red_left(tree);
377 if(key == tree->key) {
378 struct rbnode *rmin = find_min(tree->right);
379 tree->key = rmin->key;
380 tree->data = rmin->data;
381 tree->right = del_min(rb, tree->right);
383 tree->right = delete(rb, tree->right, key);
390 /*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key)
397 if((cmp = rb->cmp(key, node->key)) == 0) {
400 return find(rb, cmp < 0 ? node->left : node->right, key);
403 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls)
408 traverse(node->left, func, cls);
410 traverse(node->right, func, cls);
415 static int is_red(struct rbnode *tree)
417 return tree && tree->red;
420 static void color_flip(struct rbnode *tree)
422 tree->red = !tree->red;
423 tree->left->red = !tree->left->red;
424 tree->right->red = !tree->right->red;
427 static struct rbnode *rot_left(struct rbnode *a)
429 struct rbnode *b = a->right;
437 static struct rbnode *rot_right(struct rbnode *a)
439 struct rbnode *b = a->left;
447 static struct rbnode *find_min(struct rbnode *tree)
458 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree)
462 rb->del(tree->left, rb->del_cls);
464 rb->free(tree->left);
468 /* make sure we've got red (3/4-nodes) at the left side so we can delete at the bottom */
469 if(!is_red(tree->left) && !is_red(tree->left->left)) {
470 tree = move_red_left(tree);
472 tree->left = del_min(rb, tree->left);
474 /* fix right-reds, red-reds, and split 4-nodes on the way up */
479 /* push a red link on this node to the right */
480 static struct rbnode *move_red_right(struct rbnode *tree)
482 /* flipping it makes both children go red, so we have a red to the right */
485 /* if after the flip we've got a red-red situation to the left, fix it */
486 if(is_red(tree->left->left)) {
487 tree = rot_right(tree);
494 /* push a red link on this node to the left */
495 static struct rbnode *move_red_left(struct rbnode *tree)
497 /* flipping it makes both children go red, so we have a red to the left */
500 /* if after the flip we've got a red-red on the right-left, fix it */
501 if(is_red(tree->right->left)) {
502 tree->right = rot_right(tree->right);
503 tree = rot_left(tree);
509 static struct rbnode *fix_up(struct rbnode *tree)
511 /* fix right-leaning */
512 if(is_red(tree->right)) {
513 tree = rot_left(tree);
515 /* change invalid red-red pairs into a proper 4-node */
516 if(is_red(tree->left) && is_red(tree->left->left)) {
517 tree = rot_right(tree);
520 if(is_red(tree->left) && is_red(tree->right)) {