1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 30 /* 31 * AVL - generic AVL tree implementation for kernel use 32 * 33 * A complete description of AVL trees can be found in many CS textbooks. 34 * 35 * Here is a very brief overview. An AVL tree is a binary search tree that is 36 * almost perfectly balanced. By "almost" perfectly balanced, we mean that at 37 * any given node, the left and right subtrees are allowed to differ in height 38 * by at most 1 level. 39 * 40 * This relaxation from a perfectly balanced binary tree allows doing 41 * insertion and deletion relatively efficiently. Searching the tree is 42 * still a fast operation, roughly O(log(N)). 43 * 44 * The key to insertion and deletion is a set of tree maniuplations called 45 * rotations, which bring unbalanced subtrees back into the semi-balanced state. 46 * 47 * This implementation of AVL trees has the following peculiarities: 48 * 49 * - The AVL specific data structures are physically embedded as fields 50 * in the "using" data structures. To maintain generality the code 51 * must constantly translate between "avl_node_t *" and containing 52 * data structure "void *"s by adding/subracting the avl_offset. 53 * 54 * - Since the AVL data is always embedded in other structures, there is 55 * no locking or memory allocation in the AVL routines. This must be 56 * provided for by the enclosing data structure's semantics. Typically, 57 * avl_insert()/_remove()/avl_insert_here() require some kind of 58 * exclusive write lock. Other operations require a read lock. 59 * 60 * - The implementation uses iteration instead of explicit recursion, 61 * since it is intended to run on limited size kernel stacks. Since 62 * there is no recursion stack present to move "up" in the tree, 63 * there is an explicit "parent" link in the avl_node_t. 64 * 65 * - The left/right children pointers of a node are in an array. 66 * In the code, variables (instead of constants) are used to represent 67 * left and right indices. The implementation is written as if it only 68 * dealt with left handed manipulations. By changing the value assigned 69 * to "left", the code also works for right handed trees. The 70 * following variables/terms are frequently used: 71 * 72 * int left; // 0 when dealing with left children, 73 * // 1 for dealing with right children 74 * 75 * int left_heavy; // -1 when left subtree is taller at some node, 76 * // +1 when right subtree is taller 77 * 78 * int right; // will be the opposite of left (0 or 1) 79 * int right_heavy;// will be the opposite of left_heavy (-1 or 1) 80 * 81 * int direction; // 0 for "<" (ie. left child); 1 for ">" (right) 82 * 83 * Though it is a little more confusing to read the code, the approach 84 * allows using half as much code (and hence cache footprint) for tree 85 * manipulations and eliminates many conditional branches. 86 * 87 * - The avl_index_t is an opaque "cookie" used to find nodes at or 88 * adjacent to where a new value would be inserted in the tree. The value 89 * is a modified "avl_node_t *". The bottom bit (normally 0 for a 90 * pointer) is set to indicate if that the new node has a value greater 91 * than the value of the indicated "avl_node_t *". 92 */ 93 94 #include <sys/types.h> 95 #include <sys/param.h> 96 #include <sys/debug.h> 97 #include <sys/avl.h> 98 99 /* 100 * Small arrays to translate between balance (or diff) values and child indeces. 101 * 102 * Code that deals with binary tree data structures will randomly use 103 * left and right children when examining a tree. C "if()" statements 104 * which evaluate randomly suffer from very poor hardware branch prediction. 105 * In this code we avoid some of the branch mispredictions by using the 106 * following translation arrays. They replace random branches with an 107 * additional memory reference. Since the translation arrays are both very 108 * small the data should remain efficiently in cache. 109 */ 110 static const int avl_child2balance[2] = {-1, 1}; 111 static const int avl_balance2child[] = {0, 0, 1}; 112 113 114 /* 115 * Walk from one node to the previous valued node (ie. an infix walk 116 * towards the left). At any given node we do one of 2 things: 117 * 118 * - If there is a left child, go to it, then to it's rightmost descendant. 119 * 120 * - otherwise we return thru parent nodes until we've come from a right child. 121 * 122 * Return Value: 123 * NULL - if at the end of the nodes 124 * otherwise next node 125 */ 126 void * 127 avl_walk(avl_tree_t *tree, void *oldnode, int left) 128 { 129 size_t off = tree->avl_offset; 130 avl_node_t *node = AVL_DATA2NODE(oldnode, off); 131 int right = 1 - left; 132 int was_child; 133 134 135 /* 136 * nowhere to walk to if tree is empty 137 */ 138 if (node == NULL) 139 return (NULL); 140 141 /* 142 * Visit the previous valued node. There are two possibilities: 143 * 144 * If this node has a left child, go down one left, then all 145 * the way right. 146 */ 147 if (node->avl_child[left] != NULL) { 148 for (node = node->avl_child[left]; 149 node->avl_child[right] != NULL; 150 node = node->avl_child[right]) 151 ; 152 /* 153 * Otherwise, return thru left children as far as we can. 154 */ 155 } else { 156 for (;;) { 157 was_child = AVL_XCHILD(node); 158 node = AVL_XPARENT(node); 159 if (node == NULL) 160 return (NULL); 161 if (was_child == right) 162 break; 163 } 164 } 165 166 return (AVL_NODE2DATA(node, off)); 167 } 168 169 /* 170 * Return the lowest valued node in a tree or NULL. 171 * (leftmost child from root of tree) 172 */ 173 void * 174 avl_first(avl_tree_t *tree) 175 { 176 avl_node_t *node; 177 avl_node_t *prev = NULL; 178 size_t off = tree->avl_offset; 179 180 for (node = tree->avl_root; node != NULL; node = node->avl_child[0]) 181 prev = node; 182 183 if (prev != NULL) 184 return (AVL_NODE2DATA(prev, off)); 185 return (NULL); 186 } 187 188 /* 189 * Return the highest valued node in a tree or NULL. 190 * (rightmost child from root of tree) 191 */ 192 void * 193 avl_last(avl_tree_t *tree) 194 { 195 avl_node_t *node; 196 avl_node_t *prev = NULL; 197 size_t off = tree->avl_offset; 198 199 for (node = tree->avl_root; node != NULL; node = node->avl_child[1]) 200 prev = node; 201 202 if (prev != NULL) 203 return (AVL_NODE2DATA(prev, off)); 204 return (NULL); 205 } 206 207 /* 208 * Access the node immediately before or after an insertion point. 209 * 210 * "avl_index_t" is a (avl_node_t *) with the bottom bit indicating a child 211 * 212 * Return value: 213 * NULL: no node in the given direction 214 * "void *" of the found tree node 215 */ 216 void * 217 avl_nearest(avl_tree_t *tree, avl_index_t where, int direction) 218 { 219 int child = AVL_INDEX2CHILD(where); 220 avl_node_t *node = AVL_INDEX2NODE(where); 221 void *data; 222 size_t off = tree->avl_offset; 223 224 if (node == NULL) { 225 ASSERT(tree->avl_root == NULL); 226 return (NULL); 227 } 228 data = AVL_NODE2DATA(node, off); 229 if (child != direction) 230 return (data); 231 232 return (avl_walk(tree, data, direction)); 233 } 234 235 236 /* 237 * Search for the node which contains "value". The algorithm is a 238 * simple binary tree search. 239 * 240 * return value: 241 * NULL: the value is not in the AVL tree 242 * *where (if not NULL) is set to indicate the insertion point 243 * "void *" of the found tree node 244 */ 245 void * 246 avl_find(avl_tree_t *tree, void *value, avl_index_t *where) 247 { 248 avl_node_t *node; 249 avl_node_t *prev = NULL; 250 int child = 0; 251 int diff; 252 size_t off = tree->avl_offset; 253 254 for (node = tree->avl_root; node != NULL; 255 node = node->avl_child[child]) { 256 257 prev = node; 258 259 diff = tree->avl_compar(value, AVL_NODE2DATA(node, off)); 260 ASSERT(-1 <= diff && diff <= 1); 261 if (diff == 0) { 262 #ifdef DEBUG 263 if (where != NULL) 264 *where = NULL; 265 #endif 266 return (AVL_NODE2DATA(node, off)); 267 } 268 child = avl_balance2child[1 + diff]; 269 270 } 271 272 if (where != NULL) 273 *where = AVL_MKINDEX(prev, child); 274 275 return (NULL); 276 } 277 278 279 /* 280 * Perform a rotation to restore balance at the subtree given by depth. 281 * 282 * This routine is used by both insertion and deletion. The return value 283 * indicates: 284 * 0 : subtree did not change height 285 * !0 : subtree was reduced in height 286 * 287 * The code is written as if handling left rotations, right rotations are 288 * symmetric and handled by swapping values of variables right/left[_heavy] 289 * 290 * On input balance is the "new" balance at "node". This value is either 291 * -2 or +2. 292 */ 293 static int 294 avl_rotation(avl_tree_t *tree, avl_node_t *node, int balance) 295 { 296 int left = !(balance < 0); /* when balance = -2, left will be 0 */ 297 int right = 1 - left; 298 int left_heavy = balance >> 1; 299 int right_heavy = -left_heavy; 300 avl_node_t *parent = AVL_XPARENT(node); 301 avl_node_t *child = node->avl_child[left]; 302 avl_node_t *cright; 303 avl_node_t *gchild; 304 avl_node_t *gright; 305 avl_node_t *gleft; 306 int which_child = AVL_XCHILD(node); 307 int child_bal = AVL_XBALANCE(child); 308 309 /* BEGIN CSTYLED */ 310 /* 311 * case 1 : node is overly left heavy, the left child is balanced or 312 * also left heavy. This requires the following rotation. 313 * 314 * (node bal:-2) 315 * / \ 316 * / \ 317 * (child bal:0 or -1) 318 * / \ 319 * / \ 320 * cright 321 * 322 * becomes: 323 * 324 * (child bal:1 or 0) 325 * / \ 326 * / \ 327 * (node bal:-1 or 0) 328 * / \ 329 * / \ 330 * cright 331 * 332 * we detect this situation by noting that child's balance is not 333 * right_heavy. 334 */ 335 /* END CSTYLED */ 336 if (child_bal != right_heavy) { 337 338 /* 339 * compute new balance of nodes 340 * 341 * If child used to be left heavy (now balanced) we reduced 342 * the height of this sub-tree -- used in "return...;" below 343 */ 344 child_bal += right_heavy; /* adjust towards right */ 345 346 /* 347 * move "cright" to be node's left child 348 */ 349 cright = child->avl_child[right]; 350 node->avl_child[left] = cright; 351 if (cright != NULL) { 352 AVL_SETPARENT(cright, node); 353 AVL_SETCHILD(cright, left); 354 } 355 356 /* 357 * move node to be child's right child 358 */ 359 child->avl_child[right] = node; 360 AVL_SETBALANCE(node, -child_bal); 361 AVL_SETCHILD(node, right); 362 AVL_SETPARENT(node, child); 363 364 /* 365 * update the pointer into this subtree 366 */ 367 AVL_SETBALANCE(child, child_bal); 368 AVL_SETCHILD(child, which_child); 369 AVL_SETPARENT(child, parent); 370 if (parent != NULL) 371 parent->avl_child[which_child] = child; 372 else 373 tree->avl_root = child; 374 375 return (child_bal == 0); 376 } 377 378 /* BEGIN CSTYLED */ 379 /* 380 * case 2 : When node is left heavy, but child is right heavy we use 381 * a different rotation. 382 * 383 * (node b:-2) 384 * / \ 385 * / \ 386 * / \ 387 * (child b:+1) 388 * / \ 389 * / \ 390 * (gchild b: != 0) 391 * / \ 392 * / \ 393 * gleft gright 394 * 395 * becomes: 396 * 397 * (gchild b:0) 398 * / \ 399 * / \ 400 * / \ 401 * (child b:?) (node b:?) 402 * / \ / \ 403 * / \ / \ 404 * gleft gright 405 * 406 * computing the new balances is more complicated. As an example: 407 * if gchild was right_heavy, then child is now left heavy 408 * else it is balanced 409 */ 410 /* END CSTYLED */ 411 gchild = child->avl_child[right]; 412 gleft = gchild->avl_child[left]; 413 gright = gchild->avl_child[right]; 414 415 /* 416 * move gright to left child of node and 417 * 418 * move gleft to right child of node 419 */ 420 node->avl_child[left] = gright; 421 if (gright != NULL) { 422 AVL_SETPARENT(gright, node); 423 AVL_SETCHILD(gright, left); 424 } 425 426 child->avl_child[right] = gleft; 427 if (gleft != NULL) { 428 AVL_SETPARENT(gleft, child); 429 AVL_SETCHILD(gleft, right); 430 } 431 432 /* 433 * move child to left child of gchild and 434 * 435 * move node to right child of gchild and 436 * 437 * fixup parent of all this to point to gchild 438 */ 439 balance = AVL_XBALANCE(gchild); 440 gchild->avl_child[left] = child; 441 AVL_SETBALANCE(child, (balance == right_heavy ? left_heavy : 0)); 442 AVL_SETPARENT(child, gchild); 443 AVL_SETCHILD(child, left); 444 445 gchild->avl_child[right] = node; 446 AVL_SETBALANCE(node, (balance == left_heavy ? right_heavy : 0)); 447 AVL_SETPARENT(node, gchild); 448 AVL_SETCHILD(node, right); 449 450 AVL_SETBALANCE(gchild, 0); 451 AVL_SETPARENT(gchild, parent); 452 AVL_SETCHILD(gchild, which_child); 453 if (parent != NULL) 454 parent->avl_child[which_child] = gchild; 455 else 456 tree->avl_root = gchild; 457 458 return (1); /* the new tree is always shorter */ 459 } 460 461 462 /* 463 * Insert a new node into an AVL tree at the specified (from avl_find()) place. 464 * 465 * Newly inserted nodes are always leaf nodes in the tree, since avl_find() 466 * searches out to the leaf positions. The avl_index_t indicates the node 467 * which will be the parent of the new node. 468 * 469 * After the node is inserted, a single rotation further up the tree may 470 * be necessary to maintain an acceptable AVL balance. 471 */ 472 void 473 avl_insert(avl_tree_t *tree, void *new_data, avl_index_t where) 474 { 475 avl_node_t *node; 476 avl_node_t *parent = AVL_INDEX2NODE(where); 477 int old_balance; 478 int new_balance; 479 int which_child = AVL_INDEX2CHILD(where); 480 size_t off = tree->avl_offset; 481 482 ASSERT(tree); 483 #ifdef _LP64 484 ASSERT(((uintptr_t)new_data & 0x7) == 0); 485 #endif 486 487 node = AVL_DATA2NODE(new_data, off); 488 489 /* 490 * First, add the node to the tree at the indicated position. 491 */ 492 ++tree->avl_numnodes; 493 494 node->avl_child[0] = NULL; 495 node->avl_child[1] = NULL; 496 497 AVL_SETCHILD(node, which_child); 498 AVL_SETBALANCE(node, 0); 499 AVL_SETPARENT(node, parent); 500 if (parent != NULL) { 501 ASSERT(parent->avl_child[which_child] == NULL); 502 parent->avl_child[which_child] = node; 503 } else { 504 ASSERT(tree->avl_root == NULL); 505 tree->avl_root = node; 506 } 507 /* 508 * Now, back up the tree modifying the balance of all nodes above the 509 * insertion point. If we get to a highly unbalanced ancestor, we 510 * need to do a rotation. If we back out of the tree we are done. 511 * If we brought any subtree into perfect balance (0), we are also done. 512 */ 513 for (;;) { 514 node = parent; 515 if (node == NULL) 516 return; 517 518 /* 519 * Compute the new balance 520 */ 521 old_balance = AVL_XBALANCE(node); 522 new_balance = old_balance + avl_child2balance[which_child]; 523 524 /* 525 * If we introduced equal balance, then we are done immediately 526 */ 527 if (new_balance == 0) { 528 AVL_SETBALANCE(node, 0); 529 return; 530 } 531 532 /* 533 * If both old and new are not zero we went 534 * from -1 to -2 balance, do a rotation. 535 */ 536 if (old_balance != 0) 537 break; 538 539 AVL_SETBALANCE(node, new_balance); 540 parent = AVL_XPARENT(node); 541 which_child = AVL_XCHILD(node); 542 } 543 544 /* 545 * perform a rotation to fix the tree and return 546 */ 547 (void) avl_rotation(tree, node, new_balance); 548 } 549 550 /* 551 * Insert "new_data" in "tree" in the given "direction" either after or 552 * before (AVL_AFTER, AVL_BEFORE) the data "here". 553 * 554 * Insertions can only be done at empty leaf points in the tree, therefore 555 * if the given child of the node is already present we move to either 556 * the AVL_PREV or AVL_NEXT and reverse the insertion direction. Since 557 * every other node in the tree is a leaf, this always works. 558 * 559 * To help developers using this interface, we assert that the new node 560 * is correctly ordered at every step of the way in DEBUG kernels. 561 */ 562 void 563 avl_insert_here( 564 avl_tree_t *tree, 565 void *new_data, 566 void *here, 567 int direction) 568 { 569 avl_node_t *node; 570 int child = direction; /* rely on AVL_BEFORE == 0, AVL_AFTER == 1 */ 571 572 ASSERT(tree != NULL); 573 ASSERT(new_data != NULL); 574 ASSERT(here != NULL); 575 ASSERT(direction == AVL_BEFORE || direction == AVL_AFTER); 576 577 /* 578 * If corresponding child of node is not NULL, go to the neighboring 579 * node and reverse the insertion direction. 580 */ 581 node = AVL_DATA2NODE(here, tree->avl_offset); 582 ASSERT(tree->avl_compar(new_data, here) > 0 ? child == 1 : child == 0); 583 584 if (node->avl_child[child] != NULL) { 585 node = node->avl_child[child]; 586 child = 1 - child; 587 while (node->avl_child[child] != NULL) { 588 ASSERT(tree->avl_compar(new_data, 589 AVL_NODE2DATA(node, tree->avl_offset)) > 0 ? 590 child == 1 : child == 0); 591 node = node->avl_child[child]; 592 } 593 ASSERT(tree->avl_compar(new_data, 594 AVL_NODE2DATA(node, tree->avl_offset)) > 0 ? 595 child == 1 : child == 0); 596 } 597 ASSERT(node->avl_child[child] == NULL); 598 599 avl_insert(tree, new_data, AVL_MKINDEX(node, child)); 600 } 601 602 /* 603 * Delete a node from the AVL tree. Deletion is similar to insertion, but 604 * with 2 complications. 605 * 606 * First, we may be deleting an interior node. Consider the following subtree: 607 * 608 * d c c 609 * / \ / \ / \ 610 * b e b e b e 611 * / \ / \ / 612 * a c a a 613 * 614 * When we are deleting node (d), we find and bring up an adjacent valued leaf 615 * node, say (c), to take the interior node's place. In the code this is 616 * handled by temporarily swapping (d) and (c) in the tree and then using 617 * common code to delete (d) from the leaf position. 618 * 619 * Secondly, an interior deletion from a deep tree may require more than one 620 * rotation to fix the balance. This is handled by moving up the tree through 621 * parents and applying rotations as needed. The return value from 622 * avl_rotation() is used to detect when a subtree did not change overall 623 * height due to a rotation. 624 */ 625 void 626 avl_remove(avl_tree_t *tree, void *data) 627 { 628 avl_node_t *delete; 629 avl_node_t *parent; 630 avl_node_t *node; 631 avl_node_t tmp; 632 int old_balance; 633 int new_balance; 634 int left; 635 int right; 636 int which_child; 637 size_t off = tree->avl_offset; 638 639 ASSERT(tree); 640 641 delete = AVL_DATA2NODE(data, off); 642 643 /* 644 * Deletion is easiest with a node that has at most 1 child. 645 * We swap a node with 2 children with a sequentially valued 646 * neighbor node. That node will have at most 1 child. Note this 647 * has no effect on the ordering of the remaining nodes. 648 * 649 * As an optimization, we choose the greater neighbor if the tree 650 * is right heavy, otherwise the left neighbor. This reduces the 651 * number of rotations needed. 652 */ 653 if (delete->avl_child[0] != NULL && delete->avl_child[1] != NULL) { 654 655 /* 656 * choose node to swap from whichever side is taller 657 */ 658 old_balance = AVL_XBALANCE(delete); 659 left = avl_balance2child[old_balance + 1]; 660 right = 1 - left; 661 662 /* 663 * get to the previous value'd node 664 * (down 1 left, as far as possible right) 665 */ 666 for (node = delete->avl_child[left]; 667 node->avl_child[right] != NULL; 668 node = node->avl_child[right]) 669 ; 670 671 /* 672 * create a temp placeholder for 'node' 673 * move 'node' to delete's spot in the tree 674 */ 675 tmp = *node; 676 677 *node = *delete; 678 if (node->avl_child[left] == node) 679 node->avl_child[left] = &tmp; 680 681 parent = AVL_XPARENT(node); 682 if (parent != NULL) 683 parent->avl_child[AVL_XCHILD(node)] = node; 684 else 685 tree->avl_root = node; 686 AVL_SETPARENT(node->avl_child[left], node); 687 AVL_SETPARENT(node->avl_child[right], node); 688 689 /* 690 * Put tmp where node used to be (just temporary). 691 * It always has a parent and at most 1 child. 692 */ 693 delete = &tmp; 694 parent = AVL_XPARENT(delete); 695 parent->avl_child[AVL_XCHILD(delete)] = delete; 696 which_child = (delete->avl_child[1] != 0); 697 if (delete->avl_child[which_child] != NULL) 698 AVL_SETPARENT(delete->avl_child[which_child], delete); 699 } 700 701 702 /* 703 * Here we know "delete" is at least partially a leaf node. It can 704 * be easily removed from the tree. 705 */ 706 --tree->avl_numnodes; 707 parent = AVL_XPARENT(delete); 708 which_child = AVL_XCHILD(delete); 709 if (delete->avl_child[0] != NULL) 710 node = delete->avl_child[0]; 711 else 712 node = delete->avl_child[1]; 713 714 /* 715 * Connect parent directly to node (leaving out delete). 716 */ 717 if (node != NULL) { 718 AVL_SETPARENT(node, parent); 719 AVL_SETCHILD(node, which_child); 720 } 721 if (parent == NULL) { 722 tree->avl_root = node; 723 return; 724 } 725 parent->avl_child[which_child] = node; 726 727 728 /* 729 * Since the subtree is now shorter, begin adjusting parent balances 730 * and performing any needed rotations. 731 */ 732 do { 733 734 /* 735 * Move up the tree and adjust the balance 736 * 737 * Capture the parent and which_child values for the next 738 * iteration before any rotations occur. 739 */ 740 node = parent; 741 old_balance = AVL_XBALANCE(node); 742 new_balance = old_balance - avl_child2balance[which_child]; 743 parent = AVL_XPARENT(node); 744 which_child = AVL_XCHILD(node); 745 746 /* 747 * If a node was in perfect balance but isn't anymore then 748 * we can stop, since the height didn't change above this point 749 * due to a deletion. 750 */ 751 if (old_balance == 0) { 752 AVL_SETBALANCE(node, new_balance); 753 break; 754 } 755 756 /* 757 * If the new balance is zero, we don't need to rotate 758 * else 759 * need a rotation to fix the balance. 760 * If the rotation doesn't change the height 761 * of the sub-tree we have finished adjusting. 762 */ 763 if (new_balance == 0) 764 AVL_SETBALANCE(node, new_balance); 765 else if (!avl_rotation(tree, node, new_balance)) 766 break; 767 } while (parent != NULL); 768 } 769 770 /* 771 * initialize a new AVL tree 772 */ 773 void 774 avl_create(avl_tree_t *tree, int (*compar) (const void *, const void *), 775 size_t size, size_t offset) 776 { 777 ASSERT(tree); 778 ASSERT(compar); 779 ASSERT(size > 0); 780 ASSERT(size >= offset + sizeof (avl_node_t)); 781 #ifdef _LP64 782 ASSERT((offset & 0x7) == 0); 783 #endif 784 785 tree->avl_compar = compar; 786 tree->avl_root = NULL; 787 tree->avl_numnodes = 0; 788 tree->avl_size = size; 789 tree->avl_offset = offset; 790 } 791 792 /* 793 * Delete a tree. 794 */ 795 /* ARGSUSED */ 796 void 797 avl_destroy(avl_tree_t *tree) 798 { 799 ASSERT(tree); 800 ASSERT(tree->avl_numnodes == 0); 801 ASSERT(tree->avl_root == NULL); 802 } 803 804 805 /* 806 * Return the number of nodes in an AVL tree. 807 */ 808 ulong_t 809 avl_numnodes(avl_tree_t *tree) 810 { 811 ASSERT(tree); 812 return (tree->avl_numnodes); 813 } 814 815 816 #define CHILDBIT (1L) 817 818 /* 819 * Post-order tree walk used to visit all tree nodes and destroy the tree 820 * in post order. This is used for destroying a tree w/o paying any cost 821 * for rebalancing it. 822 * 823 * example: 824 * 825 * void *cookie = NULL; 826 * my_data_t *node; 827 * 828 * while ((node = avl_destroy_nodes(tree, &cookie)) != NULL) 829 * free(node); 830 * avl_destroy(tree); 831 * 832 * The cookie is really an avl_node_t to the current node's parent and 833 * an indication of which child you looked at last. 834 * 835 * On input, a cookie value of CHILDBIT indicates the tree is done. 836 */ 837 void * 838 avl_destroy_nodes(avl_tree_t *tree, void **cookie) 839 { 840 avl_node_t *node; 841 avl_node_t *parent; 842 int child; 843 void *first; 844 size_t off = tree->avl_offset; 845 846 /* 847 * Initial calls go to the first node or it's right descendant. 848 */ 849 if (*cookie == NULL) { 850 first = avl_first(tree); 851 852 /* 853 * deal with an empty tree 854 */ 855 if (first == NULL) { 856 *cookie = (void *)CHILDBIT; 857 return (NULL); 858 } 859 860 node = AVL_DATA2NODE(first, off); 861 parent = AVL_XPARENT(node); 862 goto check_right_side; 863 } 864 865 /* 866 * If there is no parent to return to we are done. 867 */ 868 parent = (avl_node_t *)((uintptr_t)(*cookie) & ~CHILDBIT); 869 if (parent == NULL) { 870 if (tree->avl_root != NULL) { 871 ASSERT(tree->avl_numnodes == 1); 872 tree->avl_root = NULL; 873 tree->avl_numnodes = 0; 874 } 875 return (NULL); 876 } 877 878 /* 879 * Remove the child pointer we just visited from the parent and tree. 880 */ 881 child = (uintptr_t)(*cookie) & CHILDBIT; 882 parent->avl_child[child] = NULL; 883 ASSERT(tree->avl_numnodes > 1); 884 --tree->avl_numnodes; 885 886 /* 887 * If we just did a right child or there isn't one, go up to parent. 888 */ 889 if (child == 1 || parent->avl_child[1] == NULL) { 890 node = parent; 891 parent = AVL_XPARENT(parent); 892 goto done; 893 } 894 895 /* 896 * Do parent's right child, then leftmost descendent. 897 */ 898 node = parent->avl_child[1]; 899 while (node->avl_child[0] != NULL) { 900 parent = node; 901 node = node->avl_child[0]; 902 } 903 904 /* 905 * If here, we moved to a left child. It may have one 906 * child on the right (when balance == +1). 907 */ 908 check_right_side: 909 if (node->avl_child[1] != NULL) { 910 ASSERT(AVL_XBALANCE(node) == 1); 911 parent = node; 912 node = node->avl_child[1]; 913 ASSERT(node->avl_child[0] == NULL && 914 node->avl_child[1] == NULL); 915 } else { 916 ASSERT(AVL_XBALANCE(node) <= 0); 917 } 918 919 done: 920 if (parent == NULL) { 921 *cookie = (void *)CHILDBIT; 922 ASSERT(node == tree->avl_root); 923 } else { 924 *cookie = (void *)((uintptr_t)parent | AVL_XCHILD(node)); 925 } 926 927 return (AVL_NODE2DATA(node, off)); 928 } 929