# HG changeset patch # User John Tsiombikas # Date 1318138254 -10800 # Node ID 53afe96233f2ad50c6813a5a7daa0fa0fc3e79bb # Parent 7205a7d8d3d68ef8f6b8abb378a9f8f358abf085 changed the tail-recursive find and find_min to loops diff -r 7205a7d8d3d6 -r 53afe96233f2 src/rbtree.c --- a/src/rbtree.c Sun Oct 09 07:55:51 2011 +0300 +++ b/src/rbtree.c Sun Oct 09 08:30:54 2011 +0300 @@ -26,7 +26,7 @@ static void del_tree(struct rbnode *node, void (*delfunc)(struct rbnode*, void*), void *cls); static struct rbnode *insert(struct rbtree *rb, struct rbnode *tree, void *key, void *data); static struct rbnode *delete(struct rbtree *rb, struct rbnode *tree, void *key); -static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key); +/*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key);*/ static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls); struct rbtree *rb_create(rb_cmp_func_t cmp_func) @@ -127,12 +127,21 @@ void *rb_find(struct rbtree *rb, void *key) { - return find(rb, rb->root, key); + struct rbnode *node = rb->root; + + while(node) { + int cmp = rb->cmp(key, node->key); + if(cmp == 0) { + return node; + } + node = cmp < 0 ? node->left : node->right; + } + return 0; } void *rb_findi(struct rbtree *rb, int key) { - return find(rb, rb->root, INT2PTR(key)); + return rb_find(rb, INT2PTR(key)); } @@ -322,7 +331,7 @@ return fix_up(tree); } -static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key) +/*static struct rbnode *find(struct rbtree *rb, struct rbnode *node, void *key) { int cmp; @@ -333,7 +342,7 @@ return node; } return find(rb, cmp < 0 ? node->left : node->right, key); -} +}*/ static void traverse(struct rbnode *node, void (*func)(struct rbnode*, void*), void *cls) { @@ -381,10 +390,15 @@ static struct rbnode *find_min(struct rbnode *tree) { - if(!tree || !tree->left) { - return tree; + struct rbnode *node; + + if(!tree) + return 0; + + while(node->left) { + node = node->left; } - return find_min(tree->left); + return node; } static struct rbnode *del_min(struct rbtree *rb, struct rbnode *tree) diff -r 7205a7d8d3d6 -r 53afe96233f2 test/test.c --- a/test/test.c Sun Oct 09 07:55:51 2011 +0300 +++ b/test/test.c Sun Oct 09 08:30:54 2011 +0300 @@ -102,7 +102,7 @@ glLoadIdentity(); if(input_buffer[0]) { - char *prompt = "select node to delete: "; + char *prompt = "select node: "; char *buf = alloca(strlen(prompt) + strlen(input_buffer)); glPushMatrix(); @@ -304,23 +304,12 @@ { static int wposx = -1, wposy; static char *inp_next; + static char op; switch(key) { case 27: exit(0); - case 'f': - case 'F': - if(wposx >= 0) { - glutPositionWindow(wposx, wposy); - wposx = -1; - } else { - wposx = glutGet(GLUT_WINDOW_X); - wposy = glutGet(GLUT_WINDOW_Y); - glutFullScreen(); - } - break; - case 'a': rb_inserti(tree, num_nodes++, 0); glutPostRedisplay(); @@ -339,6 +328,8 @@ break; case 'd': + case 'f': + op = key; inp_next = input_buffer; *inp_next++ = ' '; *inp_next = 0; @@ -383,8 +374,12 @@ } else if(!rb_findi(tree, x)) { fprintf(stderr, "%d not found in the tree\n", x); } else { - printf("deleting: %d\n", x); - rb_deletei(tree, x); + if(op == 'd') { + printf("deleting: %d\n", x); + rb_deletei(tree, x); + } else { + printf("%d found!\n", x); + } } input_buffer[0] = 0; glutPostRedisplay();