void avl() { printf(">> AVL\n"); avl_tree_t tree; avl_init(&tree, NULL); test_avl_t a = { 1 }; test_avl_t b = { 2 }; test_avl_t c = { 3 }; avl_insert(&tree, &a.node, &test_avl_compare); avl_insert(&tree, &b.node, &test_avl_compare); avl_insert(&tree, &c.node, &test_avl_compare); // Display them avl_node_t *node = avl_head(&tree); while (node) { avl_node_t *next = avl_next(node); avl_node_t *prev = avl_prev(node); test_avl_t *c = avl_ref(node, test_avl_t, node); test_avl_t *n = next ? avl_ref(next, test_avl_t, node) : NULL; test_avl_t *p = prev ? avl_ref(prev, test_avl_t, node) : NULL; printf("current: %d, next: %d, prev: %d\n", c->number, n ? n->number : -1, p ? p->number : -1); node = next; } }
struct avl_node* avl_search_smaller(struct avl_tree *tree, struct avl_node *node, avl_cmp_func *func) // if an exact match does not exist, // return greatest node smaller than NODE { struct avl_node *p = tree->root; struct avl_node *pp = NULL; int cmp; while(p) { cmp = func(p, node, tree->aux); pp = p; if (cmp > 0) { p = p->left; }else if (cmp < 0){ p = p->right; }else { // search success return p; } } if (!pp) { return pp; } cmp = func(pp, node, tree->aux); if (cmp < 0) { return pp; }else{ return avl_prev(pp); } }