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


GNU libavl 2.0.1

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

14.5.4 Starting at an Inserted Node

This function combines the functionality of search and insertion with initialization of a traverser.

 
void *
pbst_t_insert (struct pbst_traverser *trav, struct pbst_table *tree,
void *item)
{ struct pbst_node *p, *q; /* Current node in search and its parent. */ int dir; /* Side of q on which p is located. */ struct pbst_node *n; /* Newly inserted node. */ assert (trav != NULL && tree != NULL && item != NULL); trav-&#62;pbst_table = tree; for (q = NULL, p = tree-&#62;pbst_root; p != NULL; q = p, p = p-&#62;pbst_link[dir])
{ int cmp = tree-&#62;pbst_compare (item, p-&#62;pbst_data, tree-&#62;pbst_param); if (cmp == 0)
{ trav-&#62;pbst_node = p; return p-&#62;pbst_data; } dir = cmp > 0; } trav-&#62;pbst_node = n =
tree-&#62;pbst_alloc-&#62;libavl_malloc (tree-&#62;pbst_alloc, sizeof *p); if (n == NULL)
return NULL; tree-&#62;pbst_count++; n-&#62;pbst_link[0] = n-&#62;pbst_link[1] = NULL; n-&#62;pbst_parent = q; n-&#62;pbst_data = item; if (q != NULL) q-&#62;pbst_link[dir] = n; else
tree-&#62;pbst_root = n; return item; }
This code is included in @refalso{502


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