Starting at the First Node [ToC] [Index]     [Skip Back] [Skip Fwd]     [Prev] [Up] [Next]

To initialize a traverser to start at the least valued node, we simply descend from the root as far down and left as possible, recording the parent pointers on the stack as we go. If the stack overflows, then we balance the tree and start over.

66. <BST traverser least-item initializer 66> =
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; }

This code is included in 64.


*1. Show that bst_t_first() will never make more than one recursive call to itself at a time. [answer]