More on the AVL Insertion

Observation

All insertions into a BST add a leaf node to the tree.

Definition

An insertion into an AVL tree is a 0-insertion if the path from the leaf where the insertion was made up to the root of the tree (including the root) passes only through nodes marked with a balance factor of 0.

0-insertion Lemma

Any 0-insertion increases the height of the tree by one.

Proof (By induction on the height of the tree before the insertion.) The base case is a tree with a height of 0. The root of such a tree automatically has a balance factor of 0, and inserting a node into such a tree always increases its height from 0 to 1. For the induction step, assume that the result is true for trees of height n-1. Consider a 0-insertion into a tree of height n. Since the root started out with a balance factor of 0, its left and right subtrees have equal heights before the insertion. Consider the subtree that the insertion gets made into. The insertion into that subtree is a 0-insertion into a tree of height n-1, and by the induction hypothesis the insertion causes that subtree's height to increase by 1. That means that the subtree that the insertion was made into is now higher than the other subtree, and the height of the tree as a whole increases by 1.

Definition of the Ancestor Node

Consider the path from the leaf where the new node gets inserted up to the root of the tree. The first node on that path with an original balance factor of -1 or +1 is the ancestor node.

Non-propagation

If we can convince ourselves that in every case below the height of the subtree rooted at the ancestor node does not change after our adjustments, then it will not be necessary to adjust any balance factors or do any rotations to nodes above the ancestor node.

Observation

We can dispense with two simple cases right away.

Case One There is no ancestor node, either because the tree was empty to start with, or because the insertion is a 0-insertion. In this case the tree is still a valid AVL tree after the insertion (although some balance factors may need adjusting). No rotations are needed. In this case there are no nodes above the ancestor node, so we don't have to worry about propagation upwards.

Case Two The balance factor of the ancestor is -1 and we insert into the right subtree of the ancestor, or the balance factor of the ancestor is +1 and we insert into the left subtree of the ancestor. In this case the insertion we make is a 0-insertion into a subtree of the ancestor that increases the height of that subtree by one and therefore brings the ancestor's subtree back into balance. In this case the balance factor of the ancestor node reverts to 0, and no rotations are needed. Also in this case the height of the subtree rooted at the ancestor node does not change, so no propagation upward takes place.

The Difficult Cases

The only remaining cases are insertion into the left subtree of an ancestor with balance factor of -1 and insertion into the right subtree of an ancestor node with balance factor of +1.

By symmetry, we only need to consider the +1 case. The -1 case is symmetric.

First of all, it is clear that some rotation has to take place, because the height of the right subtree was one greater than the height of the left subtree before insertion. The insertion into the right subtree is a 0-insertion and causes the height of the right subtree to increase by 1. Immediately after insertion the right subtree of the ancestor is now 2 taller than the left subtree. Thus a left rotation about the ancestor is absolutely necessary. The only question is whether or not a preliminary rotation is necessary.

Consider the right subtree of the ancestor. The root of that right subtree is a 0 node, which means that the heights of its two subtrees were equal before insertion. The subtree we insert into will have its height increase by 1. The only question that remains is which of the two subtrees of the 0 node we inserted into, and what we should do in those cases.

Here are pictures to cover the two cases along with height information just after the insertion.

In the right hand case an initial right rotation about the 0 node will convert the tree into a tree that looks like the left picture. A left rotation about the +1 node in the left picture produces this result:

After the rotations the balance factor on the +1 node becomes 0, and the entire subtree has height n+1, which was the same height it had before the changes. Once again, no changes propagate further up into the tree.