11.5.4 Step 4: Rebalance       Rebalancing in an RTAVL tree after deletion is not completely symmetric between left-side and right-side rebalancing, but there are pairs of similar subcases on each side. The outlines are similar, too. Either way, rebalancing occurs at node y, and cases are distinguished based on the balance factor of x, the child of y on the side opposite the deletion.

```441. <Step 4: Rebalance after RTAVL deletion in left subtree 441> =

assert (x != NULL);
if (x->rtavl_balance == -1)   {
<Rebalance for - balance factor after left-side RTAVL deletion 443>
} else   {
pa[k - 1]->rtavl_link[da[k - 1]] = x;
if (x->rtavl_balance == 0)       {
<Rebalance for 0 balance factor after left-side RTAVL deletion 445>
break;
}
else /* x->rtavl_balance == +1 */       {
<Rebalance for + balance factor after left-side RTAVL deletion 447>
}
}
```

This code is included in 440.

```442. <Step 4: Rebalance after RTAVL deletion in right subtree 442> =

assert (x != NULL);
if (x->rtavl_balance == +1)   {
<Rebalance for + balance factor after right-side RTAVL deletion 444>
} else   {
pa[k - 1]->rtavl_link[da[k - 1]] = x;
if (x->rtavl_balance == 0)       {
<Rebalance for 0 balance factor after right-side RTAVL deletion 446>
break;
}
else /* x->rtavl_balance == -1 */       {
<Rebalance for - balance factor after right-side RTAVL deletion 448>
}
}
```

This code is included in 440.

##### Case 1: x has taller subtree on same side as deletion

If the taller subtree of x is on the same side as the deletion, then we rotate at x in the opposite direction from the deletion, then at y in the same direction as the deletion. This is the same as case 2 for RTAVL insertion (see rtavlinscase2), which in turn performs the general transformation described for AVL deletion case 1 (see avldelcase1), and we can reuse the code.

```443. <Rebalance for - balance factor after left-side RTAVL deletion 443> =
struct rtavl_node *w;

<Rebalance for - balance factor in RTAVL insertion in right subtree 430>
pa[k - 1]->rtavl_link[da[k - 1]] = w;
```

This code is included in 441.

```444. <Rebalance for + balance factor after right-side RTAVL deletion 444> =
struct rtavl_node *w;

<Rebalance for + balance factor in RTAVL insertion in left subtree 429>
pa[k - 1]->rtavl_link[da[k - 1]] = w;
```

This code is included in 442.

##### Case 2: x's subtrees are equal height

If x's two subtrees are of equal height, then we perform a rotation at y toward the deletion. This rotation cannot be troublesome, for the same reason discussed for rebalancing in TAVL trees (see tavldelcase2). We can even reuse the code:

```445. <Rebalance for 0 balance factor after left-side RTAVL deletion 445> =
<Rebalance for 0 balance factor after TAVL deletion in left subtree; tavl => rtavl 323>
```

This code is included in 441.

```446. <Rebalance for 0 balance factor after right-side RTAVL deletion 446> =
<Rebalance for 0 balance factor after TAVL deletion in right subtree; tavl => rtavl 327>
```

This code is included in 442.

##### Case 3: x has taller subtree on side opposite deletion

When x's taller subtree is on the side opposite the deletion, we rotate at y toward the deletion, same as case 2. If the deletion was on the left side of y, then the general form is the same as for TAVL deletion (see tavldelcase3). The special case for left-side deletion, where x lacks a left child, and the general form of the code, are shown here: ```447. <Rebalance for + balance factor after left-side RTAVL deletion 447> =
y->rtavl_balance = x->rtavl_balance = 0;
```

This code is included in 441.

The special case for right-side deletion, where x lacks a right child, and the general form of the code, are shown here: ```448. <Rebalance for - balance factor after right-side RTAVL deletion 448> =
if (x->rtavl_rtag == RTAVL_CHILD)
else   {
x->rtavl_rtag = RTAVL_CHILD;
}