Recall that an AVL tree is a Binary Search Tree that maintains the invariant that the depths of its left and right subtrees differ by at most 1. In this note, we are going to explore how far an AVL tree can be from a complete binary tree. To make this more precise, we are going to answer the following two questions.
For a fixed depth what is the minimum number of nodes an AVL tree of depth must have? (i.e. how small can the tree be?)
For a fixed number of nodes what is maximum possible depth of an AVL tree with nodes?
We will first tackle (1).
ANSWER BELOW: The answer is .
In an AVL tree, each vertex has the property that the depths of its left and right subtrees differ by at most 1. In particular this is true of the root. Let us consider the subtrees of the root. One of them must have depth , since the depth of the whole tree is . The AVL property allows the other tree to have depth or (it can't be since that is the depth of the entire tree). The minimality of the size of the tree implies that in fact the depth of the second subtree must be . Furthermore, Since the entire tree is of minimal size, the subtrees themselves must be of minimal size for their depths, so that their sizes are and respectively. Adding 1 for the root gives the relevant recurrence.
In order to know the size of the minimal AVL tree of depth , we must solve this recurrence.
Look at that again. Does it look familiar? <Drumroll> Enter the Fibonacci numbers, stage left.
The Fibonacci numbers satisfy the recurrence
so our recurrence looks just like them, except that there is an extra 1 on the right hand side.
Now, for , let Then so the sequence really is the Fibonacci numbers!
But wait a minute, I hear you cry. What about the initial conditions? What about the base cases???
OK. Fair point. So lets examine them.
is only defined from 2 on, and and It follows that for all .
where the last equation is Binet's formula, a well-known fact about Fibonacci numbers.
In other words, where . This answers question (1)
On the other hand, the smallest complete binary tree of depth has size . (Why?) This means that an AVL tree of depth could be exponentially smaller than a complete binary tree of the same depth!
Complete trees which would be the ideal data structure for binary search, if we could build them efficiently. Given that AVL trees are so far from complete trees, why are AVL trees a good data structure for binary search?
To answer that, note that the controlling parameter for the time complexity of searching in a search tree is the depth of a tree of fixed size, not the size of a tree of fixed depth. In other words, we need to answer question (2).
Inverting the answer to question (1), we see that the answer to question (2) is . But this is only a constant factor bigger than , which is the depth of a complete binary tree with vertices. Thus searching in an AVL tree only gives up a constant factor time over searching in a complete binary tree, while making the other operations involved in maintaining the tree (insert, delete) significantly less time consuming.