AVL Tree Height
Show that the height of AVL Tree is \(O(\log n)\).
While I only show u how to prove the bounds of an AVL Tree, this general technique can be applied to other types of trees.
Steps
Here are the general steps to prove the height of most trees.
- Find the lower bound for number of nodes. Express the minimum number of nodes in terms of the height,
- Find the upper bound for height. Change the subject of the equation found above to express the height in terms of number of nodes
To prove the lower bound of the height, you just have to do the opposite. Find the upper bound for minimum number of nodes for a certain height, to find the lower bound for the height.
Conjuring the recursive formula
The simplest way to get started is by drawing out AVL Tree of height 0, 1, 2, 3, etc (up to as high as you can count, or until u see a pattern). The goal is to minimize the number of nodes per height.
If you stare at the trees hard enough,
you should be able to come up with a recursive formula.
For example, you can see a tree of h = 3 is made up of a tree of h = 2 and a tree of h = 1.
Expressing it more formally, let \(N(h)\) be the minimum number of nodes in an AVL tree of height \(h\).
Derive the explicit formula
It can be seen the formula looks similar to the Fibonacci sequence. The explicit formula for Fibonacci sequence is \(F_{n} \in \Theta(\varphi^{n})\) where \(\varphi\) is the golden ratio. \(N(h)\) is a bit bigger than Fibonacci sequence so we can say \(N(h) \in \Omega(\varphi^{h})\) or \(N(h) \geq c \cdot \varphi^h\).
Finding the upper bound for height
Let \(n\) be the number of nodes in a tree. It is known that \(n \geq N(h)\) (this is trivial right?). Once we substitute everything together, we should have:
Ta-da.
Just for completeness, if you continue manipulating the equation, you'll get something like \(1.44 \log_{2} (n/c) \geq h\) for some constant \(c\).