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


GNU libavl 2.0.1

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

7.4 Insertion

The steps for insertion into a red-black tree are similar to those for insertion into an AVL tree:

  1. Search for the location to insert the new item.

  2. Insert the item.

  3. Rebalance the tree as necessary to satisfy the red-black balance condition.

Red-black node colors don't need to be updated in the way that AVL balance factors do, so there is no separate step for updating colors.

Here's the outline of the function, expressed as code:

 
void **
rb_probe (struct rb_table *tree, void *item)
{ &#60;@xref{\NODE\, , rb_probe.&#62;() local variables,198} &#60;@xref{\NODE\, , Step 1: Search RB tree for insertion point.&#62;,199} &#60;@xref{\NODE\, , Step 2: Insert RB node.&#62;,200} &#60;@xref{\NODE\, , Step 3: Rebalance after RB insertion.&#62;,201} return &n-&#62;rb_data; }
This code is included in @refalso{196

 
struct rb_node *pa[RB_MAX_HEIGHT]; /* Nodes on stack. */
unsigned char da[RB_MAX_HEIGHT];   /* Directions moved from stack nodes. */
int k;                             /* Stack height. */
struct rb_node *p; /* Traverses tree looking for insertion point. */
struct rb_node *n; /* Newly inserted node. */
assert (tree != NULL && item != NULL);
This code is included in @refalso{33

See also: [ Cormen 1990], section 14.3; [ Sedgewick 1998], program 13.6.

7.4.1 Step 1: Search  
7.4.2 Step 2: Insert  
7.4.3 Step 3: Rebalance  
7.4.4 Symmetric Case  
7.4.5 Aside: Initial Black Insertion  


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