4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
30 * AVL - generic AVL tree implementation for kernel use
32 * A complete description of AVL trees can be found in many CS textbooks.
34 * Here is a very brief overview. An AVL tree is a binary search tree that is
35 * almost perfectly balanced. By "almost" perfectly balanced, we mean that at
36 * any given node, the left and right subtrees are allowed to differ in height
39 * This relaxation from a perfectly balanced binary tree allows doing
40 * insertion and deletion relatively efficiently. Searching the tree is
41 * still a fast operation, roughly O(log(N)).
43 * The key to insertion and deletion is a set of tree maniuplations called
44 * rotations, which bring unbalanced subtrees back into the semi-balanced state.
46 * This implementation of AVL trees has the following peculiarities:
48 * - The AVL specific data structures are physically embedded as fields
49 * in the "using" data structures. To maintain generality the code
50 * must constantly translate between "avl_node_t *" and containing
51 * data structure "void *"s by adding/subracting the avl_offset.
53 * - Since the AVL data is always embedded in other structures, there is
54 * no locking or memory allocation in the AVL routines. This must be
55 * provided for by the enclosing data structure's semantics. Typically,
56 * avl_insert()/_add()/_remove()/avl_insert_here() require some kind of
57 * exclusive write lock. Other operations require a read lock.
59 * - The implementation uses iteration instead of explicit recursion,
60 * since it is intended to run on limited size kernel stacks. Since
61 * there is no recursion stack present to move "up" in the tree,
62 * there is an explicit "parent" link in the avl_node_t.
64 * - The left/right children pointers of a node are in an array.
65 * In the code, variables (instead of constants) are used to represent
66 * left and right indices. The implementation is written as if it only
67 * dealt with left handed manipulations. By changing the value assigned
68 * to "left", the code also works for right handed trees. The
69 * following variables/terms are frequently used:
71 * int left; // 0 when dealing with left children,
72 * // 1 for dealing with right children
74 * int left_heavy; // -1 when left subtree is taller at some node,
75 * // +1 when right subtree is taller
77 * int right; // will be the opposite of left (0 or 1)
78 * int right_heavy;// will be the opposite of left_heavy (-1 or 1)
80 * int direction; // 0 for "<" (ie. left child); 1 for ">" (right)
82 * Though it is a little more confusing to read the code, the approach
83 * allows using half as much code (and hence cache footprint) for tree
84 * manipulations and eliminates many conditional branches.
86 * - The avl_index_t is an opaque "cookie" used to find nodes at or
87 * adjacent to where a new value would be inserted in the tree. The value
88 * is a modified "avl_node_t *". The bottom bit (normally 0 for a
89 * pointer) is set to indicate if that the new node has a value greater
90 * than the value of the indicated "avl_node_t *".
93 #include <sys/types.h>
94 #include <sys/param.h>
95 #include <sys/debug.h>
97 #include <sys/cmn_err.h>
100 * Small arrays to translate between balance (or diff) values and child indeces.
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.
110 static const int avl_child2balance
[2] = {-1, 1};
111 static const int avl_balance2child
[] = {0, 0, 1};
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:
118 * - If there is a left child, go to it, then to it's rightmost descendant.
120 * - otherwise we return thru parent nodes until we've come from a right child.
123 * NULL - if at the end of the nodes
124 * otherwise next node
127 avl_walk(avl_tree_t
*tree
, void *oldnode
, int left
)
129 size_t off
= tree
->avl_offset
;
130 avl_node_t
*node
= AVL_DATA2NODE(oldnode
, off
);
131 int right
= 1 - left
;
136 * nowhere to walk to if tree is empty
142 * Visit the previous valued node. There are two possibilities:
144 * If this node has a left child, go down one left, then all
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
])
153 * Otherwise, return thru left children as far as we can.
157 was_child
= AVL_XCHILD(node
);
158 node
= AVL_XPARENT(node
);
161 if (was_child
== right
)
166 return (AVL_NODE2DATA(node
, off
));
170 * Return the lowest valued node in a tree or NULL.
171 * (leftmost child from root of tree)
174 avl_first(avl_tree_t
*tree
)
177 avl_node_t
*prev
= NULL
;
178 size_t off
= tree
->avl_offset
;
180 for (node
= tree
->avl_root
; node
!= NULL
; node
= node
->avl_child
[0])
184 return (AVL_NODE2DATA(prev
, off
));
189 * Return the highest valued node in a tree or NULL.
190 * (rightmost child from root of tree)
193 avl_last(avl_tree_t
*tree
)
196 avl_node_t
*prev
= NULL
;
197 size_t off
= tree
->avl_offset
;
199 for (node
= tree
->avl_root
; node
!= NULL
; node
= node
->avl_child
[1])
203 return (AVL_NODE2DATA(prev
, off
));
208 * Access the node immediately before or after an insertion point.
210 * "avl_index_t" is a (avl_node_t *) with the bottom bit indicating a child
213 * NULL: no node in the given direction
214 * "void *" of the found tree node
217 avl_nearest(avl_tree_t
*tree
, avl_index_t where
, int direction
)
219 int child
= AVL_INDEX2CHILD(where
);
220 avl_node_t
*node
= AVL_INDEX2NODE(where
);
222 size_t off
= tree
->avl_offset
;
225 ASSERT(tree
->avl_root
== NULL
);
228 data
= AVL_NODE2DATA(node
, off
);
229 if (child
!= direction
)
232 return (avl_walk(tree
, data
, direction
));
237 * Search for the node which contains "value". The algorithm is a
238 * simple binary tree search.
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
246 avl_find(avl_tree_t
*tree
, void *value
, avl_index_t
*where
)
249 avl_node_t
*prev
= NULL
;
252 size_t off
= tree
->avl_offset
;
254 for (node
= tree
->avl_root
; node
!= NULL
;
255 node
= node
->avl_child
[child
]) {
259 diff
= tree
->avl_compar(value
, AVL_NODE2DATA(node
, off
));
260 ASSERT(-1 <= diff
&& diff
<= 1);
266 return (AVL_NODE2DATA(node
, off
));
268 child
= avl_balance2child
[1 + diff
];
273 *where
= AVL_MKINDEX(prev
, child
);
280 * Perform a rotation to restore balance at the subtree given by depth.
282 * This routine is used by both insertion and deletion. The return value
284 * 0 : subtree did not change height
285 * !0 : subtree was reduced in height
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]
290 * On input balance is the "new" balance at "node". This value is either
294 avl_rotation(avl_tree_t
*tree
, avl_node_t
*node
, int balance
)
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
];
306 int which_child
= AVL_XCHILD(node
);
307 int child_bal
= AVL_XBALANCE(child
);
311 * case 1 : node is overly left heavy, the left child is balanced or
312 * also left heavy. This requires the following rotation.
317 * (child bal:0 or -1)
332 * we detect this situation by noting that child's balance is not
336 if (child_bal
!= right_heavy
) {
339 * compute new balance of nodes
341 * If child used to be left heavy (now balanced) we reduced
342 * the height of this sub-tree -- used in "return...;" below
344 child_bal
+= right_heavy
; /* adjust towards right */
347 * move "cright" to be node's left child
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
);
357 * move node to be child's right child
359 child
->avl_child
[right
] = node
;
360 AVL_SETBALANCE(node
, -child_bal
);
361 AVL_SETCHILD(node
, right
);
362 AVL_SETPARENT(node
, child
);
365 * update the pointer into this subtree
367 AVL_SETBALANCE(child
, child_bal
);
368 AVL_SETCHILD(child
, which_child
);
369 AVL_SETPARENT(child
, parent
);
371 parent
->avl_child
[which_child
] = child
;
373 tree
->avl_root
= child
;
375 return (child_bal
== 0);
380 * case 2 : When node is left heavy, but child is right heavy we use
381 * a different rotation.
401 * (child b:?) (node b:?)
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
411 gchild
= child
->avl_child
[right
];
412 gleft
= gchild
->avl_child
[left
];
413 gright
= gchild
->avl_child
[right
];
416 * move gright to left child of node and
418 * move gleft to right child of node
420 node
->avl_child
[left
] = gright
;
421 if (gright
!= NULL
) {
422 AVL_SETPARENT(gright
, node
);
423 AVL_SETCHILD(gright
, left
);
426 child
->avl_child
[right
] = gleft
;
428 AVL_SETPARENT(gleft
, child
);
429 AVL_SETCHILD(gleft
, right
);
433 * move child to left child of gchild and
435 * move node to right child of gchild and
437 * fixup parent of all this to point to gchild
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
);
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
);
450 AVL_SETBALANCE(gchild
, 0);
451 AVL_SETPARENT(gchild
, parent
);
452 AVL_SETCHILD(gchild
, which_child
);
454 parent
->avl_child
[which_child
] = gchild
;
456 tree
->avl_root
= gchild
;
458 return (1); /* the new tree is always shorter */
463 * Insert a new node into an AVL tree at the specified (from avl_find()) place.
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.
469 * After the node is inserted, a single rotation further up the tree may
470 * be necessary to maintain an acceptable AVL balance.
473 avl_insert(avl_tree_t
*tree
, void *new_data
, avl_index_t where
)
476 avl_node_t
*parent
= AVL_INDEX2NODE(where
);
479 int which_child
= AVL_INDEX2CHILD(where
);
480 size_t off
= tree
->avl_offset
;
484 ASSERT(((uintptr_t)new_data
& 0x7) == 0);
487 node
= AVL_DATA2NODE(new_data
, off
);
490 * First, add the node to the tree at the indicated position.
492 ++tree
->avl_numnodes
;
494 node
->avl_child
[0] = NULL
;
495 node
->avl_child
[1] = NULL
;
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
;
504 ASSERT(tree
->avl_root
== NULL
);
505 tree
->avl_root
= node
;
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.
519 * Compute the new balance
521 old_balance
= AVL_XBALANCE(node
);
522 new_balance
= old_balance
+ avl_child2balance
[which_child
];
525 * If we introduced equal balance, then we are done immediately
527 if (new_balance
== 0) {
528 AVL_SETBALANCE(node
, 0);
533 * If both old and new are not zero we went
534 * from -1 to -2 balance, do a rotation.
536 if (old_balance
!= 0)
539 AVL_SETBALANCE(node
, new_balance
);
540 parent
= AVL_XPARENT(node
);
541 which_child
= AVL_XCHILD(node
);
545 * perform a rotation to fix the tree and return
547 (void) avl_rotation(tree
, node
, new_balance
);
551 * Insert "new_data" in "tree" in the given "direction" either after or
552 * before (AVL_AFTER, AVL_BEFORE) the data "here".
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.
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.
570 int child
= direction
; /* rely on AVL_BEFORE == 0, AVL_AFTER == 1 */
575 ASSERT(tree
!= NULL
);
576 ASSERT(new_data
!= NULL
);
577 ASSERT(here
!= NULL
);
578 ASSERT(direction
== AVL_BEFORE
|| direction
== AVL_AFTER
);
581 * If corresponding child of node is not NULL, go to the neighboring
582 * node and reverse the insertion direction.
584 node
= AVL_DATA2NODE(here
, tree
->avl_offset
);
587 diff
= tree
->avl_compar(new_data
, here
);
588 ASSERT(-1 <= diff
&& diff
<= 1);
590 ASSERT(diff
> 0 ? child
== 1 : child
== 0);
593 if (node
->avl_child
[child
] != NULL
) {
594 node
= node
->avl_child
[child
];
596 while (node
->avl_child
[child
] != NULL
) {
598 diff
= tree
->avl_compar(new_data
,
599 AVL_NODE2DATA(node
, tree
->avl_offset
));
600 ASSERT(-1 <= diff
&& diff
<= 1);
602 ASSERT(diff
> 0 ? child
== 1 : child
== 0);
604 node
= node
->avl_child
[child
];
607 diff
= tree
->avl_compar(new_data
,
608 AVL_NODE2DATA(node
, tree
->avl_offset
));
609 ASSERT(-1 <= diff
&& diff
<= 1);
611 ASSERT(diff
> 0 ? child
== 1 : child
== 0);
614 ASSERT(node
->avl_child
[child
] == NULL
);
616 avl_insert(tree
, new_data
, AVL_MKINDEX(node
, child
));
620 * Add a new node to an AVL tree.
623 avl_add(avl_tree_t
*tree
, void *new_node
)
628 * This is unfortunate. We want to call panic() here, even for
629 * non-DEBUG kernels. In userland, however, we can't depend on anything
630 * in libc or else the rtld build process gets confused. So, all we can
631 * do in userland is resort to a normal ASSERT().
633 if (avl_find(tree
, new_node
, &where
) != NULL
)
635 panic("avl_find() succeeded inside avl_add()");
639 avl_insert(tree
, new_node
, where
);
643 * Delete a node from the AVL tree. Deletion is similar to insertion, but
644 * with 2 complications.
646 * First, we may be deleting an interior node. Consider the following subtree:
654 * When we are deleting node (d), we find and bring up an adjacent valued leaf
655 * node, say (c), to take the interior node's place. In the code this is
656 * handled by temporarily swapping (d) and (c) in the tree and then using
657 * common code to delete (d) from the leaf position.
659 * Secondly, an interior deletion from a deep tree may require more than one
660 * rotation to fix the balance. This is handled by moving up the tree through
661 * parents and applying rotations as needed. The return value from
662 * avl_rotation() is used to detect when a subtree did not change overall
663 * height due to a rotation.
666 avl_remove(avl_tree_t
*tree
, void *data
)
677 size_t off
= tree
->avl_offset
;
681 delete = AVL_DATA2NODE(data
, off
);
684 * Deletion is easiest with a node that has at most 1 child.
685 * We swap a node with 2 children with a sequentially valued
686 * neighbor node. That node will have at most 1 child. Note this
687 * has no effect on the ordering of the remaining nodes.
689 * As an optimization, we choose the greater neighbor if the tree
690 * is right heavy, otherwise the left neighbor. This reduces the
691 * number of rotations needed.
693 if (delete->avl_child
[0] != NULL
&& delete->avl_child
[1] != NULL
) {
696 * choose node to swap from whichever side is taller
698 old_balance
= AVL_XBALANCE(delete);
699 left
= avl_balance2child
[old_balance
+ 1];
703 * get to the previous value'd node
704 * (down 1 left, as far as possible right)
706 for (node
= delete->avl_child
[left
];
707 node
->avl_child
[right
] != NULL
;
708 node
= node
->avl_child
[right
])
712 * create a temp placeholder for 'node'
713 * move 'node' to delete's spot in the tree
718 if (node
->avl_child
[left
] == node
)
719 node
->avl_child
[left
] = &tmp
;
721 parent
= AVL_XPARENT(node
);
723 parent
->avl_child
[AVL_XCHILD(node
)] = node
;
725 tree
->avl_root
= node
;
726 AVL_SETPARENT(node
->avl_child
[left
], node
);
727 AVL_SETPARENT(node
->avl_child
[right
], node
);
730 * Put tmp where node used to be (just temporary).
731 * It always has a parent and at most 1 child.
734 parent
= AVL_XPARENT(delete);
735 parent
->avl_child
[AVL_XCHILD(delete)] = delete;
736 which_child
= (delete->avl_child
[1] != 0);
737 if (delete->avl_child
[which_child
] != NULL
)
738 AVL_SETPARENT(delete->avl_child
[which_child
], delete);
743 * Here we know "delete" is at least partially a leaf node. It can
744 * be easily removed from the tree.
746 ASSERT(tree
->avl_numnodes
> 0);
747 --tree
->avl_numnodes
;
748 parent
= AVL_XPARENT(delete);
749 which_child
= AVL_XCHILD(delete);
750 if (delete->avl_child
[0] != NULL
)
751 node
= delete->avl_child
[0];
753 node
= delete->avl_child
[1];
756 * Connect parent directly to node (leaving out delete).
759 AVL_SETPARENT(node
, parent
);
760 AVL_SETCHILD(node
, which_child
);
762 if (parent
== NULL
) {
763 tree
->avl_root
= node
;
766 parent
->avl_child
[which_child
] = node
;
770 * Since the subtree is now shorter, begin adjusting parent balances
771 * and performing any needed rotations.
776 * Move up the tree and adjust the balance
778 * Capture the parent and which_child values for the next
779 * iteration before any rotations occur.
782 old_balance
= AVL_XBALANCE(node
);
783 new_balance
= old_balance
- avl_child2balance
[which_child
];
784 parent
= AVL_XPARENT(node
);
785 which_child
= AVL_XCHILD(node
);
788 * If a node was in perfect balance but isn't anymore then
789 * we can stop, since the height didn't change above this point
792 if (old_balance
== 0) {
793 AVL_SETBALANCE(node
, new_balance
);
798 * If the new balance is zero, we don't need to rotate
800 * need a rotation to fix the balance.
801 * If the rotation doesn't change the height
802 * of the sub-tree we have finished adjusting.
804 if (new_balance
== 0)
805 AVL_SETBALANCE(node
, new_balance
);
806 else if (!avl_rotation(tree
, node
, new_balance
))
808 } while (parent
!= NULL
);
811 #define AVL_REINSERT(tree, obj) \
812 avl_remove((tree), (obj)); \
813 avl_add((tree), (obj))
816 avl_update_lt(avl_tree_t
*t
, void *obj
)
820 ASSERT(((neighbor
= AVL_NEXT(t
, obj
)) == NULL
) ||
821 (t
->avl_compar(obj
, neighbor
) <= 0));
823 neighbor
= AVL_PREV(t
, obj
);
824 if ((neighbor
!= NULL
) && (t
->avl_compar(obj
, neighbor
) < 0)) {
825 AVL_REINSERT(t
, obj
);
833 avl_update_gt(avl_tree_t
*t
, void *obj
)
837 ASSERT(((neighbor
= AVL_PREV(t
, obj
)) == NULL
) ||
838 (t
->avl_compar(obj
, neighbor
) >= 0));
840 neighbor
= AVL_NEXT(t
, obj
);
841 if ((neighbor
!= NULL
) && (t
->avl_compar(obj
, neighbor
) > 0)) {
842 AVL_REINSERT(t
, obj
);
850 avl_update(avl_tree_t
*t
, void *obj
)
854 neighbor
= AVL_PREV(t
, obj
);
855 if ((neighbor
!= NULL
) && (t
->avl_compar(obj
, neighbor
) < 0)) {
856 AVL_REINSERT(t
, obj
);
860 neighbor
= AVL_NEXT(t
, obj
);
861 if ((neighbor
!= NULL
) && (t
->avl_compar(obj
, neighbor
) > 0)) {
862 AVL_REINSERT(t
, obj
);
870 * initialize a new AVL tree
873 avl_create(avl_tree_t
*tree
, int (*compar
) (const void *, const void *),
874 size_t size
, size_t offset
)
879 ASSERT(size
>= offset
+ sizeof (avl_node_t
));
881 ASSERT((offset
& 0x7) == 0);
884 tree
->avl_compar
= compar
;
885 tree
->avl_root
= NULL
;
886 tree
->avl_numnodes
= 0;
887 tree
->avl_size
= size
;
888 tree
->avl_offset
= offset
;
896 avl_destroy(avl_tree_t
*tree
)
899 ASSERT(tree
->avl_numnodes
== 0);
900 ASSERT(tree
->avl_root
== NULL
);
905 * Return the number of nodes in an AVL tree.
908 avl_numnodes(avl_tree_t
*tree
)
911 return (tree
->avl_numnodes
);
915 avl_is_empty(avl_tree_t
*tree
)
918 return (tree
->avl_numnodes
== 0);
921 #define CHILDBIT (1L)
924 * Post-order tree walk used to visit all tree nodes and destroy the tree
925 * in post order. This is used for destroying a tree w/o paying any cost
926 * for rebalancing it.
930 * void *cookie = NULL;
933 * while ((node = avl_destroy_nodes(tree, &cookie)) != NULL)
937 * The cookie is really an avl_node_t to the current node's parent and
938 * an indication of which child you looked at last.
940 * On input, a cookie value of CHILDBIT indicates the tree is done.
943 avl_destroy_nodes(avl_tree_t
*tree
, void **cookie
)
949 size_t off
= tree
->avl_offset
;
952 * Initial calls go to the first node or it's right descendant.
954 if (*cookie
== NULL
) {
955 first
= avl_first(tree
);
958 * deal with an empty tree
961 *cookie
= (void *)CHILDBIT
;
965 node
= AVL_DATA2NODE(first
, off
);
966 parent
= AVL_XPARENT(node
);
967 goto check_right_side
;
971 * If there is no parent to return to we are done.
973 parent
= (avl_node_t
*)((uintptr_t)(*cookie
) & ~CHILDBIT
);
974 if (parent
== NULL
) {
975 if (tree
->avl_root
!= NULL
) {
976 ASSERT(tree
->avl_numnodes
== 1);
977 tree
->avl_root
= NULL
;
978 tree
->avl_numnodes
= 0;
984 * Remove the child pointer we just visited from the parent and tree.
986 child
= (uintptr_t)(*cookie
) & CHILDBIT
;
987 parent
->avl_child
[child
] = NULL
;
988 ASSERT(tree
->avl_numnodes
> 1);
989 --tree
->avl_numnodes
;
992 * If we just did a right child or there isn't one, go up to parent.
994 if (child
== 1 || parent
->avl_child
[1] == NULL
) {
996 parent
= AVL_XPARENT(parent
);
1001 * Do parent's right child, then leftmost descendent.
1003 node
= parent
->avl_child
[1];
1004 while (node
->avl_child
[0] != NULL
) {
1006 node
= node
->avl_child
[0];
1010 * If here, we moved to a left child. It may have one
1011 * child on the right (when balance == +1).
1014 if (node
->avl_child
[1] != NULL
) {
1015 ASSERT(AVL_XBALANCE(node
) == 1);
1017 node
= node
->avl_child
[1];
1018 ASSERT(node
->avl_child
[0] == NULL
&&
1019 node
->avl_child
[1] == NULL
);
1021 ASSERT(AVL_XBALANCE(node
) <= 0);
1025 if (parent
== NULL
) {
1026 *cookie
= (void *)CHILDBIT
;
1027 ASSERT(node
== tree
->avl_root
);
1029 *cookie
= (void *)((uintptr_t)parent
| AVL_XCHILD(node
));
1032 return (AVL_NODE2DATA(node
, off
));