CS140 Lecture notes -- Balancing Avl Trees

• Jim Plank
• Directory: ~cs140/www-home/notes/Avl
• Lecture notes: http://www.cs.utk.edu/~cs140/notes/Avl
• Thu Nov 19 10:52:16 EST 1998

Balancing Avl Trees

The book, in section 4.4 goes over AVL trees in detail. I found their notes on the balancing operations just a little confusing -- perhaps I just think differently. Anyway, here are my notes on the same material. This will be a little sketchy because it is intended to complement the book, and not to replace its material.

Suppose you have an AVL tree composed of a root node which we'll label K, and two subtrees hanging off K called T' and Z. T' and Z are both AVL trees. The height of T' is n+1 and the height of Z is n. Thus, the tree rooted at K is a valid AVL tree with height n+2. Note, Z can be empty, in which case n = 0 and T' is a single node.

The basic picture of the tree is below.

Now, suppose that we insert a new node into the tree whose key is less than K. This key will go into the tree T', and we will rename the resulting tree T. The insertion of the node increases the height of T, but it remains a valid AVL tree. The picture of the whole tree is below:

Note that the tree pictured above is no longer an AVL tree because the height of T and Z differ by two. What we're going to do is balance the tree by turning it into a valid AVL tree whose height is still n+2. We do this by applying an operation called a rotation to the tree. There are two kinds of rotations that can be used, depending on how T' was modified.

Now, let's split up T into a root node I and two subtrees X and Y. Note, Keys(X) < Key(I) < Keys(Y) < Key(K) < Keys(Z).

Now, the height of Z is n and the height of I is n+2. This means that either the height of X is n+1 and the height of Y is n, or the height of X is n and the height of Y is n+1. They can't both be n+1 because the original height of T' was n+1. Think about it and convince yourself that this is true.

Ok, now we have to split our analysis into two cases. In the first case, the height of X is n+1 and the height of Y is n:

What we're going to do is construct a valid AVL tree from these nodes/subtrees by applying what's called a ``single rotation about node I.'' This is depicted below.

Note, this is a valid binary search tree, because Keys(X) < Key(I) < Keys(Y) < Key(K) < Keys(Z). Moreover, it is now a valid AVL tree whose height is n+2. Thus, we've inserted a node into an AVL tree with a height of n+2, and we're left with an AVL tree whose height is still n+2.

Things to remember -- if you have nodes A and B, and node A is node B's parent, then after your do a single rotation about node B, node B will be node A's parent. The single rotation is a fundamental tree operation. Memorize it.

Now, let's consider the second case: the height of X is n and the height of Y is n+1:

Let's split Y into a root node J and two subtrees A and B. Note that now Keys(X) < Key(I) < Keys(A) < Key(J) < Keys(B) < Key(K) < Keys(Z). Moreover, the height of A is either n or n-1, and the height of B is either n or n-1, with the stipulation that at least one of their heights must be n.

To turn this into a valid AVL tree, we apply what's called a ``double rotation about node J.'' This is pictured below. A double rotation about node J is the result of two single rotations about node J. The first rotation turns J into I's parent. The second rotation turns J into K's parent.

Now, this is a valid binary search tree, because Keys(X) < Key(I) < Keys(A) < Key(J) < Keys(B) < Key(K) < Keys(Z). Moreover, it is now a valid AVL tree, regardless if whether the height of A and B is n or n-1. The height of the AVL tree is N+2, the same as before the insertion.

Note that n can be zero. Thus, if you have a two node tree, adding a third node can make it unbalanced, but it can then be balanced by one of the two rotations above, remembering that a subtree with height n is simply a NULL pointer (i.e. the empty tree).

Finally, if an insertion makes a tree unbalanced because the height of the right subtree is two more than the height of the left subtree, then you do the same thing as above, only pretend you're doing it in a mirror.

Inserting nodes, summarized

Each node of the AVL tree should have a field for its height.

To insert a node into the AVL tree, you do a normal insertion of a node into a binary search tree. Then you travel the path from the newly inserted node up to the root and update the heights. If, while you do this, you see that the tree rooted at one of these nodes is unbalanced, then you balance it using a single or double rotation as detailed above. In doing so, however, its height will not change, so you will not have to do anything else to the tree.

Again, see the book for more detail and for examples. Make sure you understand those examples.