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;
171 gchild->right->parent = gchild;
172 gchild->left = child;
174 gchild->left->parent = gchild;
176 *superparent = gchild;
177 gchild->parent = parent;
179 node->count = AVL_CALC_COUNT(node);
180 child->count = AVL_CALC_COUNT(child);
181 gchild->count = AVL_CALC_COUNT(gchild);
184 node->depth = AVL_CALC_DEPTH(node);
185 child->depth = AVL_CALC_DEPTH(child);
186 gchild->depth = AVL_CALC_DEPTH(gchild);
194 if(R_AVL_DEPTH(child) >= L_AVL_DEPTH(child)) {
196 if(AVL_R_COUNT(child) >= AVL_L_COUNT(child)) {
198 node->right = child->left;
200 node->right->parent = node;
202 node->parent = child;
203 *superparent = child;
204 child->parent = parent;
206 node->count = AVL_CALC_COUNT(node);
207 child->count = AVL_CALC_COUNT(child);
210 node->depth = AVL_CALC_DEPTH(node);
211 child->depth = AVL_CALC_DEPTH(child);
214 gchild = child->left;
215 node->right = gchild->left;
218 node->right->parent = node;
219 child->left = gchild->right;
222 child->left->parent = child;
225 gchild->left->parent = gchild;
226 gchild->right = child;
228 gchild->right->parent = gchild;
230 *superparent = gchild;
231 gchild->parent = parent;
233 node->count = AVL_CALC_COUNT(node);
234 child->count = AVL_CALC_COUNT(child);
235 gchild->count = AVL_CALC_COUNT(gchild);
238 node->depth = AVL_CALC_DEPTH(node);
239 child->depth = AVL_CALC_DEPTH(child);
240 gchild->depth = AVL_CALC_DEPTH(gchild);
247 node->count = AVL_CALC_COUNT(node);
250 node->depth = AVL_CALC_DEPTH(node);
257 /* (De)constructors */
259 avl_tree_t *avl_alloc_tree(avl_compare_t compare, avl_action_t delete)
263 tree = xmalloc_and_zero(sizeof(avl_tree_t));
264 tree->compare = compare;
265 tree->delete = delete;
270 void avl_free_tree(avl_tree_t *tree)
275 avl_node_t *avl_alloc_node(void)
277 return xmalloc_and_zero(sizeof(avl_node_t));
280 void avl_free_node(avl_tree_t *tree, avl_node_t *node)
282 if(node->data && tree->delete)
283 tree->delete(node->data);
290 void *avl_search(const avl_tree_t *tree, const void *data)
294 node = avl_search_node(tree, data);
296 return node ? node->data : NULL;
299 void *avl_search_closest(const avl_tree_t *tree, const void *data, int *result)
303 node = avl_search_closest_node(tree, data, result);
305 return node ? node->data : NULL;
308 void *avl_search_closest_smaller(const avl_tree_t *tree, const void *data)
312 node = avl_search_closest_smaller_node(tree, data);
314 return node ? node->data : NULL;
317 void *avl_search_closest_greater(const avl_tree_t *tree, const void *data)
321 node = avl_search_closest_greater_node(tree, data);
323 return node ? node->data : NULL;
326 avl_node_t *avl_search_node(const avl_tree_t *tree, const void *data)
331 node = avl_search_closest_node(tree, data, &result);
333 return result ? NULL : node;
336 avl_node_t *avl_search_closest_node(const avl_tree_t *tree, const void *data,
351 c = tree->compare(data, node->data);
379 avl_node_t *avl_search_closest_smaller_node(const avl_tree_t *tree,
385 node = avl_search_closest_node(tree, data, &result);
393 avl_node_t *avl_search_closest_greater_node(const avl_tree_t *tree,
399 node = avl_search_closest_node(tree, data, &result);
407 /* Insertion and deletion */
409 avl_node_t *avl_insert(avl_tree_t *tree, void *data)
411 avl_node_t *closest, *new;
415 new = avl_alloc_node();
417 avl_insert_top(tree, new);
419 closest = avl_search_closest_node(tree, data, &result);
423 new = avl_alloc_node();
425 avl_insert_before(tree, closest, new);
429 new = avl_alloc_node();
431 avl_insert_after(tree, closest, new);
449 avl_node_t *avl_insert_node(avl_tree_t *tree, avl_node_t *node)
455 avl_insert_top(tree, node);
457 closest = avl_search_closest_node(tree, node->data, &result);
461 avl_insert_before(tree, closest, node);
465 avl_insert_after(tree, closest, node);
483 void avl_insert_top(avl_tree_t *tree, avl_node_t *node)
485 node->prev = node->next = node->parent = NULL;
486 tree->head = tree->tail = tree->root = node;
489 void avl_insert_before(avl_tree_t *tree, avl_node_t *before,
494 avl_insert_after(tree, tree->tail, node);
496 avl_insert_top(tree, node);
501 node->parent = before;
502 node->prev = before->prev;
505 avl_insert_after(tree, before->prev, node);
510 before->prev->next = node;
517 avl_rebalance(tree, before);
520 void avl_insert_after(avl_tree_t *tree, avl_node_t *after, avl_node_t *node)
524 avl_insert_before(tree, tree->head, node);
526 avl_insert_top(tree, node);
531 avl_insert_before(tree, after->next, node);
536 node->parent = after;
537 node->next = after->next;
540 after->next->prev = node;
547 avl_rebalance(tree, after);
550 avl_node_t *avl_unlink(avl_tree_t *tree, void *data)
554 node = avl_search_node(tree, data);
557 avl_unlink_node(tree, node);
562 void avl_unlink_node(avl_tree_t *tree, avl_node_t *node)
565 avl_node_t **superparent;
566 avl_node_t *subst, *left, *right;
570 node->prev->next = node->next;
572 tree->head = node->next;
574 node->next->prev = node->prev;
576 tree->tail = node->prev;
578 parent = node->parent;
582 parent->left ? &parent->left : &parent->right : &tree->root;
587 *superparent = right;
590 right->parent = parent;
595 left->parent = parent;
599 if(!subst) // This only happens if node is not actually in a tree at all.
605 balnode = subst->parent;
606 balnode->right = subst->left;
609 balnode->right->parent = balnode;
612 left->parent = subst;
615 subst->right = right;
616 subst->parent = parent;
617 right->parent = subst;
618 *superparent = subst;
621 avl_rebalance(tree, balnode);
623 node->next = node->prev = node->parent = node->left = node->right = NULL;
633 void avl_delete_node(avl_tree_t *tree, avl_node_t *node)
635 avl_unlink_node(tree, node);
636 avl_free_node(tree, node);
639 void avl_delete(avl_tree_t *tree, void *data)
643 node = avl_search_node(tree, data);
646 avl_delete_node(tree, node);
649 /* Fast tree cleanup */
651 void avl_delete_tree(avl_tree_t *tree)
653 avl_node_t *node, *next;
655 for(node = tree->head; node; node = next) {
657 avl_free_node(tree, node);
665 void avl_foreach(const avl_tree_t *tree, avl_action_t action)
667 avl_node_t *node, *next;
669 for(node = tree->head; node; node = next) {
675 void avl_foreach_node(const avl_tree_t *tree, avl_action_t action)
677 avl_node_t *node, *next;
679 for(node = tree->head; node; node = next) {
688 unsigned int avl_count(const avl_tree_t *tree)
690 return AVL_NODE_COUNT(tree->root);
693 avl_node_t *avl_get_node(const avl_tree_t *tree, unsigned int index)
701 c = AVL_L_COUNT(node);
705 } else if(index > c) {
716 unsigned int avl_index(const avl_node_t *node)
721 index = AVL_L_COUNT(node);
723 while((next = node->parent)) {
724 if(node == next->right)
725 index += AVL_L_COUNT(next) + 1;
733 unsigned int avl_depth(const avl_tree_t *tree)
735 return AVL_NODE_DEPTH(tree->root);