2 avl_tree.c -- avl_ tree and linked list convenience
3 Copyright (C) 1998 Michael H. Buselli
4 2000-2003 Ivo Timmermans <ivo@o2w.nl>,
5 2000-2003 Guus Sliepen <guus@sliepen.eu.org>
6 2000-2003 Wessel Dankers <wsl@nl.linux.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
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 Original AVL tree library by Michael H. Buselli <cosine@cosine.org>.
24 Modified 2000-11-28 by Wessel Dankers <wsl@nl.linux.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://tinc.nl.linux.org/) by
30 Guus Sliepen <guus@sliepen.eu.org>.
32 $Id: avl_tree.c,v 1.1.2.15 2003/07/12 17:48:38 guus Exp $
42 #define AVL_NODE_COUNT(n) ((n) ? (n)->count : 0)
43 #define AVL_L_COUNT(n) (AVL_NODE_COUNT((n)->left))
44 #define AVL_R_COUNT(n) (AVL_NODE_COUNT((n)->right))
45 #define AVL_CALC_COUNT(n) (AVL_L_COUNT(n) + AVL_R_COUNT(n) + 1)
49 #define AVL_NODE_DEPTH(n) ((n) ? (n)->depth : 0)
50 #define L_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->left))
51 #define R_AVL_DEPTH(n) (AVL_NODE_DEPTH((n)->right))
52 #define AVL_CALC_DEPTH(n) ((L_AVL_DEPTH(n)>R_AVL_DEPTH(n)?L_AVL_DEPTH(n):R_AVL_DEPTH(n)) + 1)
56 static int lg(unsigned int u) __attribute__ ((const));
58 static int lg(unsigned int u)
92 /* Internal helper functions */
94 static int avl_check_balance(avl_node_t *node)
99 d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
101 return d < -1 ? -1 : d > 1 ? 1 : 0;
104 * d = lg(AVL_R_COUNT(node)) - lg(AVL_L_COUNT(node));
105 * d = d<-1?-1:d>1?1:0;
109 pl = lg(AVL_L_COUNT(node));
110 r = AVL_R_COUNT(node);
115 if(pl < 2 || r >> pl - 2)
122 static void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
127 avl_node_t **superparent;
132 parent = node->parent;
136 parent->left ? &parent->left : &parent->right : &tree->root;
138 switch (avl_check_balance(node)) {
142 if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
144 if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
146 node->left = child->right;
148 node->left->parent = node;
151 node->parent = child;
152 *superparent = child;
153 child->parent = parent;
155 node->count = AVL_CALC_COUNT(node);
156 child->count = AVL_CALC_COUNT(child);
159 node->depth = AVL_CALC_DEPTH(node);
160 child->depth = AVL_CALC_DEPTH(child);
163 gchild = child->right;
164 node->left = gchild->right;
167 node->left->parent = node;
168 child->right = gchild->left;
171 child->right->parent = child;
172 gchild->right = node;
175 gchild->right->parent = gchild;
176 gchild->left = child;
179 gchild->left->parent = gchild;
180 *superparent = gchild;
182 gchild->parent = parent;
184 node->count = AVL_CALC_COUNT(node);
185 child->count = AVL_CALC_COUNT(child);
186 gchild->count = AVL_CALC_COUNT(gchild);
189 node->depth = AVL_CALC_DEPTH(node);
190 child->depth = AVL_CALC_DEPTH(child);
191 gchild->depth = AVL_CALC_DEPTH(gchild);
199 if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
201 if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
203 node->right = child->left;
205 node->right->parent = node;
207 node->parent = child;
208 *superparent = child;
209 child->parent = parent;
211 node->count = AVL_CALC_COUNT(node);
212 child->count = AVL_CALC_COUNT(child);
215 node->depth = AVL_CALC_DEPTH(node);
216 child->depth = AVL_CALC_DEPTH(child);
219 gchild = child->left;
220 node->right = gchild->left;
223 node->right->parent = node;
224 child->left = gchild->right;
227 child->left->parent = child;
231 gchild->left->parent = gchild;
232 gchild->right = child;
235 gchild->right->parent = gchild;
237 *superparent = gchild;
238 gchild->parent = parent;
240 node->count = AVL_CALC_COUNT(node);
241 child->count = AVL_CALC_COUNT(child);
242 gchild->count = AVL_CALC_COUNT(gchild);
245 node->depth = AVL_CALC_DEPTH(node);
246 child->depth = AVL_CALC_DEPTH(child);
247 gchild->depth = AVL_CALC_DEPTH(gchild);
254 node->count = AVL_CALC_COUNT(node);
257 node->depth = AVL_CALC_DEPTH(node);
264 /* (De)constructors */
266 avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete)
270 tree = xmalloc_and_zero(sizeof(avl_tree_t));
271 tree->compare = compare;
272 tree->delete = delete;
277 void avl_free_tree(avl_tree_t *tree)
282 avl_node_t *avl_alloc_node(void)
284 return (avl_node_t *)xmalloc_and_zero(sizeof(avl_node_t));
287 void avl_free_node(avl_tree_t *tree, avl_node_t *node)
289 if(node->data && tree->delete)
290 tree->delete(node->data);
297 void *avl_search(const avl_tree_t *tree, const void *data)
301 node = avl_search_node(tree, data);
303 return node ? node->data : NULL;
306 void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result)
310 node = avl_search_closest_node(tree, data, result);
312 return node ? node->data : NULL;
315 void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data)
319 node = avl_search_closest_smaller_node(tree, data);
321 return node ? node->data : NULL;
324 void *avl_search_closest_greater(const avl_tree_t *tree, const void *data)
328 node = avl_search_closest_greater_node(tree, data);
330 return node ? node->data : NULL;
333 avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data)
338 node = avl_search_closest_node(tree, data, &result);
340 return result ? NULL : node;
343 avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
358 c = tree->compare(data, node->data);
386 avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
392 node = avl_search_closest_node(tree, data, &result);
400 avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
406 node = avl_search_closest_node(tree, data, &result);
414 /* Insertion and deletion */
416 avl_node_t *avl_insert(avl_tree_t *tree, void *data)
418 avl_node_t *closest, *new;
422 new = avl_alloc_node();
424 avl_insert_top(tree, new);
426 closest = avl_search_closest_node(tree, data, &result);
430 new = avl_alloc_node();
432 avl_insert_before(tree, closest, new);
436 new = avl_alloc_node();
438 avl_insert_after(tree, closest, new);
456 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
462 avl_insert_top(tree, node);
464 closest = avl_search_closest_node(tree, node->data, &result);
468 avl_insert_before(tree, closest, node);
472 avl_insert_after(tree, closest, node);
490 void avl_insert_top(avl_tree_t *tree, avl_node_t *node)
492 node->prev = node->next = node->parent = NULL;
493 tree->head = tree->tail = tree->root = node;
496 void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
501 avl_insert_after(tree, tree->tail, node);
503 avl_insert_top(tree, node);
508 node->parent = before;
509 node->prev = before->prev;
512 avl_insert_after(tree, before->prev, node);
517 before->prev->next = node;
524 avl_rebalance(tree, before);
527 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
531 avl_insert_before(tree, tree->head, node);
533 avl_insert_top(tree, node);
538 avl_insert_before(tree, after->next, node);
543 node->parent = after;
544 node->next = after->next;
547 after->next->prev = node;
554 avl_rebalance(tree, after);
557 avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
561 node = avl_search_node(tree, data);
564 avl_unlink_node(tree, node);
569 void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
572 avl_node_t **superparent;
573 avl_node_t *subst, *left, *right;
577 node->prev->next = node->next;
579 tree->head = node->next;
581 node->next->prev = node->prev;
583 tree->tail = node->prev;
585 parent = node->parent;
589 parent->left ? &parent->left : &parent->right : &tree->root;
594 *superparent = right;
597 right->parent = parent;
602 left->parent = parent;
610 balnode = subst->parent;
611 balnode->right = subst->left;
614 balnode->right->parent = balnode;
617 left->parent = subst;
620 subst->right = right;
621 subst->parent = parent;
622 right->parent = subst;
623 *superparent = subst;
626 avl_rebalance(tree, balnode);
628 node->next = node->prev = node->parent = node->left = node->right = NULL;
638 void avl_delete_node(avl_tree_t *tree, avl_node_t *node)
640 avl_unlink_node(tree, node);
641 avl_free_node(tree, node);
644 void avl_delete(avl_tree_t *tree, void *data)
648 node = avl_search_node(tree, data);
651 avl_delete_node(tree, node);
654 /* Fast tree cleanup */
656 void avl_delete_tree(avl_tree_t *tree)
658 avl_node_t *node, *next;
660 for(node = tree->root; node; node = next) {
662 avl_free_node(tree, node);
670 void avl_foreach(avl_tree_t *tree, avl_action_t action)
672 avl_node_t *node, *next;
674 for(node = tree->head; node; node = next) {
680 void avl_foreach_node(avl_tree_t *tree, avl_action_t action)
682 avl_node_t *node, *next;
684 for(node = tree->head; node; node = next) {
693 unsigned int avl_count(avl_tree_t *tree)
695 return AVL_NODE_COUNT(tree->root);
698 avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index)
706 c = AVL_L_COUNT(node);
710 } else if(index > c) {
721 unsigned int avl_index(const avl_node_t *node)
726 index = AVL_L_COUNT(node);
728 while((next = node->parent)) {
729 if(node == next->right)
730 index += AVL_L_COUNT(next) + 1;
738 unsigned int avl_depth(avl_tree_t *tree)
740 return AVL_NODE_DEPTH(tree->root);