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


GNU libavl 2.0.1

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

10.3.1 Steps 1 and 2: Search and Insert

As usual, we search the tree from the root and record parents as we go.

 
da[0] = 0;
pa[0] = (struct trb_node *) &tree-&#62;trb_root;
k = 1;
if (tree-&#62;trb_root != NULL) 
{ for (p = tree-&#62;trb_root; ; p = p-&#62;trb_link[dir])
{ int cmp = tree-&#62;trb_compare (item, p-&#62;trb_data, tree-&#62;trb_param); if (cmp == 0) return &p-&#62;trb_data; pa[k] = p; da[k++] = dir = cmp > 0; if (p-&#62;trb_tag[dir] == TRB_THREAD) break; } }
else
{ p = (struct trb_node *) &tree-&#62;trb_root; dir = 0; }
This code is included in @refalso{337

The code for insertion is included within the loop for easy access to the dir variable.

 
&#60;@xref{\NODE\, , Step 2: Insert TBST node; tbst =>.&#62; trb,256}
n-&#62;trb_color = TRB_RED;
This code is included in @refalso{337


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