5.5.2 Where is the lowest leaf?

The lowest leaf of an unbalanced AVL tree has to do with the last insertion operation. For the purpose of our discussion, we’ll use a infix notation. This means that in the notation (TA,n,TB), TA is the left subtree, and TB is the right subtree when n is the root of this particular tree.

When a node n of an AVL tree is not balanced, it means one of its subtree is at least two levels higher than the other one. For the purpose of our discussion, let us assume the left tree is higher. In other words, in a subtree (TL,n,TR), the height of TL is at least two more than the height of TR.

Even though we know that TL is the higher one, we still need to know which subtree of TL is higher. In other words, we need to look at the subtree as ((TLL,nL,TLR),n,TR). Here, we have three subtrees, TLL, TLR and TR. We know that TLL or TLR has a height that is one more than TR for the subtree (TLL,nL,TLR) to be 2 levels higher than TR.

In the case that TLL is no shorter than TLR, we can restructure this subtree as follows: (TLL,nL,(TLR,n,TR)). In other words, nL becomes the new root of this subtree. The restructuring is valid because the in-order traversal of the subtree still yields the same order. This restructuring requires the recomputation of the heights of nodes n and nL.

In the case that TLR is the highest subtree, we need to represent TLR as (TLRL,nLR,TLRR). This means that our original subtree is ((TLL,nL,(TLRL,nLR,TLRR)),n,TR). Given this break down, we can rearrange the tree as ((TLL,nL,TLRL),nLR,(TLRR,n,TR)). Note that the problem is solved because both subtrees of nLR are now moved up, and hence the height of the tree rooted at nLR after the rearrangement is reduced by one. This restructuring requires the recomputation of nodes n, nL, and nLR.

The other two cases are symmetric. If TR is higher than TL, then we first reexpress the tree as (TL,n,(TRL,nR,TRR)). If TRR is higher than TRL, then we restructure the subtree as ((TL,n,TRL),nR,TRR). This requries the recomputation of the heights of n and nR.

If TRL is higher than TRR, then we represent the subtree as (TL,n,((TRLL,nRL,TRLR),nR,TRR)). Then, it is restructured to become ((TL,n,TRLL),nRL,(TRLR,nR,TRR)). This requires the recomputation of the heights of n, nR and nRL.