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


GNU libavl 2.0.1

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

5.9.3.5 Starting at an Inserted Node

Another operation that can be useful is to insert a new node and construct a traverser to the inserted node in a single operation. The following code does this:

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


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