www.delorie.com/gnu/docs/avl/libavl_55.html   search  
 
Buy GNU books!


GNU libavl 2.0.1

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.9.3.4 Starting at a Found Node

Sometimes it is convenient to begin a traversal at a particular item in a tree. This function works in the same was as bst_find(), but records parent pointers in the traverser structure as it descends the tree.

 
void *
bst_t_find (struct bst_traverser *trav, struct bst_table *tree, void *item)
{ struct bst_node *p, *q; assert (trav != NULL && tree != NULL && item != NULL); trav-&#62;bst_table = tree; trav-&#62;bst_height = 0; trav-&#62;bst_generation = tree-&#62;bst_generation; for (p = tree-&#62;bst_root; p != NULL; p = q)
{ int cmp = tree-&#62;bst_compare (item, p-&#62;bst_data, tree-&#62;bst_param); if (cmp < 0)
q = p-&#62;bst_link[0]; else if (cmp > 0)
q = p-&#62;bst_link[1]; else /* cmp == 0 */
{ trav-&#62;bst_node = p; return p-&#62;bst_data; } if (trav-&#62;bst_height >= BST_MAX_HEIGHT)
{ bst_balance (trav-&#62;bst_table); return bst_t_find (trav, tree, item); } trav-&#62;bst_stack[trav-&#62;bst_height++] = p; } trav-&#62;bst_height = 0; trav-&#62;bst_node = NULL; return NULL; }
This code is included in @refalso{63


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003