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

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

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

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

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

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

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

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.

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.