Original post

Get the Balance Right!

~ Depeche Mode

## How a tree can get out of balance

As we have seen in last week’s article, search performance is best if the tree’s height is small. Unfortunately, without any further measure, our simple binary search tree can quickly get out of shape – or never reach a good shape in the first place.

The picture below shows a balanced tree on the left and an extreme case of an unbalanced tree at the right. In the balanced tree, element #6 can be reached in three steps, whereas in the extremely unbalanced case, it takes six steps to find element #6.

Unfortunately, the extreme case can occur quite easily: Just create the tree from a sorted list.

```
tree.Insert(1)
tree.Insert(2)
tree.Insert(3)
tree.Insert(4)
tree.Insert(5)
tree.Insert(6)
```

According to `Insert`

‘s logic, each new element is added as the right child of the rightmost node, because it is larger than any of the elements that were already inserted.

We need a way to avoid this.

## A Definition Of “Balanced”

For our purposes, a good working definition of “balanced” is:

The heights of the two child subtrees of any node differ by at most one.

(Wikipedia: AVL-Tree)

Why “at most one”? Shouldn’t we demand *zero* difference for perfect balance? Actually, no, as we can see on this very simple two-node tree:

The left subtree is a single node, hence the height is 1, and the right “subtree” is empty, hence the height is zero. There is no way to make both subtrees exactly the same height, except perhaps by adding a third “fake” node that has no other purpose of providing perfect balance. But we would gain nothing from this, so a height difference of 1 is perfectly acceptable.

Note that our definition of *balanced* does not include the *size* of the left and right subtrees of a node. That is, the following tree is completely fine:

The left subtree is considerably larger than the right one; yet for either of the two subtrees, any node can be reached with at most four search steps. And the heights of both subtrees differs only by one.

## How to keep a tree in balance

Now that we know what balance means, we need to take care of always keeping the tree in balance. This task consists of two parts: First, we need to be able to detect when a (sub-)tree goes out of balance. And second, we need a way to rearrange the nodes so that the tree is in balance again.

### Step 1. Detecting an imbalance

Balance is related to subtree heights, so we might think of writing a “height” method that descends a given subtree to calculate its height. But this can be come quite costly in terms of CPU time, as these calculations would need to be done repeatedly as we try to determine the balance of each subtee and each subtree’s subree, and so on.

Instead, we store a “balance factor” in each node. This factor is an integer that tells the height difference between the node’s right and left subtrees, or more formally (this is just maths, no Go code):

```
balance_factor := height(right_subtree) - height(left_subtree)
```

Based on our definition of “balanced”, the balance factor of a balanced tree can be -1, 0, or +1. If the balance factor is outside that range (that is, either smaller than -1 or larger than +1), the tree is out of balance and needs to be rebalanced.

After inserting or deleting a node, the balance factors of all affected nodes and parent nodes must be updated.

*For brevity, this article only handles the Insert case.*

Here is how `Insert`

maintains the balance factors:

- First,
`Insert`

descends recursively down the tree until it finds a node`n`

to append the new value.`n`

is either a leaf (that is, it has no children) or a half-leaf (that is, it has exactly one (direct) child). - If
`n`

is a leaf, adding a new child node increases the height of the subtree`n`

by 1. If the child node is added to the left, the balance of`n`

changes from 0 to -1. If the child is added to the right, the balance changes from 0 to 1. `Insert`

now adds a new child node to node`n`

.- The height increase is passed back to
`n`

‘s parent node. - Depending on whether
`n`

is the left or the right child, the parent node adjusts its balance accordingly.

**If the balance factor of a node changes to +2 or -2, respectively, we have detected an imbalance.** At this point, the tree needs rebalancing.

*Please enable JavaScript to view the animation.*

### Removing the imbalance

Let’s assume a node `n`

that has one left child and no right child. `n`

‘s left child has no children; otherwise, the tree at node `n`

would already be out of balance. (The following considerations also apply to inserting below the *right* child in a mirror-reversed way, so we can focus on the left-child scenario here.)

Now let’s insert a new node below the left child of `n`

.

Two scenarios can happen:

#### 1. The new node was inserted as the *left* child of `n`

‘s left child.

Since `n`

has no right children, its balance factor is now -2. (Remember, the balance is defined as “height of right tree minus height of left tree”.) This is an easy case. All we have to do is to “rotate” the tree:

- Make the left child node the root node.
- Make the former root node the new root node’s right child.

Here is a visualization of these steps (click “Rotate”):

*Please enable JavaScript to view the animation.*

The balance is restored, and the tree’s sort order is still intact.

Easy enough, isn’t it? Well, only until we look into the other scenario…

#### 2. The new node was inserted as the *right* child of `n`

‘s left child.

This looks quite similar to the previous case, so let’s try the same rotation here. Click “Single Rotation” in the diagram below and see what happens:

*Please enable JavaScript to view the animation.*

The tree is again unbalanced; the root node’s balance factor changed from -2 to +2. Obviously, a simple rotation as in case 1 does not work here.

Now try the second button, “Double Rotation”. Here, the unbalanced node’s left subtree is rotated first, and now the situation is similar to case 1. Rotating the tree to the right finally rebalances the tree and retains the sort order.

#### Two more cases and a summary

The two cases above assumed that the unbalanced node’s balance factor is -2. If the balance factor is +2, the same cases apply in an analogous way, except that everything is mirror-reversed.

To summarize, here is a scenario where all of the above is included – double rotation as well as reassigning a child node/tree to a rotated node.

*Please enable JavaScript to view the animation.*

## The Code

Now, after all this theory, let’s see how to add the balancing into the code from the previous article.

First, we set up two helper functions, `min`

and `max`

, that we will need later.