2 avl_tree.c -- avl_ tree and linked list convenience
3 Copyright (C) 1998 Michael H. Buselli
4 2000-2005 Ivo Timmermans,
5 2000-2006 Guus Sliepen <guus@tinc-vpn.org>
6 2000-2005 Wessel Dankers <wsl@tinc-vpn.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 Original AVL tree library by Michael H. Buselli <cosine@cosine.org>.
24 Modified 2000-11-28 by Wessel Dankers <wsl@tinc-vpn.org> to use counts
25 instead of depths, to add the ->next and ->prev and to generally obfuscate
26 the code. Mail me if you found a bug.
28 Cleaned up and incorporated some of the ideas from the red-black tree
29 library for inclusion into tinc (http://www.tinc-vpn.org/) by
30 Guus Sliepen <guus@tinc-vpn.org>.
39 #define AVL_NODE_COUNT(n) ((n) ? (n)->count : 0)
40 #define AVL_L_COUNT(n) (AVL_NODE_COUNT((n)->left))
41 #define AVL_R_COUNT(n) (AVL_NODE_COUNT((n)->right))
42 #define AVL_CALC_COUNT(n) (AVL_L_COUNT(n) + AVL_R_COUNT(n) + 1)
46 #define AVL_NODE_DEPTH(n) ((n) ? (n)->depth : 0)
47 #define L_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->left))
48 #define R_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->right))
49 #define AVL_CALC_DEPTH(n) ((L_AVL_DEPTH(n)>R_AVL_DEPTH(n)?L_AVL_DEPTH(n):R_AVL_DEPTH(n)) + 1)
53 static int lg(unsigned int u) __attribute__ ((__const__));
55 static int lg(unsigned int u)
89 /* Internal helper functions */
91 static int avl_check_balance(const avl_node_t *node)
96 d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
98 return d < -1 ? -1 : d > 1 ? 1 : 0;
101 * d = lg(AVL_R_COUNT(node)) - lg(AVL_L_COUNT(node));
102 * d = d<-1?-1:d>1?1:0;
106 pl = lg(AVL_L_COUNT(node));
107 r = AVL_R_COUNT(node);
112 if(pl < 2 || r >> pl - 2)
119 static void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
124 avl_node_t **superparent;
129 parent = node->parent;
133 parent->left ? &parent->left : &parent->right : &tree->root;
135 switch (avl_check_balance(node)) {
139 if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
141 if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
143 node->left = child->right;
145 node->left->parent = node;
148 node->parent = child;
149 *superparent = child;
150 child->parent = parent;
152 node->count = AVL_CALC_COUNT(node);
153 child->count = AVL_CALC_COUNT(child);
156 node->depth = AVL_CALC_DEPTH(node);
157 child->depth = AVL_CALC_DEPTH(child);
160 gchild = child->right;
161 node->left = gchild->right;
164 node->left->parent = node;
165 child->right = gchild->left;
168 child->right->parent = child;
169 gchild->right = node;
172 gchild->right->parent = gchild;
173 gchild->left = child;
176 gchild->left->parent = gchild;
177 *superparent = gchild;
179 gchild->parent = parent;
181 node->count = AVL_CALC_COUNT(node);
182 child->count = AVL_CALC_COUNT(child);
183 gchild->count = AVL_CALC_COUNT(gchild);
186 node->depth = AVL_CALC_DEPTH(node);
187 child->depth = AVL_CALC_DEPTH(child);
188 gchild->depth = AVL_CALC_DEPTH(gchild);
196 if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
198 if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
200 node->right = child->left;
202 node->right->parent = node;
204 node->parent = child;
205 *superparent = child;
206 child->parent = parent;
208 node->count = AVL_CALC_COUNT(node);
209 child->count = AVL_CALC_COUNT(child);
212 node->depth = AVL_CALC_DEPTH(node);
213 child->depth = AVL_CALC_DEPTH(child);
216 gchild = child->left;
217 node->right = gchild->left;
220 node->right->parent = node;
221 child->left = gchild->right;
224 child->left->parent = child;
228 gchild->left->parent = gchild;
229 gchild->right = child;
231 gchild->right->parent = gchild;
233 *superparent = gchild;
234 gchild->parent = parent;
236 node->count = AVL_CALC_COUNT(node);
237 child->count = AVL_CALC_COUNT(child);
238 gchild->count = AVL_CALC_COUNT(gchild);
241 node->depth = AVL_CALC_DEPTH(node);
242 child->depth = AVL_CALC_DEPTH(child);
243 gchild->depth = AVL_CALC_DEPTH(gchild);
250 node->count = AVL_CALC_COUNT(node);
253 node->depth = AVL_CALC_DEPTH(node);
260 /* (De)constructors */
262 avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete)
266 tree = xmalloc_and_zero(sizeof(avl_tree_t));
267 tree->compare = compare;
268 tree->delete = delete;
273 void avl_free_tree(avl_tree_t *tree)
278 avl_node_t *avl_alloc_node(void)
280 return xmalloc_and_zero(sizeof(avl_node_t));
283 void avl_free_node(avl_tree_t *tree, avl_node_t *node)
285 if(node->data && tree->delete)
286 tree->delete(node->data);
293 void *avl_search(const avl_tree_t *tree, const void *data)
297 node = avl_search_node(tree, data);
299 return node ? node->data : NULL;
302 void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result)
306 node = avl_search_closest_node(tree, data, result);
308 return node ? node->data : NULL;
311 void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data)
315 node = avl_search_closest_smaller_node(tree, data);
317 return node ? node->data : NULL;
320 void *avl_search_closest_greater(const avl_tree_t *tree, const void *data)
324 node = avl_search_closest_greater_node(tree, data);
326 return node ? node->data : NULL;
329 avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data)
334 node = avl_search_closest_node(tree, data, &result);
336 return result ? NULL : node;
339 avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
354 c = tree->compare(data, node->data);
382 avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
388 node = avl_search_closest_node(tree, data, &result);
396 avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
402 node = avl_search_closest_node(tree, data, &result);
410 /* Insertion and deletion */
412 avl_node_t *avl_insert(avl_tree_t *tree, void *data)
414 avl_node_t *closest, *new;
418 new = avl_alloc_node();
420 avl_insert_top(tree, new);
422 closest = avl_search_closest_node(tree, data, &result);
426 new = avl_alloc_node();
428 avl_insert_before(tree, closest, new);
432 new = avl_alloc_node();
434 avl_insert_after(tree, closest, new);
452 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
458 avl_insert_top(tree, node);
460 closest = avl_search_closest_node(tree, node->data, &result);
464 avl_insert_before(tree, closest, node);
468 avl_insert_after(tree, closest, node);
486 void avl_insert_top(avl_tree_t *tree, avl_node_t *node)
488 node->prev = node->next = node->parent = NULL;
489 tree->head = tree->tail = tree->root = node;
492 void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
497 avl_insert_after(tree, tree->tail, node);
499 avl_insert_top(tree, node);
504 node->parent = before;
505 node->prev = before->prev;
508 avl_insert_after(tree, before->prev, node);
513 before->prev->next = node;
520 avl_rebalance(tree, before);
523 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
527 avl_insert_before(tree, tree->head, node);
529 avl_insert_top(tree, node);
534 avl_insert_before(tree, after->next, node);
539 node->parent = after;
540 node->next = after->next;
543 after->next->prev = node;
550 avl_rebalance(tree, after);
553 avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
557 node = avl_search_node(tree, data);
560 avl_unlink_node(tree, node);
565 void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
568 avl_node_t **superparent;
569 avl_node_t *subst, *left, *right;
573 node->prev->next = node->next;
575 tree->head = node->next;
577 node->next->prev = node->prev;
579 tree->tail = node->prev;
581 parent = node->parent;
585 parent->left ? &parent->left : &parent->right : &tree->root;
590 *superparent = right;
593 right->parent = parent;
598 left->parent = parent;
602 if(!subst) // This only happens if node is not actually in a tree at all.
608 balnode = subst->parent;
609 balnode->right = subst->left;
612 balnode->right->parent = balnode;
615 left->parent = subst;
618 subst->right = right;
619 subst->parent = parent;
620 right->parent = subst;
621 *superparent = subst;
624 avl_rebalance(tree, balnode);
626 node->next = node->prev = node->parent = node->left = node->right = NULL;
636 void avl_delete_node(avl_tree_t *tree, avl_node_t *node)
638 avl_unlink_node(tree, node);
639 avl_free_node(tree, node);
642 void avl_delete(avl_tree_t *tree, void *data)
646 node = avl_search_node(tree, data);
649 avl_delete_node(tree, node);
652 /* Fast tree cleanup */
654 void avl_delete_tree(avl_tree_t *tree)
656 avl_node_t *node, *next;
658 for(node = tree->head; node; node = next) {
660 avl_free_node(tree, node);
668 void avl_foreach(const avl_tree_t *tree, avl_action_t action)
670 avl_node_t *node, *next;
672 for(node = tree->head; node; node = next) {
678 void avl_foreach_node(const avl_tree_t *tree, avl_action_t action)
680 avl_node_t *node, *next;
682 for(node = tree->head; node; node = next) {
691 unsigned int avl_count(const avl_tree_t *tree)
693 return AVL_NODE_COUNT(tree->root);
696 avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index)
704 c = AVL_L_COUNT(node);
708 } else if(index > c) {
719 unsigned int avl_index(const avl_node_t *node)
724 index = AVL_L_COUNT(node);
726 while((next = node->parent)) {
727 if(node == next->right)
728 index += AVL_L_COUNT(next) + 1;
736 unsigned int avl_depth(const avl_tree_t *tree)
738 return AVL_NODE_DEPTH(tree->root);