2 avl_tree.c -- avl_ tree and linked list convenience
3 Copyright (C) 1998 Michael H. Buselli
4 2000,2001 Ivo Timmermans <ivo@o2w.nl>,
5 2000,2001 Guus Sliepen <guus@sliepen.eu.org>
6 2000,2001 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.13 2003/06/11 19:39:02 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 int lg(unsigned int u)
90 /* Internal helper functions */
92 int avl_check_balance(avl_node_t *node)
97 d = R_AVL_DEPTH(node) - L_AVL_DEPTH(node);
99 return d < -1 ? -1 : d > 1 ? 1 : 0;
102 * d = lg(AVL_R_COUNT(node)) - lg(AVL_L_COUNT(node));
103 * d = d<-1?-1:d>1?1:0;
107 pl = lg(AVL_L_COUNT(node));
108 r = AVL_R_COUNT(node);
113 if(pl < 2 || r >> pl - 2)
120 void avl_rebalance(avl_tree_t *tree, avl_node_t *node)
125 avl_node_t **superparent;
130 parent = node->parent;
134 parent->left ? &parent->left : &parent->right : &tree->root;
136 switch (avl_check_balance(node)) {
140 if(L_AVL_DEPTH(child) >= R_AVL_DEPTH(child)) {
142 if(AVL_L_COUNT(child) >= AVL_R_COUNT(child)) {
144 node->left = child->right;
146 node->left->parent = node;
149 node->parent = child;
150 *superparent = child;
151 child->parent = parent;
153 node->count = AVL_CALC_COUNT(node);
154 child->count = AVL_CALC_COUNT(child);
157 node->depth = AVL_CALC_DEPTH(node);
158 child->depth = AVL_CALC_DEPTH(child);
161 gchild = child->right;
162 node->left = gchild->right;
165 node->left->parent = node;
166 child->right = gchild->left;
169 child->right->parent = child;
170 gchild->right = node;
173 gchild->right->parent = gchild;
174 gchild->left = child;
177 gchild->left->parent = gchild;
178 *superparent = gchild;
180 gchild->parent = parent;
182 node->count = AVL_CALC_COUNT(node);
183 child->count = AVL_CALC_COUNT(child);
184 gchild->count = AVL_CALC_COUNT(gchild);
187 node->depth = AVL_CALC_DEPTH(node);
188 child->depth = AVL_CALC_DEPTH(child);
189 gchild->depth = AVL_CALC_DEPTH(gchild);
197 if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
199 if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
201 node->right = child->left;
203 node->right->parent = node;
205 node->parent = child;
206 *superparent = child;
207 child->parent = parent;
209 node->count = AVL_CALC_COUNT(node);
210 child->count = AVL_CALC_COUNT(child);
213 node->depth = AVL_CALC_DEPTH(node);
214 child->depth = AVL_CALC_DEPTH(child);
217 gchild = child->left;
218 node->right = gchild->left;
221 node->right->parent = node;
222 child->left = gchild->right;
225 child->left->parent = child;
229 gchild->left->parent = gchild;
230 gchild->right = child;
233 gchild->right->parent = gchild;
235 *superparent = gchild;
236 gchild->parent = parent;
238 node->count = AVL_CALC_COUNT(node);
239 child->count = AVL_CALC_COUNT(child);
240 gchild->count = AVL_CALC_COUNT(gchild);
243 node->depth = AVL_CALC_DEPTH(node);
244 child->depth = AVL_CALC_DEPTH(child);
245 gchild->depth = AVL_CALC_DEPTH(gchild);
252 node->count = AVL_CALC_COUNT(node);
255 node->depth = AVL_CALC_DEPTH(node);
262 /* (De)constructors */
264 avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete)
268 tree = xmalloc_and_zero(sizeof(avl_tree_t));
269 tree->compare = compare;
270 tree->delete = delete;
275 void avl_free_tree(avl_tree_t *tree)
280 avl_node_t *avl_alloc_node(void)
282 return (avl_node_t *)xmalloc_and_zero(sizeof(avl_node_t));
285 void avl_free_node(avl_tree_t *tree, avl_node_t *node)
287 if(node->data && tree->delete)
288 tree->delete(node->data);
295 void *avl_search(const avl_tree_t *tree, const void *data)
299 node = avl_search_node(tree, data);
301 return node ? node->data : NULL;
304 void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result)
308 node = avl_search_closest_node(tree, data, result);
310 return node ? node->data : NULL;
313 void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data)
317 node = avl_search_closest_smaller_node(tree, data);
319 return node ? node->data : NULL;
322 void *avl_search_closest_greater(const avl_tree_t *tree, const void *data)
326 node = avl_search_closest_greater_node(tree, data);
328 return node ? node->data : NULL;
331 avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data)
336 node = avl_search_closest_node(tree, data, &result);
338 return result ? NULL : node;
341 avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
356 c = tree->compare(data, node->data);
384 avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
390 node = avl_search_closest_node(tree, data, &result);
398 avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
404 node = avl_search_closest_node(tree, data, &result);
412 /* Insertion and deletion */
414 avl_node_t *avl_insert(avl_tree_t *tree, void *data)
416 avl_node_t *closest, *new;
420 new = avl_alloc_node();
422 avl_insert_top(tree, new);
424 closest = avl_search_closest_node(tree, data, &result);
428 new = avl_alloc_node();
430 avl_insert_before(tree, closest, new);
434 new = avl_alloc_node();
436 avl_insert_after(tree, closest, new);
454 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
460 avl_insert_top(tree, node);
462 closest = avl_search_closest_node(tree, node->data, &result);
466 avl_insert_before(tree, closest, node);
470 avl_insert_after(tree, closest, node);
488 void avl_insert_top(avl_tree_t *tree, avl_node_t *node)
490 node->prev = node->next = node->parent = NULL;
491 tree->head = tree->tail = tree->root = node;
494 void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
499 avl_insert_after(tree, tree->tail, node);
501 avl_insert_top(tree, node);
506 node->parent = before;
507 node->prev = before->prev;
510 avl_insert_after(tree, before->prev, node);
515 before->prev->next = node;
522 avl_rebalance(tree, before);
525 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
529 avl_insert_before(tree, tree->head, node);
531 avl_insert_top(tree, node);
536 avl_insert_before(tree, after->next, node);
541 node->parent = after;
542 node->next = after->next;
545 after->next->prev = node;
552 avl_rebalance(tree, after);
555 avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
559 node = avl_search_node(tree, data);
562 avl_unlink_node(tree, node);
567 void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
570 avl_node_t **superparent;
571 avl_node_t *subst, *left, *right;
575 node->prev->next = node->next;
577 tree->head = node->next;
579 node->next->prev = node->prev;
581 tree->tail = node->prev;
583 parent = node->parent;
587 parent->left ? &parent->left : &parent->right : &tree->root;
592 *superparent = right;
595 right->parent = parent;
600 left->parent = parent;
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->root; node; node = next) {
660 avl_free_node(tree, node);
668 void avl_foreach(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(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(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(avl_tree_t *tree)
738 return AVL_NODE_DEPTH(tree->root);