| 1 | /***************************************************************************** |
| 2 | |
| 3 | avl.c - Source code for the AVL-tree library. |
| 4 | |
| 5 | Copyright (C) 1998 Michael H. Buselli <[email protected]> |
| 6 | Copyright (C) 2000-2002 Wessel Dankers <[email protected]> |
| 7 | |
| 8 | This library is free software; you can redistribute it and/or |
| 9 | modify it under the terms of the GNU Lesser General Public |
| 10 | License as published by the Free Software Foundation; either |
| 11 | version 2.1 of the License, or (at your option) any later version. |
| 12 | |
| 13 | This library 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 GNU |
| 16 | Lesser General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU Lesser General Public |
| 19 | License along with this library; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | |
| 22 | Augmented AVL-tree. Original by Michael H. Buselli <[email protected]>. |
| 23 | |
| 24 | Modified by Wessel Dankers <[email protected]> to add a bunch of bloat to |
| 25 | the sourcecode, change the interface and squash a few bugs. |
| 26 | Mail him if you find new bugs. |
| 27 | |
| 28 | *****************************************************************************/ |
| 29 | |
| 30 | #include <stdio.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <errno.h> |
| 33 | #include "avl.h" |
| 34 | |
| 35 | static void avl_rebalance(avl_tree_t *, avl_node_t *); |
| 36 | |
| 37 | #ifdef AVL_COUNT |
| 38 | #define NODE_COUNT(n) ((n) ? (n)->count : 0) |
| 39 | #define L_COUNT(n) (NODE_COUNT((n)->left)) |
| 40 | #define R_COUNT(n) (NODE_COUNT((n)->right)) |
| 41 | #define CALC_COUNT(n) (L_COUNT(n) + R_COUNT(n) + 1) |
| 42 | #endif |
| 43 | |
| 44 | #ifdef AVL_DEPTH |
| 45 | #define NODE_DEPTH(n) ((n) ? (n)->depth : 0) |
| 46 | #define L_DEPTH(n) (NODE_DEPTH((n)->left)) |
| 47 | #define R_DEPTH(n) (NODE_DEPTH((n)->right)) |
| 48 | #define CALC_DEPTH(n) ((L_DEPTH(n)>R_DEPTH(n)?L_DEPTH(n):R_DEPTH(n)) + 1) |
| 49 | #endif |
| 50 | |
| 51 | #ifndef AVL_DEPTH |
| 52 | /* Also known as ffs() (from BSD) */ |
| 53 | static int lg(unsigned int u) { |
| 54 | int r = 1; |
| 55 | if(!u) return 0; |
| 56 | if(u & 0xffff0000) { u >>= 16; r += 16; } |
| 57 | if(u & 0x0000ff00) { u >>= 8; r += 8; } |
| 58 | if(u & 0x000000f0) { u >>= 4; r += 4; } |
| 59 | if(u & 0x0000000c) { u >>= 2; r += 2; } |
| 60 | if(u & 0x00000002) r++; |
| 61 | return r; |
| 62 | } |
| 63 | #endif |
| 64 | |
| 65 | static int avl_check_balance(avl_node_t *avlnode) { |
| 66 | #ifdef AVL_DEPTH |
| 67 | int d; |
| 68 | d = R_DEPTH(avlnode) - L_DEPTH(avlnode); |
| 69 | return d<-1?-1:d>1?1:0; |
| 70 | #else |
| 71 | /* int d; |
| 72 | * d = lg(R_COUNT(avlnode)) - lg(L_COUNT(avlnode)); |
| 73 | * d = d<-1?-1:d>1?1:0; |
| 74 | */ |
| 75 | #ifdef AVL_COUNT |
| 76 | int pl, r; |
| 77 | |
| 78 | pl = lg(L_COUNT(avlnode)); |
| 79 | r = R_COUNT(avlnode); |
| 80 | |
| 81 | if(r>>pl+1) |
| 82 | return 1; |
| 83 | if(pl<2 || r>>pl-2) |
| 84 | return 0; |
| 85 | return -1; |
| 86 | #else |
| 87 | #error No balancing possible. |
| 88 | #endif |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | #ifdef AVL_COUNT |
| 93 | unsigned int avl_count(const avl_tree_t *avltree) { |
| 94 | return NODE_COUNT(avltree->top); |
| 95 | } |
| 96 | |
| 97 | avl_node_t *avl_at(const avl_tree_t *avltree, unsigned int index) { |
| 98 | avl_node_t *avlnode; |
| 99 | unsigned int c; |
| 100 | |
| 101 | avlnode = avltree->top; |
| 102 | |
| 103 | while(avlnode) { |
| 104 | c = L_COUNT(avlnode); |
| 105 | |
| 106 | if(index < c) { |
| 107 | avlnode = avlnode->left; |
| 108 | } else if(index > c) { |
| 109 | avlnode = avlnode->right; |
| 110 | index -= c+1; |
| 111 | } else { |
| 112 | return avlnode; |
| 113 | } |
| 114 | } |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | unsigned int avl_index(const avl_node_t *avlnode) { |
| 119 | avl_node_t *next; |
| 120 | unsigned int c; |
| 121 | |
| 122 | c = L_COUNT(avlnode); |
| 123 | |
| 124 | while((next = avlnode->parent)) { |
| 125 | if(avlnode == next->right) |
| 126 | c += L_COUNT(next) + 1; |
| 127 | avlnode = next; |
| 128 | } |
| 129 | |
| 130 | return c; |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | int avl_search_closest(const avl_tree_t *avltree, const void *item, avl_node_t **avlnode) { |
| 135 | avl_node_t *node; |
| 136 | avl_compare_t cmp; |
| 137 | int c; |
| 138 | |
| 139 | if(!avlnode) |
| 140 | avlnode = &node; |
| 141 | |
| 142 | node = avltree->top; |
| 143 | |
| 144 | if(!node) |
| 145 | return *avlnode = NULL, 0; |
| 146 | |
| 147 | cmp = avltree->cmp; |
| 148 | |
| 149 | for(;;) { |
| 150 | c = cmp(item, node->item); |
| 151 | |
| 152 | if(c < 0) { |
| 153 | if(node->left) |
| 154 | node = node->left; |
| 155 | else |
| 156 | return *avlnode = node, -1; |
| 157 | } else if(c > 0) { |
| 158 | if(node->right) |
| 159 | node = node->right; |
| 160 | else |
| 161 | return *avlnode = node, 1; |
| 162 | } else { |
| 163 | return *avlnode = node, 0; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * avl_search: |
| 170 | * Return a pointer to a node with the given item in the tree. |
| 171 | * If no such item is in the tree, then NULL is returned. |
| 172 | */ |
| 173 | avl_node_t *avl_search(const avl_tree_t *avltree, const void *item) { |
| 174 | avl_node_t *node; |
| 175 | return avl_search_closest(avltree, item, &node) ? NULL : node; |
| 176 | } |
| 177 | |
| 178 | avl_tree_t *avl_init_tree(avl_tree_t *rc, avl_compare_t cmp, avl_freeitem_t freeitem) { |
| 179 | if(rc) { |
| 180 | rc->head = NULL; |
| 181 | rc->tail = NULL; |
| 182 | rc->top = NULL; |
| 183 | rc->cmp = cmp; |
| 184 | rc->freeitem = freeitem; |
| 185 | } |
| 186 | return rc; |
| 187 | } |
| 188 | |
| 189 | avl_tree_t *avl_alloc_tree(avl_compare_t cmp, avl_freeitem_t freeitem) { |
| 190 | return avl_init_tree(malloc(sizeof(avl_tree_t)), cmp, freeitem); |
| 191 | } |
| 192 | |
| 193 | void avl_clear_tree(avl_tree_t *avltree) { |
| 194 | avltree->top = avltree->head = avltree->tail = NULL; |
| 195 | } |
| 196 | |
| 197 | void avl_free_nodes(avl_tree_t *avltree) { |
| 198 | avl_node_t *node, *next; |
| 199 | avl_freeitem_t freeitem; |
| 200 | |
| 201 | freeitem = avltree->freeitem; |
| 202 | |
| 203 | for(node = avltree->head; node; node = next) { |
| 204 | next = node->next; |
| 205 | if(freeitem) |
| 206 | freeitem(node->item); |
| 207 | free(node); |
| 208 | } |
| 209 | |
| 210 | return avl_clear_tree(avltree); |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * avl_free_tree: |
| 215 | * Free all memory used by this tree. If freeitem is not NULL, then |
| 216 | * it is assumed to be a destructor for the items referenced in the avl_ |
| 217 | * tree, and they are deleted as well. |
| 218 | */ |
| 219 | void avl_free_tree(avl_tree_t *avltree) { |
| 220 | avl_free_nodes(avltree); |
| 221 | free(avltree); |
| 222 | } |
| 223 | |
| 224 | static void avl_clear_node(avl_node_t *newnode) { |
| 225 | newnode->left = newnode->right = NULL; |
| 226 | #ifdef AVL_COUNT |
| 227 | newnode->count = 1; |
| 228 | #endif |
| 229 | #ifdef AVL_DEPTH |
| 230 | newnode->depth = 1; |
| 231 | #endif |
| 232 | } |
| 233 | |
| 234 | avl_node_t *avl_init_node(avl_node_t *newnode, void *item) { |
| 235 | if(newnode) { |
| 236 | /* avl_clear_node(newnode); */ |
| 237 | newnode->item = item; |
| 238 | } |
| 239 | return newnode; |
| 240 | } |
| 241 | |
| 242 | avl_node_t *avl_insert_top(avl_tree_t *avltree, avl_node_t *newnode) { |
| 243 | avl_clear_node(newnode); |
| 244 | newnode->prev = newnode->next = newnode->parent = NULL; |
| 245 | avltree->head = avltree->tail = avltree->top = newnode; |
| 246 | return newnode; |
| 247 | } |
| 248 | |
| 249 | avl_node_t *avl_insert_before(avl_tree_t *avltree, avl_node_t *node, avl_node_t *newnode) { |
| 250 | if(!node) |
| 251 | return avltree->tail |
| 252 | ? avl_insert_after(avltree, avltree->tail, newnode) |
| 253 | : avl_insert_top(avltree, newnode); |
| 254 | |
| 255 | if(node->left) |
| 256 | return avl_insert_after(avltree, node->prev, newnode); |
| 257 | |
| 258 | avl_clear_node(newnode); |
| 259 | |
| 260 | newnode->next = node; |
| 261 | newnode->parent = node; |
| 262 | |
| 263 | newnode->prev = node->prev; |
| 264 | if(node->prev) |
| 265 | node->prev->next = newnode; |
| 266 | else |
| 267 | avltree->head = newnode; |
| 268 | node->prev = newnode; |
| 269 | |
| 270 | node->left = newnode; |
| 271 | avl_rebalance(avltree, node); |
| 272 | return newnode; |
| 273 | } |
| 274 | |
| 275 | avl_node_t *avl_insert_after(avl_tree_t *avltree, avl_node_t *node, avl_node_t *newnode) { |
| 276 | if(!node) |
| 277 | return avltree->head |
| 278 | ? avl_insert_before(avltree, avltree->head, newnode) |
| 279 | : avl_insert_top(avltree, newnode); |
| 280 | |
| 281 | if(node->right) |
| 282 | return avl_insert_before(avltree, node->next, newnode); |
| 283 | |
| 284 | avl_clear_node(newnode); |
| 285 | |
| 286 | newnode->prev = node; |
| 287 | newnode->parent = node; |
| 288 | |
| 289 | newnode->next = node->next; |
| 290 | if(node->next) |
| 291 | node->next->prev = newnode; |
| 292 | else |
| 293 | avltree->tail = newnode; |
| 294 | node->next = newnode; |
| 295 | |
| 296 | node->right = newnode; |
| 297 | avl_rebalance(avltree, node); |
| 298 | return newnode; |
| 299 | } |
| 300 | |
| 301 | avl_node_t *avl_insert_node(avl_tree_t *avltree, avl_node_t *newnode) { |
| 302 | avl_node_t *node; |
| 303 | |
| 304 | if(!avltree->top) |
| 305 | return avl_insert_top(avltree, newnode); |
| 306 | |
| 307 | switch(avl_search_closest(avltree, newnode->item, &node)) { |
| 308 | case -1: |
| 309 | return avl_insert_before(avltree, node, newnode); |
| 310 | case 1: |
| 311 | return avl_insert_after(avltree, node, newnode); |
| 312 | } |
| 313 | |
| 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | /* |
| 318 | * avl_insert: |
| 319 | * Create a new node and insert an item there. |
| 320 | * Returns the new node on success or NULL if no memory could be allocated. |
| 321 | */ |
| 322 | avl_node_t *avl_insert(avl_tree_t *avltree, void *item) { |
| 323 | avl_node_t *newnode; |
| 324 | |
| 325 | newnode = avl_init_node(malloc(sizeof(avl_node_t)), item); |
| 326 | if(newnode) { |
| 327 | if(avl_insert_node(avltree, newnode)) |
| 328 | return newnode; |
| 329 | free(newnode); |
| 330 | errno = EEXIST; |
| 331 | } |
| 332 | return NULL; |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | * avl_unlink_node: |
| 337 | * Removes the given node. Does not delete the item at that node. |
| 338 | * The item of the node may be freed before calling avl_unlink_node. |
| 339 | * (In other words, it is not referenced by this function.) |
| 340 | */ |
| 341 | void avl_unlink_node(avl_tree_t *avltree, avl_node_t *avlnode) { |
| 342 | avl_node_t *parent; |
| 343 | avl_node_t **superparent; |
| 344 | avl_node_t *subst, *left, *right; |
| 345 | avl_node_t *balnode; |
| 346 | |
| 347 | if(avlnode->prev) |
| 348 | avlnode->prev->next = avlnode->next; |
| 349 | else |
| 350 | avltree->head = avlnode->next; |
| 351 | |
| 352 | if(avlnode->next) |
| 353 | avlnode->next->prev = avlnode->prev; |
| 354 | else |
| 355 | avltree->tail = avlnode->prev; |
| 356 | |
| 357 | parent = avlnode->parent; |
| 358 | |
| 359 | superparent = parent |
| 360 | ? avlnode == parent->left ? &parent->left : &parent->right |
| 361 | : &avltree->top; |
| 362 | |
| 363 | left = avlnode->left; |
| 364 | right = avlnode->right; |
| 365 | if(!left) { |
| 366 | *superparent = right; |
| 367 | if(right) |
| 368 | right->parent = parent; |
| 369 | balnode = parent; |
| 370 | } else if(!right) { |
| 371 | *superparent = left; |
| 372 | left->parent = parent; |
| 373 | balnode = parent; |
| 374 | } else { |
| 375 | subst = avlnode->prev; |
| 376 | if(subst == left) { |
| 377 | balnode = subst; |
| 378 | } else { |
| 379 | balnode = subst->parent; |
| 380 | balnode->right = subst->left; |
| 381 | if(balnode->right) |
| 382 | balnode->right->parent = balnode; |
| 383 | subst->left = left; |
| 384 | left->parent = subst; |
| 385 | } |
| 386 | subst->right = right; |
| 387 | subst->parent = parent; |
| 388 | right->parent = subst; |
| 389 | *superparent = subst; |
| 390 | } |
| 391 | |
| 392 | avl_rebalance(avltree, balnode); |
| 393 | } |
| 394 | |
| 395 | void *avl_delete_node(avl_tree_t *avltree, avl_node_t *avlnode) { |
| 396 | void *item = NULL; |
| 397 | if(avlnode) { |
| 398 | item = avlnode->item; |
| 399 | avl_unlink_node(avltree, avlnode); |
| 400 | if(avltree->freeitem) |
| 401 | avltree->freeitem(item); |
| 402 | free(avlnode); |
| 403 | } |
| 404 | return item; |
| 405 | } |
| 406 | |
| 407 | void *avl_delete(avl_tree_t *avltree, const void *item) { |
| 408 | return avl_delete_node(avltree, avl_search(avltree, item)); |
| 409 | } |
| 410 | |
| 411 | avl_node_t *avl_fixup_node(avl_tree_t *avltree, avl_node_t *newnode) { |
| 412 | avl_node_t *oldnode = NULL, *node; |
| 413 | |
| 414 | if(!avltree || !newnode) |
| 415 | return NULL; |
| 416 | |
| 417 | node = newnode->prev; |
| 418 | if(node) { |
| 419 | oldnode = node->next; |
| 420 | node->next = newnode; |
| 421 | } else { |
| 422 | avltree->head = newnode; |
| 423 | } |
| 424 | |
| 425 | node = newnode->next; |
| 426 | if(node) { |
| 427 | oldnode = node->prev; |
| 428 | node->prev = newnode; |
| 429 | } else { |
| 430 | avltree->tail = newnode; |
| 431 | } |
| 432 | |
| 433 | node = newnode->parent; |
| 434 | if(node) { |
| 435 | if(node->left == oldnode) |
| 436 | node->left = newnode; |
| 437 | else |
| 438 | node->right = newnode; |
| 439 | } else { |
| 440 | oldnode = avltree->top; |
| 441 | avltree->top = newnode; |
| 442 | } |
| 443 | |
| 444 | return oldnode; |
| 445 | } |
| 446 | |
| 447 | /* |
| 448 | * avl_rebalance: |
| 449 | * Rebalances the tree if one side becomes too heavy. This function |
| 450 | * assumes that both subtrees are AVL-trees with consistant data. The |
| 451 | * function has the additional side effect of recalculating the count of |
| 452 | * the tree at this node. It should be noted that at the return of this |
| 453 | * function, if a rebalance takes place, the top of this subtree is no |
| 454 | * longer going to be the same node. |
| 455 | */ |
| 456 | void avl_rebalance(avl_tree_t *avltree, avl_node_t *avlnode) { |
| 457 | avl_node_t *child; |
| 458 | avl_node_t *gchild; |
| 459 | avl_node_t *parent; |
| 460 | avl_node_t **superparent; |
| 461 | |
| 462 | parent = avlnode; |
| 463 | |
| 464 | while(avlnode) { |
| 465 | parent = avlnode->parent; |
| 466 | |
| 467 | superparent = parent |
| 468 | ? avlnode == parent->left ? &parent->left : &parent->right |
| 469 | : &avltree->top; |
| 470 | |
| 471 | switch(avl_check_balance(avlnode)) { |
| 472 | case -1: |
| 473 | child = avlnode->left; |
| 474 | #ifdef AVL_DEPTH |
| 475 | if(L_DEPTH(child) >= R_DEPTH(child)) { |
| 476 | #else |
| 477 | #ifdef AVL_COUNT |
| 478 | if(L_COUNT(child) >= R_COUNT(child)) { |
| 479 | #else |
| 480 | #error No balancing possible. |
| 481 | #endif |
| 482 | #endif |
| 483 | avlnode->left = child->right; |
| 484 | if(avlnode->left) |
| 485 | avlnode->left->parent = avlnode; |
| 486 | child->right = avlnode; |
| 487 | avlnode->parent = child; |
| 488 | *superparent = child; |
| 489 | child->parent = parent; |
| 490 | #ifdef AVL_COUNT |
| 491 | avlnode->count = CALC_COUNT(avlnode); |
| 492 | child->count = CALC_COUNT(child); |
| 493 | #endif |
| 494 | #ifdef AVL_DEPTH |
| 495 | avlnode->depth = CALC_DEPTH(avlnode); |
| 496 | child->depth = CALC_DEPTH(child); |
| 497 | #endif |
| 498 | } else { |
| 499 | gchild = child->right; |
| 500 | avlnode->left = gchild->right; |
| 501 | if(avlnode->left) |
| 502 | avlnode->left->parent = avlnode; |
| 503 | child->right = gchild->left; |
| 504 | if(child->right) |
| 505 | child->right->parent = child; |
| 506 | gchild->right = avlnode; |
| 507 | if(gchild->right) |
| 508 | gchild->right->parent = gchild; |
| 509 | gchild->left = child; |
| 510 | if(gchild->left) |
| 511 | gchild->left->parent = gchild; |
| 512 | *superparent = gchild; |
| 513 | gchild->parent = parent; |
| 514 | #ifdef AVL_COUNT |
| 515 | avlnode->count = CALC_COUNT(avlnode); |
| 516 | child->count = CALC_COUNT(child); |
| 517 | gchild->count = CALC_COUNT(gchild); |
| 518 | #endif |
| 519 | #ifdef AVL_DEPTH |
| 520 | avlnode->depth = CALC_DEPTH(avlnode); |
| 521 | child->depth = CALC_DEPTH(child); |
| 522 | gchild->depth = CALC_DEPTH(gchild); |
| 523 | #endif |
| 524 | } |
| 525 | break; |
| 526 | case 1: |
| 527 | child = avlnode->right; |
| 528 | #ifdef AVL_DEPTH |
| 529 | if(R_DEPTH(child) >= L_DEPTH(child)) { |
| 530 | #else |
| 531 | #ifdef AVL_COUNT |
| 532 | if(R_COUNT(child) >= L_COUNT(child)) { |
| 533 | #else |
| 534 | #error No balancing possible. |
| 535 | #endif |
| 536 | #endif |
| 537 | avlnode->right = child->left; |
| 538 | if(avlnode->right) |
| 539 | avlnode->right->parent = avlnode; |
| 540 | child->left = avlnode; |
| 541 | avlnode->parent = child; |
| 542 | *superparent = child; |
| 543 | child->parent = parent; |
| 544 | #ifdef AVL_COUNT |
| 545 | avlnode->count = CALC_COUNT(avlnode); |
| 546 | child->count = CALC_COUNT(child); |
| 547 | #endif |
| 548 | #ifdef AVL_DEPTH |
| 549 | avlnode->depth = CALC_DEPTH(avlnode); |
| 550 | child->depth = CALC_DEPTH(child); |
| 551 | #endif |
| 552 | } else { |
| 553 | gchild = child->left; |
| 554 | avlnode->right = gchild->left; |
| 555 | if(avlnode->right) |
| 556 | avlnode->right->parent = avlnode; |
| 557 | child->left = gchild->right; |
| 558 | if(child->left) |
| 559 | child->left->parent = child; |
| 560 | gchild->left = avlnode; |
| 561 | if(gchild->left) |
| 562 | gchild->left->parent = gchild; |
| 563 | gchild->right = child; |
| 564 | if(gchild->right) |
| 565 | gchild->right->parent = gchild; |
| 566 | *superparent = gchild; |
| 567 | gchild->parent = parent; |
| 568 | #ifdef AVL_COUNT |
| 569 | avlnode->count = CALC_COUNT(avlnode); |
| 570 | child->count = CALC_COUNT(child); |
| 571 | gchild->count = CALC_COUNT(gchild); |
| 572 | #endif |
| 573 | #ifdef AVL_DEPTH |
| 574 | avlnode->depth = CALC_DEPTH(avlnode); |
| 575 | child->depth = CALC_DEPTH(child); |
| 576 | gchild->depth = CALC_DEPTH(gchild); |
| 577 | #endif |
| 578 | } |
| 579 | break; |
| 580 | default: |
| 581 | #ifdef AVL_COUNT |
| 582 | avlnode->count = CALC_COUNT(avlnode); |
| 583 | #endif |
| 584 | #ifdef AVL_DEPTH |
| 585 | avlnode->depth = CALC_DEPTH(avlnode); |
| 586 | #endif |
| 587 | } |
| 588 | avlnode = parent; |
| 589 | } |
| 590 | } |