14.5.3 Step 4: Rebalance [ToC] [Index]     [Skip Back] [Skip Fwd]     [Prev] [Up] [Next]

The two cases for PAVL deletion are distinguished based on x's balance factor, as always:

540. <Step 4: Rebalance after PAVL deletion 540> =
struct pavl_node *x = y->pavl_link[1];
if (x->pavl_balance == -1)
  { 
    <Left-side rebalancing case 1 in PAVL deletion 541>
  } else
  {
    <Left-side rebalancing case 2 in PAVL deletion 542>
  }

This code is included in 539.

Case 1: x has - balance factor

The same rebalancing is needed here as for a - balance factor in PAVL insertion, and the same code is used.

541. <Left-side rebalancing case 1 in PAVL deletion 541> =
struct pavl_node *w;

<Rebalance for - balance factor in PAVL insertion in right subtree 533>
q->pavl_link[dir] = w;

This code is included in 540.

Case 2: x has + or 0 balance factor

If x has a + or 0 balance factor, we rotate left at y and update parent pointers as for any left rotation (see PBST Rotations). We also update balance factors. If x started with balance factor 0, then we're done. Otherwise, x becomes the new y for the next loop iteration, and rebalancing continues. See avldel2, for details on this rebalancing case.

542. <Left-side rebalancing case 2 in PAVL deletion 542> =
y->pavl_link[1] = x->pavl_link[0];
x->pavl_link[0] = y;
x->pavl_parent = y->pavl_parent;
y->pavl_parent = x;
if (y->pavl_link[1] != NULL)
  y->pavl_link[1]->pavl_parent = y;
q->pavl_link[dir] = x;
if (x->pavl_balance == 0) 
  { x->pavl_balance = -1; y->pavl_balance = +1; break; }
else
  { x->pavl_balance = y->pavl_balance = 0; y = x; }

This code is included in 540.