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
11 #undef L__EST_LONG_BIT
14 #undef L__MASK_HIGH_BIT
16 #undef L__BIT_ARR_DEFN
21 #undef L__BIT_ARR_LONGS
23 #undef L__CHECK_READ_ERROR
24 #undef L__CHECK_READ_ERROR_INV_DEPTH
26 #undef L__BALANCE_PARAM_PREFIX
30 #define L__ AVL_UNIQUE
38 /* Determine correct storage class for functions */
51 #define L__SIZE AVL_SIZE
55 #define L__SIZE unsigned long
59 #define L__MASK_HIGH_BIT ((int) ~ ((~ (unsigned) 0) >> 1))
61 /* ANSI C/ISO C++ require that a long have at least 32 bits. Set
62 ** L__EST_LONG_BIT to be the greatest multiple of 8 in the range
63 ** 32 - 64 (inclusive) that is less than or equal to the number of
67 #if (((LONG_MAX >> 31) >> 7) == 0)
69 #define L__EST_LONG_BIT 32
71 #elif (((LONG_MAX >> 31) >> 15) == 0)
73 #define L__EST_LONG_BIT 40
75 #elif (((LONG_MAX >> 31) >> 23) == 0)
77 #define L__EST_LONG_BIT 48
79 #elif (((LONG_MAX >> 31) >> 31) == 0)
81 #define L__EST_LONG_BIT 56
85 #define L__EST_LONG_BIT 64
89 #define L__LONG_BIT (sizeof(long) * CHAR_BIT)
91 #if ((AVL_MAX_DEPTH) > L__EST_LONG_BIT)
93 /* The maximum depth may be greater than the number of bits in a long,
94 ** so multiple longs are needed to hold a bit array indexed by node
97 #define L__BIT_ARR_LONGS (((AVL_MAX_DEPTH) + L__LONG_BIT - 1) / L__LONG_BIT)
99 #define L__BIT_ARR_DEFN(NAME) unsigned long NAME[L__BIT_ARR_LONGS];
101 #define L__BIT_ARR_VAL(BIT_ARR, BIT_NUM) \
102 ((BIT_ARR)[(BIT_NUM) / L__LONG_BIT] & (1L << ((BIT_NUM) % L__LONG_BIT)))
104 #define L__BIT_ARR_0(BIT_ARR, BIT_NUM) \
105 (BIT_ARR)[(BIT_NUM) / L__LONG_BIT] &= ~(1L << ((BIT_NUM) % L__LONG_BIT));
107 #define L__BIT_ARR_1(BIT_ARR, BIT_NUM) \
108 (BIT_ARR)[(BIT_NUM) / L__LONG_BIT] |= 1L << ((BIT_NUM) % L__LONG_BIT);
110 #define L__BIT_ARR_ALL(BIT_ARR, BIT_VAL) \
111 { int i = L__BIT_ARR_LONGS; do (BIT_ARR)[--i] = 0L - (BIT_VAL); while(i); }
113 #else /* The bit array can definitely fit in one long */
115 #define L__BIT_ARR_DEFN(NAME) unsigned long NAME;
117 #define L__BIT_ARR_VAL(BIT_ARR, BIT_NUM) ((BIT_ARR) & (1L << (BIT_NUM)))
119 #define L__BIT_ARR_0(BIT_ARR, BIT_NUM) (BIT_ARR) &= ~(1L << (BIT_NUM));
121 #define L__BIT_ARR_1(BIT_ARR, BIT_NUM) (BIT_ARR) |= 1L << (BIT_NUM);
123 #define L__BIT_ARR_ALL(BIT_ARR, BIT_VAL) (BIT_ARR) = 0L - (BIT_VAL);
127 #ifdef AVL_READ_ERRORS_HAPPEN
129 #define L__CHECK_READ_ERROR(ERROR_RETURN) \
130 { if (AVL_READ_ERROR) return(ERROR_RETURN); }
134 #define L__CHECK_READ_ERROR(ERROR_RETURN)
138 /* The presumed reason that an instantiation places additional fields
139 ** inside the AVL tree structure is that the SET_ and GET_ macros
140 ** need these fields. The "balance" function does not explicitly use
141 ** any fields in the AVL tree structure, so only pass an AVL tree
142 ** structure pointer to "balance" if it has instantiation-specific
143 ** fields that are (presumably) needed by the SET_/GET_ calls within
146 #ifdef AVL_INSIDE_STRUCT
148 #define L__BALANCE_PARAM_CALL_PREFIX L__tree,
149 #define L__BALANCE_PARAM_DECL_PREFIX L__(avl) *L__tree,
153 #define L__BALANCE_PARAM_CALL_PREFIX
154 #define L__BALANCE_PARAM_DECL_PREFIX
160 #define L__IMPL_MASK (AVL_IMPL_MASK)
164 /* Define all functions. */
165 #define L__IMPL_MASK AVL_IMPL_ALL
169 #if (L__IMPL_MASK & AVL_IMPL_INIT)
171 L__SC
void L__(init
)(L__(avl
) *L__tree
) { AVL_SET_ROOT(L__tree
, AVL_NULL
); }
175 #if (L__IMPL_MASK & AVL_IMPL_IS_EMPTY)
177 L__SC
int L__(is_empty
)(L__(avl
) *L__tree
)
178 { return(L__tree
->root
== AVL_NULL
); }
182 /* Put the private balance function in the same compilation module as
183 ** the insert function. */
184 #if (L__IMPL_MASK & AVL_IMPL_INSERT)
186 /* Balances subtree, returns handle of root node of subtree after balancing.
188 L__SC AVL_HANDLE
L__(balance
)(L__BALANCE_PARAM_DECL_PREFIX AVL_HANDLE bal_h
)
192 /* Either the "greater than" or the "less than" subtree of
193 ** this node has to be 2 levels deeper (or else it wouldn't
196 if (AVL_GET_BALANCE_FACTOR(bal_h
) > 0)
198 /* "Greater than" subtree is deeper. */
200 deep_h
= AVL_GET_GREATER(bal_h
, 1);
202 L__CHECK_READ_ERROR(AVL_NULL
)
204 if (AVL_GET_BALANCE_FACTOR(deep_h
) < 0)
208 AVL_HANDLE old_h
= bal_h
;
209 bal_h
= AVL_GET_LESS(deep_h
, 1);
210 L__CHECK_READ_ERROR(AVL_NULL
)
211 AVL_SET_GREATER(old_h
, AVL_GET_LESS(bal_h
, 1))
212 AVL_SET_LESS(deep_h
, AVL_GET_GREATER(bal_h
, 1))
213 AVL_SET_LESS(bal_h
, old_h
)
214 AVL_SET_GREATER(bal_h
, deep_h
)
216 bf
= AVL_GET_BALANCE_FACTOR(bal_h
);
221 AVL_SET_BALANCE_FACTOR(old_h
, -1)
222 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
226 AVL_SET_BALANCE_FACTOR(deep_h
, 1)
227 AVL_SET_BALANCE_FACTOR(old_h
, 0)
229 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
233 AVL_SET_BALANCE_FACTOR(old_h
, 0)
234 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
239 AVL_SET_GREATER(bal_h
, AVL_GET_LESS(deep_h
, 0))
240 AVL_SET_LESS(deep_h
, bal_h
)
241 if (AVL_GET_BALANCE_FACTOR(deep_h
) == 0)
243 AVL_SET_BALANCE_FACTOR(deep_h
, -1)
244 AVL_SET_BALANCE_FACTOR(bal_h
, 1)
248 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
249 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
256 /* "Less than" subtree is deeper. */
258 deep_h
= AVL_GET_LESS(bal_h
, 1);
259 L__CHECK_READ_ERROR(AVL_NULL
)
261 if (AVL_GET_BALANCE_FACTOR(deep_h
) > 0)
264 AVL_HANDLE old_h
= bal_h
;
265 bal_h
= AVL_GET_GREATER(deep_h
, 1);
266 L__CHECK_READ_ERROR(AVL_NULL
)
267 AVL_SET_LESS(old_h
, AVL_GET_GREATER(bal_h
, 0))
268 AVL_SET_GREATER(deep_h
, AVL_GET_LESS(bal_h
, 0))
269 AVL_SET_GREATER(bal_h
, old_h
)
270 AVL_SET_LESS(bal_h
, deep_h
)
272 bf
= AVL_GET_BALANCE_FACTOR(bal_h
);
277 AVL_SET_BALANCE_FACTOR(old_h
, 1)
278 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
282 AVL_SET_BALANCE_FACTOR(deep_h
, -1)
283 AVL_SET_BALANCE_FACTOR(old_h
, 0)
285 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
289 AVL_SET_BALANCE_FACTOR(old_h
, 0)
290 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
295 AVL_SET_LESS(bal_h
, AVL_GET_GREATER(deep_h
, 0))
296 AVL_SET_GREATER(deep_h
, bal_h
)
297 if (AVL_GET_BALANCE_FACTOR(deep_h
) == 0)
299 AVL_SET_BALANCE_FACTOR(deep_h
, 1)
300 AVL_SET_BALANCE_FACTOR(bal_h
, -1)
304 AVL_SET_BALANCE_FACTOR(deep_h
, 0)
305 AVL_SET_BALANCE_FACTOR(bal_h
, 0)
314 L__SC AVL_HANDLE
L__(insert
)(L__(avl
) *L__tree
, AVL_HANDLE h
)
316 AVL_SET_LESS(h
, AVL_NULL
)
317 AVL_SET_GREATER(h
, AVL_NULL
)
318 AVL_SET_BALANCE_FACTOR(h
, 0)
320 if (L__tree
->root
== AVL_NULL
) {
321 AVL_SET_ROOT(L__tree
, h
);
324 /* Last unbalanced node encountered in search for insertion point. */
325 AVL_HANDLE unbal
= AVL_NULL
;
326 /* Parent of last unbalanced node. */
327 AVL_HANDLE parent_unbal
= AVL_NULL
;
328 /* Balance factor of last unbalanced node. */
331 /* Zero-based depth in tree. */
332 unsigned depth
= 0, unbal_depth
= 0;
334 /* Records a path into the tree. If bit n is true, indicates
335 ** take greater branch from the nth node in the path, otherwise
336 ** take the less branch. bit 0 gives branch from root, and
338 L__BIT_ARR_DEFN(branch
)
340 AVL_HANDLE hh
= L__tree
->root
;
341 AVL_HANDLE parent
= AVL_NULL
;
346 if (AVL_GET_BALANCE_FACTOR(hh
) != 0)
349 parent_unbal
= parent
;
352 cmp
= AVL_COMPARE_NODE_NODE(h
, hh
);
359 hh
= AVL_GET_GREATER(hh
, 1);
360 L__BIT_ARR_1(branch
, depth
)
364 hh
= AVL_GET_LESS(hh
, 1);
365 L__BIT_ARR_0(branch
, depth
)
367 L__CHECK_READ_ERROR(AVL_NULL
)
370 while (hh
!= AVL_NULL
);
372 /* Add node to insert as leaf of tree. */
374 AVL_SET_LESS(parent
, h
)
376 AVL_SET_GREATER(parent
, h
)
380 if (unbal
== AVL_NULL
)
384 cmp
= L__BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
386 unbal_bf
= AVL_GET_BALANCE_FACTOR(unbal
);
391 hh
= cmp
< 0 ? AVL_GET_LESS(unbal
, 1) : AVL_GET_GREATER(unbal
, 1);
392 L__CHECK_READ_ERROR(AVL_NULL
)
393 if ((unbal_bf
!= -2) && (unbal_bf
!= 2))
395 /* No rebalancing of tree is necessary. */
396 AVL_SET_BALANCE_FACTOR(unbal
, unbal_bf
)
404 cmp
= L__BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
408 AVL_SET_BALANCE_FACTOR(hh
, -1)
409 hh
= AVL_GET_LESS(hh
, 1);
413 AVL_SET_BALANCE_FACTOR(hh
, 1)
414 hh
= AVL_GET_GREATER(hh
, 1);
416 L__CHECK_READ_ERROR(AVL_NULL
)
419 if (unbal
!= AVL_NULL
)
421 unbal
= L__(balance
)(L__BALANCE_PARAM_CALL_PREFIX unbal
);
422 L__CHECK_READ_ERROR(AVL_NULL
)
423 if (parent_unbal
== AVL_NULL
)
425 AVL_SET_ROOT(L__tree
, unbal
);
429 depth
= unbal_depth
- 1;
430 cmp
= L__BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
432 AVL_SET_LESS(parent_unbal
, unbal
)
434 AVL_SET_GREATER(parent_unbal
, unbal
)
445 #if (L__IMPL_MASK & AVL_IMPL_SEARCH)
447 L__SC AVL_HANDLE
L__(search
)(L__(avl
) *L__tree
, AVL_KEY k
, avl_search_type st
)
450 AVL_HANDLE match_h
= AVL_NULL
;
451 AVL_HANDLE h
= L__tree
->root
;
455 else if (st
& AVL_GREATER
)
460 while (h
!= AVL_NULL
)
462 cmp
= AVL_COMPARE_KEY_NODE(k
, h
);
472 else if (target_cmp
!= 0)
473 if (!((cmp
^ target_cmp
) & L__MASK_HIGH_BIT
))
474 /* cmp and target_cmp are both positive or both negative. */
476 h
= cmp
< 0 ? AVL_GET_LESS(h
, 1) : AVL_GET_GREATER(h
, 1);
477 L__CHECK_READ_ERROR(AVL_NULL
)
485 #if (L__IMPL_MASK & AVL_IMPL_SEARCH_LEAST)
487 L__SC AVL_HANDLE
L__(search_least
)(L__(avl
) *L__tree
)
489 AVL_HANDLE h
= L__tree
->root
;
490 AVL_HANDLE parent
= AVL_NULL
;
492 while (h
!= AVL_NULL
)
495 h
= AVL_GET_LESS(h
, 1);
496 L__CHECK_READ_ERROR(AVL_NULL
)
504 #if (L__IMPL_MASK & AVL_IMPL_SEARCH_GREATEST)
506 L__SC AVL_HANDLE
L__(search_greatest
)(L__(avl
) *L__tree
)
508 AVL_HANDLE h
= L__tree
->root
;
509 AVL_HANDLE parent
= AVL_NULL
;
511 while (h
!= AVL_NULL
)
514 h
= AVL_GET_GREATER(h
, 1);
515 L__CHECK_READ_ERROR(AVL_NULL
)
523 #if (L__IMPL_MASK & AVL_IMPL_REMOVE)
525 /* Prototype of balance function (called by remove) in case not in
526 ** same compilation unit.
528 L__SC AVL_HANDLE
L__(balance
)(L__BALANCE_PARAM_DECL_PREFIX AVL_HANDLE bal_h
);
530 L__SC AVL_HANDLE
L__(remove
)(L__(avl
) *L__tree
, AVL_KEY k
)
532 /* Zero-based depth in tree. */
533 unsigned depth
= 0, rm_depth
;
535 /* Records a path into the tree. If bit n is true, indicates
536 ** take greater branch from the nth node in the path, otherwise
537 ** take the less branch. bit 0 gives branch from root, and
539 L__BIT_ARR_DEFN(branch
)
541 AVL_HANDLE h
= L__tree
->root
;
542 AVL_HANDLE parent
= AVL_NULL
;
545 int cmp
, cmp_shortened_sub_with_path
;
549 AVL_HANDLE parent_rm
;
554 /* No node in tree with given key. */
556 cmp
= AVL_COMPARE_KEY_NODE(k
, h
);
558 /* Found node to remove. */
563 h
= AVL_GET_GREATER(h
, 1);
564 L__BIT_ARR_1(branch
, depth
)
568 h
= AVL_GET_LESS(h
, 1);
569 L__BIT_ARR_0(branch
, depth
)
571 L__CHECK_READ_ERROR(AVL_NULL
)
573 cmp_shortened_sub_with_path
= cmp
;
579 /* If the node to remove is not a leaf node, we need to get a
580 ** leaf node, or a node with a single leaf as its child, to put
581 ** in the place of the node to remove. We will get the greatest
582 ** node in the less subtree (of the node to remove), or the least
583 ** node in the greater subtree. We take the leaf node from the
584 ** deeper subtree, if there is one. */
586 if (AVL_GET_BALANCE_FACTOR(h
) < 0)
588 child
= AVL_GET_LESS(h
, 1);
589 L__BIT_ARR_0(branch
, depth
)
594 child
= AVL_GET_GREATER(h
, 1);
595 L__BIT_ARR_1(branch
, depth
)
598 L__CHECK_READ_ERROR(AVL_NULL
)
601 if (child
!= AVL_NULL
)
610 child
= AVL_GET_LESS(h
, 1);
611 L__BIT_ARR_0(branch
, depth
)
615 child
= AVL_GET_GREATER(h
, 1);
616 L__BIT_ARR_1(branch
, depth
)
618 L__CHECK_READ_ERROR(AVL_NULL
)
621 while (child
!= AVL_NULL
);
624 /* Only went through do loop once. Deleted node will be replaced
625 ** in the tree structure by one of its immediate children. */
626 cmp_shortened_sub_with_path
= -cmp
;
628 cmp_shortened_sub_with_path
= cmp
;
630 /* Get the handle of the opposite child, which may not be null. */
631 child
= cmp
> 0 ? AVL_GET_LESS(h
, 0) : AVL_GET_GREATER(h
, 0);
634 if (parent
== AVL_NULL
) {
635 /* There were only 1 or 2 nodes in this tree. */
636 AVL_SET_ROOT(L__tree
, child
);
638 else if (cmp_shortened_sub_with_path
< 0)
639 AVL_SET_LESS(parent
, child
)
641 AVL_SET_GREATER(parent
, child
)
643 /* "path" is the parent of the subtree being eliminated or reduced
644 ** from a depth of 2 to 1. If "path" is the node to be removed, we
645 ** set path to the node we're about to poke into the position of the
646 ** node to be removed. */
647 path
= parent
== rm
? h
: parent
;
651 /* Poke in the replacement for the node to be removed. */
652 AVL_SET_LESS(h
, AVL_GET_LESS(rm
, 0))
653 AVL_SET_GREATER(h
, AVL_GET_GREATER(rm
, 0))
654 AVL_SET_BALANCE_FACTOR(h
, AVL_GET_BALANCE_FACTOR(rm
))
655 if (parent_rm
== AVL_NULL
) {
656 AVL_SET_ROOT(L__tree
, h
);
660 depth
= rm_depth
- 1;
661 if (L__BIT_ARR_VAL(branch
, depth
))
662 AVL_SET_GREATER(parent_rm
, h
)
664 AVL_SET_LESS(parent_rm
, h
)
668 if (path
!= AVL_NULL
)
670 /* Create a temporary linked list from the parent of the path node
671 ** to the root node. */
677 if (L__BIT_ARR_VAL(branch
, depth
))
679 child
= AVL_GET_GREATER(h
, 1);
680 AVL_SET_GREATER(h
, parent
)
684 child
= AVL_GET_LESS(h
, 1);
685 AVL_SET_LESS(h
, parent
)
687 L__CHECK_READ_ERROR(AVL_NULL
)
693 /* Climb from the path node to the root node using the linked
694 ** list, restoring the tree structure and rebalancing as necessary.
697 cmp
= cmp_shortened_sub_with_path
;
702 bf
= AVL_GET_BALANCE_FACTOR(h
);
707 if ((bf
== -2) || (bf
== 2))
709 h
= L__(balance
)(L__BALANCE_PARAM_CALL_PREFIX h
);
710 L__CHECK_READ_ERROR(AVL_NULL
)
711 bf
= AVL_GET_BALANCE_FACTOR(h
);
714 AVL_SET_BALANCE_FACTOR(h
, bf
)
715 reduced_depth
= (bf
== 0);
717 if (parent
== AVL_NULL
)
722 cmp
= L__BIT_ARR_VAL(branch
, depth
) ? 1 : -1;
725 parent
= AVL_GET_LESS(h
, 1);
726 AVL_SET_LESS(h
, child
)
730 parent
= AVL_GET_GREATER(h
, 1);
731 AVL_SET_GREATER(h
, child
)
733 L__CHECK_READ_ERROR(AVL_NULL
)
735 AVL_SET_ROOT(L__tree
, h
);
743 #if (L__IMPL_MASK & AVL_IMPL_SUBST)
745 L__SC AVL_HANDLE
L__(subst
)(L__(avl
) *L__tree
, AVL_HANDLE new_node
)
747 AVL_HANDLE h
= L__tree
->root
;
748 AVL_HANDLE parent
= AVL_NULL
;
751 /* Search for node already in tree with same key. */
755 /* No node in tree with same key as new node. */
757 cmp
= AVL_COMPARE_NODE_NODE(new_node
, h
);
759 /* Found the node to substitute new one for. */
763 h
= cmp
< 0 ? AVL_GET_LESS(h
, 1) : AVL_GET_GREATER(h
, 1);
764 L__CHECK_READ_ERROR(AVL_NULL
)
767 /* Copy tree housekeeping fields from node in tree to new node. */
768 AVL_SET_LESS(new_node
, AVL_GET_LESS(h
, 0))
769 AVL_SET_GREATER(new_node
, AVL_GET_GREATER(h
, 0))
770 AVL_SET_BALANCE_FACTOR(new_node
, AVL_GET_BALANCE_FACTOR(h
))
772 if (parent
== AVL_NULL
)
774 /* New node is also new root. */
775 AVL_SET_ROOT(L__tree
, new_node
);
779 /* Make parent point to new node. */
781 AVL_SET_LESS(parent
, new_node
)
783 AVL_SET_GREATER(parent
, new_node
)
791 #ifdef AVL_BUILD_ITER_TYPE
793 #if (L__IMPL_MASK & AVL_IMPL_BUILD)
795 L__SC
int L__(build
)(
796 L__(avl
) *L__tree
, AVL_BUILD_ITER_TYPE p
, L__SIZE num_nodes
)
798 /* Gives path to subtree being built. If bit n is false, branch
799 ** less from the node at depth n, if true branch greater. */
800 L__BIT_ARR_DEFN(branch
)
802 /* If bit n is true, then for the current subtree at depth n, its
803 ** greater subtree has one more node than its less subtree. */
806 /* Depth of root node of current subtree. */
809 /* Number of nodes in current subtree. */
810 L__SIZE num_sub
= num_nodes
;
812 /* The algorithm relies on a stack of nodes whose less subtree has
813 ** been built, but whose greater subtree has not yet been built.
814 ** The stack is implemented as linked list. The nodes are linked
815 ** together by having the "greater" handle of a node set to the
816 ** next node in the list. "less_parent" is the handle of the first
817 ** node in the list. */
818 AVL_HANDLE less_parent
= AVL_NULL
;
820 /* h is root of current subtree, child is one of its children. */
826 AVL_SET_ROOT(L__tree
, AVL_NULL
);
834 /* Subtract one for root of subtree. */
837 L__BIT_ARR_1(rem
, depth
)
839 L__BIT_ARR_0(rem
, depth
)
840 L__BIT_ARR_0(branch
, depth
)
847 /* Build a subtree with two nodes, slanting to greater.
848 ** I arbitrarily chose to always have the extra node in the
849 ** greater subtree when there is an odd number of nodes to
850 ** split between the two subtrees. */
852 h
= AVL_BUILD_ITER_VAL(p
);
853 L__CHECK_READ_ERROR(0)
854 AVL_BUILD_ITER_INCR(p
)
855 child
= AVL_BUILD_ITER_VAL(p
);
856 L__CHECK_READ_ERROR(0)
857 AVL_BUILD_ITER_INCR(p
)
858 AVL_SET_LESS(child
, AVL_NULL
)
859 AVL_SET_GREATER(child
, AVL_NULL
)
860 AVL_SET_BALANCE_FACTOR(child
, 0)
861 AVL_SET_GREATER(h
, child
)
862 AVL_SET_LESS(h
, AVL_NULL
)
863 AVL_SET_BALANCE_FACTOR(h
, 1)
865 else /* num_sub == 1 */
867 /* Build a subtree with one node. */
869 h
= AVL_BUILD_ITER_VAL(p
);
870 L__CHECK_READ_ERROR(0)
871 AVL_BUILD_ITER_INCR(p
)
872 AVL_SET_LESS(h
, AVL_NULL
)
873 AVL_SET_GREATER(h
, AVL_NULL
)
874 AVL_SET_BALANCE_FACTOR(h
, 0)
880 if (!L__BIT_ARR_VAL(branch
, depth
))
881 /* We've completed a less subtree. */
884 /* We've completed a greater subtree, so attach it to
885 ** its parent (that is less than it). We pop the parent
886 ** off the stack of less parents. */
889 less_parent
= AVL_GET_GREATER(h
, 1);
890 L__CHECK_READ_ERROR(0)
891 AVL_SET_GREATER(h
, child
)
892 /* num_sub = 2 * (num_sub - rem[depth]) + rem[depth] + 1 */
894 num_sub
+= L__BIT_ARR_VAL(rem
, depth
) ? 0 : 1;
895 if (num_sub
& (num_sub
- 1))
896 /* num_sub is not a power of 2. */
897 AVL_SET_BALANCE_FACTOR(h
, 0)
899 /* num_sub is a power of 2. */
900 AVL_SET_BALANCE_FACTOR(h
, 1)
903 if (num_sub
== num_nodes
)
904 /* We've completed the full tree. */
907 /* The subtree we've completed is the less subtree of the
908 ** next node in the sequence. */
911 h
= AVL_BUILD_ITER_VAL(p
);
912 L__CHECK_READ_ERROR(0)
913 AVL_BUILD_ITER_INCR(p
)
914 AVL_SET_LESS(h
, child
)
916 /* Put h into stack of less parents. */
917 AVL_SET_GREATER(h
, less_parent
)
920 /* Proceed to creating greater than subtree of h. */
921 L__BIT_ARR_1(branch
, depth
)
922 num_sub
+= L__BIT_ARR_VAL(rem
, depth
) ? 1 : 0;
925 } /* end for ( ; ; ) */
927 AVL_SET_ROOT(L__tree
, h
);
936 #if (L__IMPL_MASK & AVL_IMPL_INIT_ITER)
938 /* Initialize depth to invalid value, to indicate iterator is
939 ** invalid. (Depth is zero-base.) It's not necessary to initialize
940 ** iterators prior to passing them to the "start" function.
942 L__SC
void L__(init_iter
)(L__(iter
) *iter
) { iter
->depth
= ~0; }
946 #ifdef AVL_READ_ERRORS_HAPPEN
948 #define L__CHECK_READ_ERROR_INV_DEPTH \
949 { if (AVL_READ_ERROR) { iter->depth = ~0; return; } }
953 #define L__CHECK_READ_ERROR_INV_DEPTH
957 #if (L__IMPL_MASK & AVL_IMPL_START_ITER)
959 L__SC
void L__(start_iter
)(
960 L__(avl
) *L__tree
, L__(iter
) *iter
, AVL_KEY k
, avl_search_type st
)
962 AVL_HANDLE h
= L__tree
->root
;
966 /* Save the tree that we're going to iterate through in a
967 ** member variable. */
968 iter
->tree_
= L__tree
;
977 /* Key can be greater than key of starting node. */
979 else if (st
& AVL_GREATER
)
980 /* Key can be less than key of starting node. */
983 /* Key must be same as key of starting node. */
988 cmp
= AVL_COMPARE_KEY_NODE(k
, h
);
993 /* Equal node was sought and found as starting node. */
999 else if (target_cmp
!= 0)
1000 if (!((cmp
^ target_cmp
) & L__MASK_HIGH_BIT
))
1001 /* cmp and target_cmp are both negative or both positive. */
1003 h
= cmp
< 0 ? AVL_GET_LESS(h
, 1) : AVL_GET_GREATER(h
, 1);
1004 L__CHECK_READ_ERROR_INV_DEPTH
1008 L__BIT_ARR_1(iter
->branch
, d
)
1010 L__BIT_ARR_0(iter
->branch
, d
)
1011 iter
->path_h
[d
++] = h
;
1017 #if (L__IMPL_MASK & AVL_IMPL_START_ITER_LEAST)
1019 L__SC
void L__(start_iter_least
)(L__(avl
) *L__tree
, L__(iter
) *iter
)
1021 AVL_HANDLE h
= L__tree
->root
;
1023 iter
->tree_
= L__tree
;
1027 L__BIT_ARR_ALL(iter
->branch
, 0)
1029 while (h
!= AVL_NULL
)
1031 if (iter
->depth
!= ~0)
1032 iter
->path_h
[iter
->depth
] = h
;
1034 h
= AVL_GET_LESS(h
, 1);
1035 L__CHECK_READ_ERROR_INV_DEPTH
1041 #if (L__IMPL_MASK & AVL_IMPL_START_ITER_GREATEST)
1043 L__SC
void L__(start_iter_greatest
)(L__(avl
) *L__tree
, L__(iter
) *iter
)
1045 AVL_HANDLE h
= L__tree
->root
;
1047 iter
->tree_
= L__tree
;
1051 L__BIT_ARR_ALL(iter
->branch
, 1)
1053 while (h
!= AVL_NULL
)
1055 if (iter
->depth
!= ~0)
1056 iter
->path_h
[iter
->depth
] = h
;
1058 h
= AVL_GET_GREATER(h
, 1);
1059 L__CHECK_READ_ERROR_INV_DEPTH
1065 #if (L__IMPL_MASK & AVL_IMPL_GET_ITER)
1067 L__SC AVL_HANDLE
L__(get_iter
)(L__(iter
) *iter
)
1069 if (iter
->depth
== ~0)
1072 return(iter
->depth
== 0 ?
1073 iter
->tree_
->root
: iter
->path_h
[iter
->depth
- 1]);
1078 #if (L__IMPL_MASK & AVL_IMPL_INCR_ITER)
1080 L__SC
void L__(incr_iter
)(L__(iter
) *iter
)
1082 #define L__tree (iter->tree_)
1084 if (iter
->depth
!= ~0)
1087 AVL_GET_GREATER((iter
->depth
== 0 ?
1088 iter
->tree_
->root
: iter
->path_h
[iter
->depth
- 1]), 1);
1089 L__CHECK_READ_ERROR_INV_DEPTH
1094 if (iter
->depth
== 0)
1101 while (L__BIT_ARR_VAL(iter
->branch
, iter
->depth
));
1104 L__BIT_ARR_1(iter
->branch
, iter
->depth
)
1105 iter
->path_h
[iter
->depth
++] = h
;
1108 h
= AVL_GET_LESS(h
, 1);
1109 L__CHECK_READ_ERROR_INV_DEPTH
1112 L__BIT_ARR_0(iter
->branch
, iter
->depth
)
1113 iter
->path_h
[iter
->depth
++] = h
;
1123 #if (L__IMPL_MASK & AVL_IMPL_DECR_ITER)
1125 L__SC
void L__(decr_iter
)(L__(iter
) *iter
)
1127 #define L__tree (iter->tree_)
1129 if (iter
->depth
!= ~0)
1132 AVL_GET_LESS((iter
->depth
== 0 ?
1133 iter
->tree_
->root
: iter
->path_h
[iter
->depth
- 1]), 1);
1134 L__CHECK_READ_ERROR_INV_DEPTH
1139 if (iter
->depth
== 0)
1146 while (!L__BIT_ARR_VAL(iter
->branch
, iter
->depth
));
1149 L__BIT_ARR_0(iter
->branch
, iter
->depth
)
1150 iter
->path_h
[iter
->depth
++] = h
;
1153 h
= AVL_GET_GREATER(h
, 1);
1154 L__CHECK_READ_ERROR_INV_DEPTH
1157 L__BIT_ARR_1(iter
->branch
, iter
->depth
)
1158 iter
->path_h
[iter
->depth
++] = h
;
1168 /* Tidy up the preprocessor symbol name space. */
1170 #undef L__EST_LONG_BIT
1172 #undef L__MASK_HIGH_BIT
1174 #undef L__BIT_ARR_DEFN
1175 #undef L__BIT_ARR_VAL
1178 #undef L__BIT_ARR_ALL
1179 #undef L__CHECK_READ_ERROR
1180 #undef L__CHECK_READ_ERROR_INV_DEPTH
1181 #undef L__BIT_ARR_LONGS
1183 #undef L__CHECK_READ_ERROR
1184 #undef L__CHECK_READ_ERROR_INV_DEPTH
1186 #undef L__BALANCE_PARAM_CALL_PREFIX
1187 #undef L__BALANCE_PARAM_DECL_PREFIX