1 /* Abstract AVL Tree Generic C Package.
2 ** Implementation generation header file.
4 ** This code is in the public domain. See cavl_tree.html for interface
7 ** Version: 1.5 Author: Walt Karas
13 #undef L__EST_LONG_BIT
16 #undef L__MASK_HIGH_BIT
18 #undef L__BIT_ARR_DEFN
19 #undef L__BIT_ARR_CLEAR
24 #undef L__BIT_ARR_LONGS
26 #undef L__CHECK_READ_ERROR
27 #undef L__CHECK_READ_ERROR_INV_DEPTH
29 #undef L__BALANCE_PARAM_PREFIX
33 #define L__ AVL_UNIQUE
41 /* Determine correct storage class for functions */
54 #define L__SIZE AVL_SIZE
58 #define L__SIZE unsigned long
62 #define L__MASK_HIGH_BIT ((int) ~ ((~ (unsigned) 0) >> 1))
64 /* ANSI C/ISO C++ require that a long have at least 32 bits. Set
65 ** L__EST_LONG_BIT to be the greatest multiple of 8 in the range
66 ** 32 - 64 (inclusive) that is less than or equal to the number of
70 #if (((LONG_MAX >> 31) >> 7) == 0)
72 #define L__EST_LONG_BIT 32
74 #elif (((LONG_MAX >> 31) >> 15) == 0)
76 #define L__EST_LONG_BIT 40
78 #elif (((LONG_MAX >> 31) >> 23) == 0)
80 #define L__EST_LONG_BIT 48
82 #elif (((LONG_MAX >> 31) >> 31) == 0)
84 #define L__EST_LONG_BIT 56
88 #define L__EST_LONG_BIT 64
92 #define L__LONG_BIT (sizeof(long) * CHAR_BIT)
94 #if ((AVL_MAX_DEPTH) > L__EST_LONG_BIT)
96 /* The maximum depth may be greater than the number of bits in a long,
97 ** so multiple longs are needed to hold a bit array indexed by node
100 #define L__BIT_ARR_LONGS (((AVL_MAX_DEPTH) + L__LONG_BIT - 1) / L__LONG_BIT)
102 #define L__BIT_ARR_DEFN(NAME) unsigned long NAME[L__BIT_ARR_LONGS];
104 #define L__BIT_ARR_CLEAR(NAME) memset(NAME, 0, sizeof(NAME));
106 #define L__BIT_ARR_VAL(BIT_ARR, BIT_NUM) \
107 ((BIT_ARR)[(BIT_NUM) / L__LONG_BIT] & (1L << ((BIT_NUM) % L__LONG_BIT)))
109 #define L__BIT_ARR_0(BIT_ARR, BIT_NUM) \
110 (BIT_ARR)[(BIT_NUM) / L__LONG_BIT] &= ~(1L << ((BIT_NUM) % L__LONG_BIT));
112 #define L__BIT_ARR_1(BIT_ARR, BIT_NUM) \
113 (BIT_ARR)[(BIT_NUM) / L__LONG_BIT] |= 1L << ((BIT_NUM) % L__LONG_BIT);
115 #define L__BIT_ARR_ALL(BIT_ARR, BIT_VAL) \
116 { int i = L__BIT_ARR_LONGS; do (BIT_ARR)[--i] = 0L - (BIT_VAL); while(i); }
118 #else /* The bit array can definitely fit in one long */
120 #define L__BIT_ARR_DEFN(NAME) unsigned long NAME;
122 #define L__BIT_ARR_CLEAR(NAME) NAME = 0;
124 #define L__BIT_ARR_VAL(BIT_ARR, BIT_NUM) ((BIT_ARR) & (1L << (BIT_NUM)))
126 #define L__BIT_ARR_0(BIT_ARR, BIT_NUM) (BIT_ARR) &= ~(1L << (BIT_NUM));
128 #define L__BIT_ARR_1(BIT_ARR, BIT_NUM) (BIT_ARR) |= 1L << (BIT_NUM);
130 #define L__BIT_ARR_ALL(BIT_ARR, BIT_VAL) (BIT_ARR) = 0L - (BIT_VAL);
134 #ifdef AVL_READ_ERRORS_HAPPEN
136 #define L__CHECK_READ_ERROR(ERROR_RETURN) \
137 { if (AVL_READ_ERROR) return(ERROR_RETURN); }
141 #define L__CHECK_READ_ERROR(ERROR_RETURN)
145 /* The presumed reason that an instantiation places additional fields
146 ** inside the AVL tree structure is that the SET_ and GET_ macros
147 ** need these fields. The "balance" function does not explicitly use
148 ** any fields in the AVL tree structure, so only pass an AVL tree
149 ** structure pointer to "balance" if it has instantiation-specific
150 ** fields that are (presumably) needed by the SET_/GET_ calls within
153 #ifdef AVL_INSIDE_STRUCT
155 #define L__BALANCE_PARAM_CALL_PREFIX L__tree,
156 #define L__BALANCE_PARAM_DECL_PREFIX L__(avl) *L__tree,
160 #define L__BALANCE_PARAM_CALL_PREFIX
161 #define L__BALANCE_PARAM_DECL_PREFIX
167 #define L__IMPL_MASK (AVL_IMPL_MASK)
171 /* Define all functions. */
172 #define L__IMPL_MASK AVL_IMPL_ALL
176 #if (L__IMPL_MASK & AVL_IMPL_INIT)
178 L__SC
void L__(init
)(L__(avl
) *L__tree
) { AVL_SET_ROOT(L__tree
, AVL_NULL
); }
182 #if (L__IMPL_MASK & AVL_IMPL_IS_EMPTY)
184 L__SC
int L__(is_empty
)(L__(avl
) *L__tree
)
185 { return(L__tree
->root
== AVL_NULL
); }
189 /* Put the private balance function in the same compilation module as
190 ** the insert function. */
191 #if (L__IMPL_MASK & AVL_IMPL_INSERT)
193 /* Balances subtree, returns handle of root node of subtree after balancing.
195 static L__SC AVL_HANDLE
L__(balance
)(L__BALANCE_PARAM_DECL_PREFIX AVL_HANDLE bal_h
)
199 /* Either the "greater than" or the "less than" subtree of
200 ** this node has to be 2 levels deeper (or else it wouldn't
203 if (AVL_GET_BALANCE_FACTOR(bal_h
) > 0)
205 /* "Greater than" subtree is deeper. */
207 deep_h
= AVL_GET_GREATER(bal_h
, 1);
209 L__CHECK_READ_ERROR(AVL_NULL
)
211 if (AVL_GET_BALANCE_FACTOR(deep_h
) < 0)
215 AVL_HANDLE old_h
= bal_h
;
216 bal_h
= AVL_GET_LESS(deep_h
, 1);
217 L__CHECK_READ_ERROR(AVL_NULL
)
218 AVL_SET_GREATER(old_h
, AVL_GET_LESS(bal_h
, 1))
219 AVL_SET_LESS(deep_h
, AVL_GET_GREATER(bal_h
, 1))
220 AVL_SET_LESS(bal_h
, old_h
)
221 AVL_SET_GREATER(bal_h
, deep_h
)
223 bf
= AVL_GET_BALANCE_FACTOR(bal_h
);
228 AVL_SET_BALANCE_FACTOR(old_h
, -1)
229 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
233 AVL_SET_BALANCE_FACTOR(deep_h
, 1)
234 AVL_SET_BALANCE_FACTOR(old_h
, 0)
236 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
240 AVL_SET_BALANCE_FACTOR(old_h
, 0)
241 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
246 AVL_SET_GREATER(bal_h
, AVL_GET_LESS(deep_h
, 0))
247 AVL_SET_LESS(deep_h
, bal_h
)
248 if (AVL_GET_BALANCE_FACTOR(deep_h
) == 0)
250 AVL_SET_BALANCE_FACTOR(deep_h
, -1)
251 AVL_SET_BALANCE_FACTOR(bal_h
, 1)
255 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
256 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
263 /* "Less than" subtree is deeper. */
265 deep_h
= AVL_GET_LESS(bal_h
, 1);
266 L__CHECK_READ_ERROR(AVL_NULL
)
268 if (AVL_GET_BALANCE_FACTOR(deep_h
) > 0)
271 AVL_HANDLE old_h
= bal_h
;
272 bal_h
= AVL_GET_GREATER(deep_h
, 1);
273 L__CHECK_READ_ERROR(AVL_NULL
)
274 AVL_SET_LESS(old_h
, AVL_GET_GREATER(bal_h
, 0))
275 AVL_SET_GREATER(deep_h
, AVL_GET_LESS(bal_h
, 0))
276 AVL_SET_GREATER(bal_h
, old_h
)
277 AVL_SET_LESS(bal_h
, deep_h
)
279 bf
= AVL_GET_BALANCE_FACTOR(bal_h
);
284 AVL_SET_BALANCE_FACTOR(old_h
, 1)
285 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
289 AVL_SET_BALANCE_FACTOR(deep_h
, -1)
290 AVL_SET_BALANCE_FACTOR(old_h
, 0)
292 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
296 AVL_SET_BALANCE_FACTOR(old_h
, 0)
297 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
302 AVL_SET_LESS(bal_h
, AVL_GET_GREATER(deep_h
, 0))
303 AVL_SET_GREATER(deep_h
, bal_h
)
304 if (AVL_GET_BALANCE_FACTOR(deep_h
) == 0)
306 AVL_SET_BALANCE_FACTOR(deep_h
, 1)
307 AVL_SET_BALANCE_FACTOR(bal_h
, -1)
311 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
312 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
321 L__SC AVL_HANDLE
L__(insert
)(L__(avl
) *L__tree
, AVL_HANDLE h
)
323 AVL_SET_LESS(h
, AVL_NULL
)
324 AVL_SET_GREATER(h
, AVL_NULL
)
325 AVL_SET_BALANCE_FACTOR(h
, 0)
327 if (L__tree
->root
== AVL_NULL
) {
328 AVL_SET_ROOT(L__tree
, h
);
331 /* Last unbalanced node encountered in search for insertion point. */
332 AVL_HANDLE unbal
= AVL_NULL
;
333 /* Parent of last unbalanced node. */
334 AVL_HANDLE parent_unbal
= AVL_NULL
;
335 /* Balance factor of last unbalanced node. */
338 /* Zero-based depth in tree. */
339 unsigned depth
= 0, unbal_depth
= 0;
341 /* Records a path into the tree. If bit n is true, indicates
342 ** take greater branch from the nth node in the path, otherwise
343 ** take the less branch. bit 0 gives branch from root, and
345 L__BIT_ARR_DEFN(branch
)
347 AVL_HANDLE hh
= L__tree
->root
;
348 AVL_HANDLE parent
= AVL_NULL
;
351 L__BIT_ARR_CLEAR(branch
)
355 if (AVL_GET_BALANCE_FACTOR(hh
) != 0)
358 parent_unbal
= parent
;
361 cmp
= AVL_COMPARE_NODE_NODE(h
, hh
);
368 hh
= AVL_GET_GREATER(hh
, 1);
369 L__BIT_ARR_1(branch
, depth
)
373 hh
= AVL_GET_LESS(hh
, 1);
374 L__BIT_ARR_0(branch
, depth
)
376 L__CHECK_READ_ERROR(AVL_NULL
)
379 while (hh
!= AVL_NULL
);
381 /* Add node to insert as leaf of tree. */
383 AVL_SET_LESS(parent
, h
)
385 AVL_SET_GREATER(parent
, h
)
389 if (unbal
== AVL_NULL
)
393 cmp
= L__BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
395 unbal_bf
= AVL_GET_BALANCE_FACTOR(unbal
);
400 hh
= cmp
< 0 ? AVL_GET_LESS(unbal
, 1) : AVL_GET_GREATER(unbal
, 1);
401 L__CHECK_READ_ERROR(AVL_NULL
)
402 if ((unbal_bf
!= -2) && (unbal_bf
!= 2))
404 /* No rebalancing of tree is necessary. */
405 AVL_SET_BALANCE_FACTOR(unbal
, unbal_bf
)
413 cmp
= L__BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
417 AVL_SET_BALANCE_FACTOR(hh
, -1)
418 hh
= AVL_GET_LESS(hh
, 1);
422 AVL_SET_BALANCE_FACTOR(hh
, 1)
423 hh
= AVL_GET_GREATER(hh
, 1);
425 L__CHECK_READ_ERROR(AVL_NULL
)
428 if (unbal
!= AVL_NULL
)
430 unbal
= L__(balance
)(L__BALANCE_PARAM_CALL_PREFIX unbal
);
431 L__CHECK_READ_ERROR(AVL_NULL
)
432 if (parent_unbal
== AVL_NULL
)
434 AVL_SET_ROOT(L__tree
, unbal
);
438 depth
= unbal_depth
- 1;
439 cmp
= L__BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
441 AVL_SET_LESS(parent_unbal
, unbal
)
443 AVL_SET_GREATER(parent_unbal
, unbal
)
454 #if (L__IMPL_MASK & AVL_IMPL_SEARCH)
456 L__SC AVL_HANDLE
L__(search
)(L__(avl
) *L__tree
, AVL_KEY k
, avl_search_type st
)
459 AVL_HANDLE match_h
= AVL_NULL
;
460 AVL_HANDLE h
= L__tree
->root
;
464 else if (st
& AVL_GREATER
)
469 while (h
!= AVL_NULL
)
471 cmp
= AVL_COMPARE_KEY_NODE(k
, h
);
481 else if (target_cmp
!= 0)
482 if (!((cmp
^ target_cmp
) & L__MASK_HIGH_BIT
))
483 /* cmp and target_cmp are both positive or both negative. */
485 h
= cmp
< 0 ? AVL_GET_LESS(h
, 1) : AVL_GET_GREATER(h
, 1);
486 L__CHECK_READ_ERROR(AVL_NULL
)
494 #if (L__IMPL_MASK & AVL_IMPL_SEARCH_LEAST)
496 L__SC AVL_HANDLE
L__(search_least
)(L__(avl
) *L__tree
)
498 AVL_HANDLE h
= L__tree
->root
;
499 AVL_HANDLE parent
= AVL_NULL
;
501 while (h
!= AVL_NULL
)
504 h
= AVL_GET_LESS(h
, 1);
505 L__CHECK_READ_ERROR(AVL_NULL
)
513 L__SC AVL_HANDLE
L__(search_root
)(L__(avl
) *L__tree
)
515 return L__tree
->root
;
519 #if (L__IMPL_MASK & AVL_IMPL_SEARCH_GREATEST)
521 L__SC AVL_HANDLE
L__(search_greatest
)(L__(avl
) *L__tree
)
523 AVL_HANDLE h
= L__tree
->root
;
524 AVL_HANDLE parent
= AVL_NULL
;
526 while (h
!= AVL_NULL
)
529 h
= AVL_GET_GREATER(h
, 1);
530 L__CHECK_READ_ERROR(AVL_NULL
)
538 #if (L__IMPL_MASK & AVL_IMPL_REMOVE)
540 /* Prototype of balance function (called by remove) in case not in
541 ** same compilation unit.
543 L__SC AVL_HANDLE
L__(balance
)(L__BALANCE_PARAM_DECL_PREFIX AVL_HANDLE bal_h
);
545 L__SC AVL_HANDLE
L__(remove
)(L__(avl
) *L__tree
, AVL_KEY k
)
547 /* Zero-based depth in tree. */
548 unsigned depth
= 0, rm_depth
;
550 /* Records a path into the tree. If bit n is true, indicates
551 ** take greater branch from the nth node in the path, otherwise
552 ** take the less branch. bit 0 gives branch from root, and
554 L__BIT_ARR_DEFN(branch
)
556 AVL_HANDLE h
= L__tree
->root
;
557 AVL_HANDLE parent
= AVL_NULL
;
560 int cmp
, cmp_shortened_sub_with_path
= 0;
564 AVL_HANDLE parent_rm
;
566 L__BIT_ARR_CLEAR(branch
)
571 /* No node in tree with given key. */
573 cmp
= AVL_COMPARE_KEY_NODE(k
, h
);
575 /* Found node to remove. */
580 h
= AVL_GET_GREATER(h
, 1);
581 L__BIT_ARR_1(branch
, depth
)
585 h
= AVL_GET_LESS(h
, 1);
586 L__BIT_ARR_0(branch
, depth
)
588 L__CHECK_READ_ERROR(AVL_NULL
)
590 cmp_shortened_sub_with_path
= cmp
;
596 /* If the node to remove is not a leaf node, we need to get a
597 ** leaf node, or a node with a single leaf as its child, to put
598 ** in the place of the node to remove. We will get the greatest
599 ** node in the less subtree (of the node to remove), or the least
600 ** node in the greater subtree. We take the leaf node from the
601 ** deeper subtree, if there is one. */
603 if (AVL_GET_BALANCE_FACTOR(h
) < 0)
605 child
= AVL_GET_LESS(h
, 1);
606 L__BIT_ARR_0(branch
, depth
)
611 child
= AVL_GET_GREATER(h
, 1);
612 L__BIT_ARR_1(branch
, depth
)
615 L__CHECK_READ_ERROR(AVL_NULL
)
618 if (child
!= AVL_NULL
)
627 child
= AVL_GET_LESS(h
, 1);
628 L__BIT_ARR_0(branch
, depth
)
632 child
= AVL_GET_GREATER(h
, 1);
633 L__BIT_ARR_1(branch
, depth
)
635 L__CHECK_READ_ERROR(AVL_NULL
)
638 while (child
!= AVL_NULL
);
641 /* Only went through do loop once. Deleted node will be replaced
642 ** in the tree structure by one of its immediate children. */
643 cmp_shortened_sub_with_path
= -cmp
;
645 cmp_shortened_sub_with_path
= cmp
;
647 /* Get the handle of the opposite child, which may not be null. */
648 child
= cmp
> 0 ? AVL_GET_LESS(h
, 0) : AVL_GET_GREATER(h
, 0);
651 if (parent
== AVL_NULL
) {
652 /* There were only 1 or 2 nodes in this tree. */
653 AVL_SET_ROOT(L__tree
, child
);
655 else if (cmp_shortened_sub_with_path
< 0)
656 AVL_SET_LESS(parent
, child
)
658 AVL_SET_GREATER(parent
, child
)
660 /* "path" is the parent of the subtree being eliminated or reduced
661 ** from a depth of 2 to 1. If "path" is the node to be removed, we
662 ** set path to the node we're about to poke into the position of the
663 ** node to be removed. */
664 path
= parent
== rm
? h
: parent
;
668 /* Poke in the replacement for the node to be removed. */
669 AVL_SET_LESS(h
, AVL_GET_LESS(rm
, 0))
670 AVL_SET_GREATER(h
, AVL_GET_GREATER(rm
, 0))
671 AVL_SET_BALANCE_FACTOR(h
, AVL_GET_BALANCE_FACTOR(rm
))
672 if (parent_rm
== AVL_NULL
) {
673 AVL_SET_ROOT(L__tree
, h
);
677 depth
= rm_depth
- 1;
678 if (L__BIT_ARR_VAL(branch
, depth
))
679 AVL_SET_GREATER(parent_rm
, h
)
681 AVL_SET_LESS(parent_rm
, h
)
685 if (path
!= AVL_NULL
)
687 /* Create a temporary linked list from the parent of the path node
688 ** to the root node. */
694 if (L__BIT_ARR_VAL(branch
, depth
))
696 child
= AVL_GET_GREATER(h
, 1);
697 AVL_SET_GREATER(h
, parent
)
701 child
= AVL_GET_LESS(h
, 1);
702 AVL_SET_LESS(h
, parent
)
704 L__CHECK_READ_ERROR(AVL_NULL
)
710 /* Climb from the path node to the root node using the linked
711 ** list, restoring the tree structure and rebalancing as necessary.
714 cmp
= cmp_shortened_sub_with_path
;
719 bf
= AVL_GET_BALANCE_FACTOR(h
);
724 if ((bf
== -2) || (bf
== 2))
726 h
= L__(balance
)(L__BALANCE_PARAM_CALL_PREFIX h
);
727 L__CHECK_READ_ERROR(AVL_NULL
)
728 bf
= AVL_GET_BALANCE_FACTOR(h
);
731 AVL_SET_BALANCE_FACTOR(h
, bf
)
732 reduced_depth
= (bf
== 0);
734 if (parent
== AVL_NULL
)
739 cmp
= L__BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
742 parent
= AVL_GET_LESS(h
, 1);
743 AVL_SET_LESS(h
, child
)
747 parent
= AVL_GET_GREATER(h
, 1);
748 AVL_SET_GREATER(h
, child
)
750 L__CHECK_READ_ERROR(AVL_NULL
)
752 AVL_SET_ROOT(L__tree
, h
);
760 #if (L__IMPL_MASK & AVL_IMPL_SUBST)
762 L__SC AVL_HANDLE
L__(subst
)(L__(avl
) *L__tree
, AVL_HANDLE new_node
)
764 AVL_HANDLE h
= L__tree
->root
;
765 AVL_HANDLE parent
= AVL_NULL
;
766 int cmp
, last_cmp
= 0;
768 /* Search for node already in tree with same key. */
772 /* No node in tree with same key as new node. */
774 cmp
= AVL_COMPARE_NODE_NODE(new_node
, h
);
776 /* Found the node to substitute new one for. */
780 h
= cmp
< 0 ? AVL_GET_LESS(h
, 1) : AVL_GET_GREATER(h
, 1);
781 L__CHECK_READ_ERROR(AVL_NULL
)
784 /* Copy tree housekeeping fields from node in tree to new node. */
785 AVL_SET_LESS(new_node
, AVL_GET_LESS(h
, 0))
786 AVL_SET_GREATER(new_node
, AVL_GET_GREATER(h
, 0))
787 AVL_SET_BALANCE_FACTOR(new_node
, AVL_GET_BALANCE_FACTOR(h
))
789 if (parent
== AVL_NULL
)
791 /* New node is also new root. */
792 AVL_SET_ROOT(L__tree
, new_node
);
796 /* Make parent point to new node. */
798 AVL_SET_LESS(parent
, new_node
)
800 AVL_SET_GREATER(parent
, new_node
)
808 #ifdef AVL_BUILD_ITER_TYPE
810 #if (L__IMPL_MASK & AVL_IMPL_BUILD)
812 L__SC
int L__(build
)(
813 L__(avl
) *L__tree
, AVL_BUILD_ITER_TYPE p
, L__SIZE num_nodes
)
815 /* Gives path to subtree being built. If bit n is false, branch
816 ** less from the node at depth n, if true branch greater. */
817 L__BIT_ARR_DEFN(branch
)
819 /* If bit n is true, then for the current subtree at depth n, its
820 ** greater subtree has one more node than its less subtree. */
823 /* Depth of root node of current subtree. */
826 /* Number of nodes in current subtree. */
827 L__SIZE num_sub
= num_nodes
;
829 /* The algorithm relies on a stack of nodes whose less subtree has
830 ** been built, but whose greater subtree has not yet been built.
831 ** The stack is implemented as linked list. The nodes are linked
832 ** together by having the "greater" handle of a node set to the
833 ** next node in the list. "less_parent" is the handle of the first
834 ** node in the list. */
835 AVL_HANDLE less_parent
= AVL_NULL
;
837 /* h is root of current subtree, child is one of its children. */
843 AVL_SET_ROOT(L__tree
, AVL_NULL
);
847 L__BIT_ARR_CLEAR(branch
)
848 L__BIT_ARR_CLEAR(rem
)
854 /* Subtract one for root of subtree. */
857 L__BIT_ARR_1(rem
, depth
)
859 L__BIT_ARR_0(rem
, depth
)
860 L__BIT_ARR_0(branch
, depth
)
867 /* Build a subtree with two nodes, slanting to greater.
868 ** I arbitrarily chose to always have the extra node in the
869 ** greater subtree when there is an odd number of nodes to
870 ** split between the two subtrees. */
872 h
= AVL_BUILD_ITER_VAL(p
);
873 L__CHECK_READ_ERROR(0)
874 AVL_BUILD_ITER_INCR(p
)
875 child
= AVL_BUILD_ITER_VAL(p
);
876 L__CHECK_READ_ERROR(0)
877 AVL_BUILD_ITER_INCR(p
)
878 AVL_SET_LESS(child
, AVL_NULL
)
879 AVL_SET_GREATER(child
, AVL_NULL
)
880 AVL_SET_BALANCE_FACTOR(child
, 0)
881 AVL_SET_GREATER(h
, child
)
882 AVL_SET_LESS(h
, AVL_NULL
)
883 AVL_SET_BALANCE_FACTOR(h
, 1)
885 else /* num_sub == 1 */
887 /* Build a subtree with one node. */
889 h
= AVL_BUILD_ITER_VAL(p
);
890 L__CHECK_READ_ERROR(0)
891 AVL_BUILD_ITER_INCR(p
)
892 AVL_SET_LESS(h
, AVL_NULL
)
893 AVL_SET_GREATER(h
, AVL_NULL
)
894 AVL_SET_BALANCE_FACTOR(h
, 0)
900 if (!L__BIT_ARR_VAL(branch
, depth
))
901 /* We've completed a less subtree. */
904 /* We've completed a greater subtree, so attach it to
905 ** its parent (that is less than it). We pop the parent
906 ** off the stack of less parents. */
909 less_parent
= AVL_GET_GREATER(h
, 1);
910 L__CHECK_READ_ERROR(0)
911 AVL_SET_GREATER(h
, child
)
912 /* num_sub = 2 * (num_sub - rem[depth]) + rem[depth] + 1 */
914 num_sub
+= L__BIT_ARR_VAL(rem
, depth
) ? 0 : 1;
915 if (num_sub
& (num_sub
- 1))
916 /* num_sub is not a power of 2. */
917 AVL_SET_BALANCE_FACTOR(h
, 0)
919 /* num_sub is a power of 2. */
920 AVL_SET_BALANCE_FACTOR(h
, 1)
923 if (num_sub
== num_nodes
)
924 /* We've completed the full tree. */
927 /* The subtree we've completed is the less subtree of the
928 ** next node in the sequence. */
931 h
= AVL_BUILD_ITER_VAL(p
);
932 L__CHECK_READ_ERROR(0)
933 AVL_BUILD_ITER_INCR(p
)
934 AVL_SET_LESS(h
, child
)
936 /* Put h into stack of less parents. */
937 AVL_SET_GREATER(h
, less_parent
)
940 /* Proceed to creating greater than subtree of h. */
941 L__BIT_ARR_1(branch
, depth
)
942 num_sub
+= L__BIT_ARR_VAL(rem
, depth
) ? 1 : 0;
945 } /* end for ( ; ; ) */
947 AVL_SET_ROOT(L__tree
, h
);
956 #if (L__IMPL_MASK & AVL_IMPL_INIT_ITER)
958 /* Initialize depth to invalid value, to indicate iterator is
959 ** invalid. (Depth is zero-base.) It's not necessary to initialize
960 ** iterators prior to passing them to the "start" function.
962 L__SC
void L__(init_iter
)(L__(iter
) *iter
) { iter
->depth
= ~0; }
966 #ifdef AVL_READ_ERRORS_HAPPEN
968 #define L__CHECK_READ_ERROR_INV_DEPTH \
969 { if (AVL_READ_ERROR) { iter->depth = ~0; return; } }
973 #define L__CHECK_READ_ERROR_INV_DEPTH
977 #if (L__IMPL_MASK & AVL_IMPL_START_ITER)
979 L__SC
void L__(start_iter
)(
980 L__(avl
) *L__tree
, L__(iter
) *iter
, AVL_KEY k
, avl_search_type st
)
982 AVL_HANDLE h
= L__tree
->root
;
986 /* Save the tree that we're going to iterate through in a
987 ** member variable. */
988 iter
->tree_
= L__tree
;
997 /* Key can be greater than key of starting node. */
999 else if (st
& AVL_GREATER
)
1000 /* Key can be less than key of starting node. */
1003 /* Key must be same as key of starting node. */
1008 cmp
= AVL_COMPARE_KEY_NODE(k
, h
);
1013 /* Equal node was sought and found as starting node. */
1019 else if (target_cmp
!= 0)
1020 if (!((cmp
^ target_cmp
) & L__MASK_HIGH_BIT
))
1021 /* cmp and target_cmp are both negative or both positive. */
1023 h
= cmp
< 0 ? AVL_GET_LESS(h
, 1) : AVL_GET_GREATER(h
, 1);
1024 L__CHECK_READ_ERROR_INV_DEPTH
1028 L__BIT_ARR_1(iter
->branch
, d
)
1030 L__BIT_ARR_0(iter
->branch
, d
)
1031 iter
->path_h
[d
++] = h
;
1037 #if (L__IMPL_MASK & AVL_IMPL_START_ITER_LEAST)
1039 L__SC
void L__(start_iter_least
)(L__(avl
) *L__tree
, L__(iter
) *iter
)
1041 AVL_HANDLE h
= L__tree
->root
;
1043 iter
->tree_
= L__tree
;
1047 L__BIT_ARR_ALL(iter
->branch
, 0)
1049 while (h
!= AVL_NULL
)
1051 if (iter
->depth
!= ~0)
1052 iter
->path_h
[iter
->depth
] = h
;
1054 h
= AVL_GET_LESS(h
, 1);
1055 L__CHECK_READ_ERROR_INV_DEPTH
1061 #if (L__IMPL_MASK & AVL_IMPL_START_ITER_GREATEST)
1063 L__SC
void L__(start_iter_greatest
)(L__(avl
) *L__tree
, L__(iter
) *iter
)
1065 AVL_HANDLE h
= L__tree
->root
;
1067 iter
->tree_
= L__tree
;
1071 L__BIT_ARR_ALL(iter
->branch
, 1)
1073 while (h
!= AVL_NULL
)
1075 if (iter
->depth
!= ~0)
1076 iter
->path_h
[iter
->depth
] = h
;
1078 h
= AVL_GET_GREATER(h
, 1);
1079 L__CHECK_READ_ERROR_INV_DEPTH
1085 #if (L__IMPL_MASK & AVL_IMPL_GET_ITER)
1087 L__SC AVL_HANDLE
L__(get_iter
)(L__(iter
) *iter
)
1089 if (iter
->depth
== ~0)
1092 return(iter
->depth
== 0 ?
1093 iter
->tree_
->root
: iter
->path_h
[iter
->depth
- 1]);
1098 #if (L__IMPL_MASK & AVL_IMPL_INCR_ITER)
1100 L__SC
void L__(incr_iter
)(L__(iter
) *iter
)
1102 #define L__tree (iter->tree_)
1104 if (iter
->depth
!= ~0)
1107 AVL_GET_GREATER((iter
->depth
== 0 ?
1108 iter
->tree_
->root
: iter
->path_h
[iter
->depth
- 1]), 1);
1109 L__CHECK_READ_ERROR_INV_DEPTH
1114 if (iter
->depth
== 0)
1121 while (L__BIT_ARR_VAL(iter
->branch
, iter
->depth
));
1124 L__BIT_ARR_1(iter
->branch
, iter
->depth
)
1125 iter
->path_h
[iter
->depth
++] = h
;
1128 h
= AVL_GET_LESS(h
, 1);
1129 L__CHECK_READ_ERROR_INV_DEPTH
1132 L__BIT_ARR_0(iter
->branch
, iter
->depth
)
1133 iter
->path_h
[iter
->depth
++] = h
;
1143 #if (L__IMPL_MASK & AVL_IMPL_DECR_ITER)
1145 L__SC
void L__(decr_iter
)(L__(iter
) *iter
)
1147 #define L__tree (iter->tree_)
1149 if (iter
->depth
!= ~0)
1152 AVL_GET_LESS((iter
->depth
== 0 ?
1153 iter
->tree_
->root
: iter
->path_h
[iter
->depth
- 1]), 1);
1154 L__CHECK_READ_ERROR_INV_DEPTH
1159 if (iter
->depth
== 0)
1166 while (!L__BIT_ARR_VAL(iter
->branch
, iter
->depth
));
1169 L__BIT_ARR_0(iter
->branch
, iter
->depth
)
1170 iter
->path_h
[iter
->depth
++] = h
;
1173 h
= AVL_GET_GREATER(h
, 1);
1174 L__CHECK_READ_ERROR_INV_DEPTH
1177 L__BIT_ARR_1(iter
->branch
, iter
->depth
)
1178 iter
->path_h
[iter
->depth
++] = h
;
1188 /* Tidy up the preprocessor symbol name space. */
1190 #undef L__EST_LONG_BIT
1192 #undef L__MASK_HIGH_BIT
1194 #undef L__BIT_ARR_DEFN
1195 #undef L__BIT_ARR_CLEAR
1196 #undef L__BIT_ARR_VAL
1199 #undef L__BIT_ARR_ALL
1200 #undef L__CHECK_READ_ERROR
1201 #undef L__CHECK_READ_ERROR_INV_DEPTH
1202 #undef L__BIT_ARR_LONGS
1204 #undef L__CHECK_READ_ERROR
1205 #undef L__CHECK_READ_ERROR_INV_DEPTH
1207 #undef L__BALANCE_PARAM_CALL_PREFIX
1208 #undef L__BALANCE_PARAM_DECL_PREFIX