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


GNU libavl 2.0.1

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

6.4.2 Step 2: Insert

Following the search loop, q is the last non-null node examined, so it is the parent of the node to be inserted. The code below creates and initializes a new node as a child of q on side dir, and stores a pointer to it into n. Compare this code for insertion to that within &#60;@xref{\NODE\, , BST item insertion function.&#62;,32}.

 
n = q-&#62;avl_link[dir] = 
tree-&#62;avl_alloc-&#62;libavl_malloc (tree-&#62;avl_alloc, sizeof *n); if (n == NULL) return NULL; tree-&#62;avl_count++; n-&#62;avl_data = item; n-&#62;avl_link[0] = n-&#62;avl_link[1] = NULL; n-&#62;avl_balance = 0; if (y == NULL) return &n-&#62;avl_data;
This code is included in @refalso{146

Exercises:

1. How can y be NULL? Why is this special-cased? [answer]


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