| www.delorie.com/gnu/docs/avl/libavl_174.html | search |
![]() Buy GNU books! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
The basic rebalancing loop is unchanged from <@xref{\NODE\, , \TITLE\}.>{Step 3: Rebalance after RB insertion,201}.
while (k >= 3 && pa[k - 1]->trb_color == TRB_RED) |
The cases for rebalancing are the same as in <@xref{\NODE\, , \TITLE\}.>{Left-side rebalancing after RB insertion,202}, too. We do need to check for threads, instead of null pointers.
struct trb_node *y = pa[k - 2]->trb_link[1];
if (pa[k - 2]->trb_tag[1] == TRB_CHILD && y->trb_color == TRB_RED)
{ |
The rest of this section deals with the individual rebalancing cases, the same as in unthreaded RB insertion (see section 7.4.3 Step 3: Rebalance). Each iteration deals with a node whose color has just been changed to red, which is the newly inserted node n in the first trip through the loop. In the discussion, we'll call this node q.
If node q has an red "uncle", then only recoloring is required. Because no links are changed, no threads need to be updated, and we can reuse the code for RB insertion without change:
<@xref{\NODE\, , Case 1 in left-side RB insertion rebalancing; rb =>.> trb,203}
|
If q is the left child of its parent, we rotate right at q's grandparent, and recolor a few nodes. Here's the transformation:

This transformation can only cause thread problems with subtree c, since the other subtrees stay firmly in place. If c is a thread, then we need to make adjustments after the transformation to account for the difference between threaded and unthreaded rotation, so that the final operation looks like this:

<@xref{\NODE\, , Case 2 in left-side RB insertion rebalancing; rb =>.> trb,204}
if (y->trb_tag[1] == TRB_THREAD) |
The modification to case 3 is the same as the modification to case 2, but it applies to a left rotation instead of a right rotation. The adjusted case looks like this:

<@xref{\NODE\, , Case 3 in left-side RB insertion rebalancing; rb =>.> trb,205}
if (y->trb_tag[0] == TRB_THREAD) |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |