rbtree
diff src/rbtree.c @ 3:53afe96233f2
changed the tail-recursive find and find_min to loops
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Sun, 09 Oct 2011 08:30:54 +0300 |
parents | 6621337b6378 |
children | 56a08d00bb41 |
line diff
1.1 --- a/src/rbtree.c Sun Oct 09 07:55:51 2011 +0300 1.2 +++ b/src/rbtree.c Sun Oct 09 08:30:54 2011 +0300 1.3 @@ -26,7 +26,7 @@ 1.4 static void del_tree(struct rbnode *node, void (*delfunc)(struct rbnode*, void*), void *cls); 1.5 static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data); 1.6 static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key); 1.7 -static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key); 1.8 +/*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key);*/ 1.9 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls); 1.10 1.11 struct rbtree *rb_create(rb_cmp_func_t cmp_func) 1.12 @@ -127,12 +127,21 @@ 1.13 1.14 void *rb_find(struct rbtree *rb, void *key) 1.15 { 1.16 - return find(rb, rb->root, key); 1.17 + struct rbnode *node = rb->root; 1.18 + 1.19 + while(node) { 1.20 + int cmp = rb->cmp(key, node->key); 1.21 + if(cmp == 0) { 1.22 + return node; 1.23 + } 1.24 + node = cmp < 0 ? node->left : node->right; 1.25 + } 1.26 + return 0; 1.27 } 1.28 1.29 void *rb_findi(struct rbtree *rb, int key) 1.30 { 1.31 - return find(rb, rb->root, INT2PTR(key)); 1.32 + return rb_find(rb, INT2PTR(key)); 1.33 } 1.34 1.35 1.36 @@ -322,7 +331,7 @@ 1.37 return fix_up(tree); 1.38 } 1.39 1.40 -static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key) 1.41 +/*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key) 1.42 { 1.43 int cmp; 1.44 1.45 @@ -333,7 +342,7 @@ 1.46 return node; 1.47 } 1.48 return find(rb, cmp < 0 ? node->left : node->right, key); 1.49 -} 1.50 +}*/ 1.51 1.52 static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls) 1.53 { 1.54 @@ -381,10 +390,15 @@ 1.55 1.56 static struct rbnode *find_min(struct rbnode *tree) 1.57 { 1.58 - if(!tree || !tree->left) { 1.59 - return tree; 1.60 + struct rbnode *node; 1.61 + 1.62 + if(!tree) 1.63 + return 0; 1.64 + 1.65 + while(node->left) { 1.66 + node = node->left; 1.67 } 1.68 - return find_min(tree->left); 1.69 + return node; 1.70 } 1.71 1.72 static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree)