2 * collectd - src/utils_avltree.c
3 * Copyright (C) 2006,2007 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
30 #include "utils_avltree.h"
32 #define BALANCE(n) ((((n)->left == NULL) ? 0 : (n)->left->height) \
33 - (((n)->right == NULL) ? 0 : (n)->right->height))
44 struct c_avl_node_s *left;
45 struct c_avl_node_s *right;
46 struct c_avl_node_s *parent;
48 typedef struct c_avl_node_s c_avl_node_t;
53 int (*compare) (const void *, const void *);
56 struct c_avl_iterator_s
66 static void verify_tree (c_avl_node_t *n)
71 verify_tree (n->left);
72 verify_tree (n->right);
74 assert ((BALANCE (n) >= -1) && (BALANCE (n) <= 1));
75 assert ((n->parent == NULL) || (n->parent->right == n) || (n->parent->left == n));
76 } /* void verify_tree */
78 # define verify_tree(n) /**/
81 static void free_node (c_avl_node_t *n)
94 static int calc_height (c_avl_node_t *n)
102 height_left = (n->left == NULL) ? 0 : n->left->height;
103 height_right = (n->right == NULL) ? 0 : n->right->height;
105 return (((height_left > height_right)
107 : height_right) + 1);
108 } /* int calc_height */
110 static c_avl_node_t *search (c_avl_tree_t *t, const void *key)
118 cmp = t->compare (key, n->key);
133 * / \ /_c\ ==> / a\ / \
135 * / a\ /_b\ /_b\ /_c\
138 static c_avl_node_t *rotate_right (c_avl_tree_t *t, c_avl_node_t *x)
156 assert ((p == NULL) || (p->left == x) || (p->right == x));
159 else if (p->left == x)
164 x->height = calc_height (x);
165 y->height = calc_height (y);
168 } /* void rotate_left */
174 * /_a\ / \ ==> / \ / c\
176 * /_b\ / c\ /_a\ /_b\
179 static c_avl_node_t *rotate_left (c_avl_tree_t *t, c_avl_node_t *x)
197 assert ((p == NULL) || (p->left == x) || (p->right == x));
200 else if (p->left == x)
205 x->height = calc_height (x);
206 y->height = calc_height (y);
209 } /* void rotate_left */
211 static c_avl_node_t *rotate_left_right (c_avl_tree_t *t, c_avl_node_t *x)
213 rotate_left (t, x->left);
214 return (rotate_right (t, x));
215 } /* void rotate_left_right */
217 static c_avl_node_t *rotate_right_left (c_avl_tree_t *t, c_avl_node_t *x)
219 rotate_right (t, x->right);
220 return (rotate_left (t, x));
221 } /* void rotate_right_left */
223 static void rebalance (c_avl_tree_t *t, c_avl_node_t *n)
231 assert ((b_top >= -2) && (b_top <= 2));
235 assert (n->right != NULL);
236 b_bottom = BALANCE (n->right);
237 assert ((b_bottom >= -1) || (b_bottom <= 1));
239 n = rotate_right_left (t, n);
241 n = rotate_left (t, n);
245 assert (n->left != NULL);
246 b_bottom = BALANCE (n->left);
247 assert ((b_bottom >= -1) || (b_bottom <= 1));
249 n = rotate_left_right (t, n);
251 n = rotate_right (t, n);
255 int height = calc_height (n);
256 if (height == n->height)
261 assert (n->height == calc_height (n));
264 } /* while (n != NULL) */
265 } /* void rebalance */
267 static c_avl_node_t *c_avl_node_next (c_avl_node_t *n)
269 c_avl_node_t *r; /* return node */
276 /* If we can't descent any further, we have to backtrack to the first
277 * parent that's bigger than we, i. e. who's _left_ child we are. */
278 if (n->right == NULL)
281 while ((r != NULL) && (r->parent != NULL))
289 /* n->right == NULL && r == NULL => t is root and has no next
290 * r->left != n => r->right = n => r->parent == NULL */
291 if ((r == NULL) || (r->left != n))
293 assert ((r == NULL) || (r->parent == NULL));
298 assert (r->left == n);
305 while (r->left != NULL)
310 } /* c_avl_node_t *c_avl_node_next */
312 static c_avl_node_t *c_avl_node_prev (c_avl_node_t *n)
314 c_avl_node_t *r; /* return node */
321 /* If we can't descent any further, we have to backtrack to the first
322 * parent that's smaller than we, i. e. who's _right_ child we are. */
326 while ((r != NULL) && (r->parent != NULL))
334 /* n->left == NULL && r == NULL => t is root and has no next
335 * r->right != n => r->left = n => r->parent == NULL */
336 if ((r == NULL) || (r->right != n))
338 assert ((r == NULL) || (r->parent == NULL));
343 assert (r->right == n);
350 while (r->right != NULL)
355 } /* c_avl_node_t *c_avl_node_prev */
357 static int _remove (c_avl_tree_t *t, c_avl_node_t *n)
359 assert ((t != NULL) && (n != NULL));
361 if ((n->left != NULL) && (n->right != NULL))
363 c_avl_node_t *r; /* replacement node */
364 if (BALANCE (n) > 0) /* left subtree is higher */
366 assert (n->left != NULL);
367 r = c_avl_node_prev (n);
370 else /* right subtree is higher */
372 assert (n->right != NULL);
373 r = c_avl_node_next (n);
376 assert ((r->left == NULL) || (r->right == NULL));
385 assert ((n->left == NULL) || (n->right == NULL));
387 if ((n->left == NULL) && (n->right == NULL))
389 /* Deleting a leave is easy */
390 if (n->parent == NULL)
392 assert (t->root == n);
397 assert ((n->parent->left == n)
398 || (n->parent->right == n));
399 if (n->parent->left == n)
400 n->parent->left = NULL;
402 n->parent->right = NULL;
404 rebalance (t, n->parent);
409 else if (n->left == NULL)
411 assert (BALANCE (n) == -1);
412 assert ((n->parent == NULL) || (n->parent->left == n) || (n->parent->right == n));
413 if (n->parent == NULL)
415 assert (t->root == n);
418 else if (n->parent->left == n)
420 n->parent->left = n->right;
424 n->parent->right = n->right;
426 n->right->parent = n->parent;
428 if (n->parent != NULL)
429 rebalance (t, n->parent);
434 else if (n->right == NULL)
436 assert (BALANCE (n) == 1);
437 assert ((n->parent == NULL) || (n->parent->left == n) || (n->parent->right == n));
438 if (n->parent == NULL)
440 assert (t->root == n);
443 else if (n->parent->left == n)
445 n->parent->left = n->left;
449 n->parent->right = n->left;
451 n->left->parent = n->parent;
453 if (n->parent != NULL)
454 rebalance (t, n->parent);
465 } /* void *_remove */
470 c_avl_tree_t *c_avl_create (int (*compare) (const void *, const void *))
477 if ((t = (c_avl_tree_t *) malloc (sizeof (c_avl_tree_t))) == NULL)
481 t->compare = compare;
486 void c_avl_destroy (c_avl_tree_t *t)
492 int c_avl_insert (c_avl_tree_t *t, void *key, void *value)
498 if ((new = (c_avl_node_t *) malloc (sizeof (c_avl_node_t))) == NULL)
517 cmp = t->compare (nptr->key, new->key);
526 if (nptr->right == NULL)
538 else /* if (cmp > 0) */
541 if (nptr->left == NULL)
555 verify_tree (t->root);
557 } /* int c_avl_insert */
559 int c_avl_remove (c_avl_tree_t *t, const void *key, void **rkey, void **rvalue)
575 status = _remove (t, n);
576 verify_tree (t->root);
578 } /* void *c_avl_remove */
580 int c_avl_get (c_avl_tree_t *t, const void *key, void **value)
596 int c_avl_pick (c_avl_tree_t *t, void **key, void **value)
601 if ((key == NULL) || (value == NULL))
607 while ((n->left != NULL) || (n->right != NULL))
609 int height_left = (n->left == NULL) ? 0 : n->left->height;
610 int height_right = (n->right == NULL) ? 0 : n->right->height;
612 if (height_left > height_right)
621 else if (p->left == n)
633 } /* int c_avl_pick */
635 c_avl_iterator_t *c_avl_get_iterator (c_avl_tree_t *t)
637 c_avl_iterator_t *iter;
642 iter = (c_avl_iterator_t *) malloc (sizeof (c_avl_iterator_t));
645 memset (iter, '\0', sizeof (c_avl_iterator_t));
649 } /* c_avl_iterator_t *c_avl_get_iterator */
651 int c_avl_iterator_next (c_avl_iterator_t *iter, void **key, void **value)
655 if ((iter == NULL) || (key == NULL) || (value == NULL))
658 if (iter->node == NULL)
660 for (n = iter->tree->root; n != NULL; n = n->left)
667 n = c_avl_node_next (iter->node);
678 } /* int c_avl_iterator_next */
680 int c_avl_iterator_prev (c_avl_iterator_t *iter, void **key, void **value)
684 if ((iter == NULL) || (key == NULL) || (value == NULL))
687 if (iter->node == NULL)
689 for (n = iter->tree->root; n != NULL; n = n->left)
690 if (n->right == NULL)
696 n = c_avl_node_prev (iter->node);
707 } /* int c_avl_iterator_prev */
709 void c_avl_iterator_destroy (c_avl_iterator_t *iter)