/* Initializes |trav| for |tree| and selects and returns a pointer to its least-valued item. Returns |NULL| if |tree| contains no nodes. */ void * bst_t_first (struct bst_traverser *trav, struct bst_table *tree) { struct bst_node *x; assert (tree != NULL && trav != NULL); trav->bst_table = tree; trav->bst_height = 0; trav->bst_generation = tree->bst_generation; x = tree->bst_root; if (x != NULL) while (x->bst_link[0] != NULL) { if (trav->bst_height >= BST_MAX_HEIGHT) { bst_balance (tree); return bst_t_first (trav, tree); } trav->bst_stack[trav->bst_height++] = x; x = x->bst_link[0]; } trav->bst_node = x; return x != NULL ? x->bst_data : NULL; }
/* Returns the next data item in inorder within the tree being traversed with |trav|, or if there are no more data items returns |NULL|. */ void * bst_t_next (struct bst_traverser *trav) { struct bst_node *x; assert (trav != NULL); if (trav->bst_generation != trav->bst_table->bst_generation) trav_refresh (trav); x = trav->bst_node; if (x == NULL) { return bst_t_first (trav, trav->bst_table); } else if (x->bst_link[1] != NULL) { if (trav->bst_height >= BST_MAX_HEIGHT) { bst_balance (trav->bst_table); return bst_t_next (trav); } trav->bst_stack[trav->bst_height++] = x; x = x->bst_link[1]; while (x->bst_link[0] != NULL) { if (trav->bst_height >= BST_MAX_HEIGHT) { bst_balance (trav->bst_table); return bst_t_next (trav); } trav->bst_stack[trav->bst_height++] = x; x = x->bst_link[0]; } } else { struct bst_node *y; do { if (trav->bst_height == 0) { trav->bst_node = NULL; return NULL; } y = x; x = trav->bst_stack[--trav->bst_height]; } while (y == x->bst_link[1]); } trav->bst_node = x; return x->bst_data; }
const struct ccl_pair_t *ccl_iterate (struct ccl_t *data) { struct ccl_pair_t *pair; if (data == 0) return 0; if (data->iterating) { pair = bst_t_next (&data->traverser); } else { data->iterating = 1; pair = bst_t_first (&data->traverser, data->table); } return pair; }