Finding the minimum and maximum height in a AVL tree, given a number of nodes?

darkserith picture darkserith · Jun 11, 2015 · Viewed 45.1k times · Source

Is there a formula to calculate what the maximum and minimum height for an AVL tree, given a certain number of nodes?

For example:
Textbook question:
What is the maximum/minimum height for an AVL tree of 3 nodes, 5 nodes, and 7 nodes?
Textbook answer:
The maximum/minimum height for an AVL tree of 3 nodes is 2/2, for 5 nodes is 3/3, for 7 nodes is 4/3

I don't know if they figured it out by some magic formula, or if they draw out the AVL tree for each of the given heights and determined it that way.

Answer

River picture River · Jun 11, 2015

The solution below is appropriate for working things out by hand and gaining an intuition, please see the exact formulas at the bottom of this answer for larger trees (54+ nodes).

Well the minimum height is easy, just fill each level of the tree with nodes until you run out. That height is the minimum.

To find the maximum, do the same as for the minimum, but then go back one step (remove the last placed node) and see if adding that node to the opposite sub-tree (from where it just was) violates the AVL tree property. If it does, your max height is just your min height. Otherwise this new height (which should be min height+1) is your max height.

If you need an overview of what the properties of an AVL tree are, or just a general explanation of an AVL tree, Wikipedia is a great place to start.

Example:

Let's take the 7 node example case. You fill in all levels and find a completely filled tree of height 3. (1 at level 1, 2 at level 2, 4 at level 3. 1+2+4=7 nodes.) That means 3 is your minimum.

Now find the max. Remove that last node and place it on the left subtree instead of the right. The right subtree still has height 3, but the left subtree now has height 4. However these values differ by less than 2, so it is still an AVL tree. Therefore your max height is 4. (Which is min+1)

All three examples worked out below (note that the numbers correspond to order of placement, NOT value):

Worked out as an example:


Formulas1:

The technique shown above doesn't hold if you have a tree with a very large number nodes. In this case, one can use the following formulas to calculate the exact min/max.

Given n nodes2:

Minimum: ceil(log2(n+1))

Maximum: floor(1.44*log2(n+2)-.328)

If you're curious, the first time max-min>1 is when n=54.

1Thanks to Jamie S for bringing this failure at larger node counts to my attention.

2These formulas are from the Wikipedia AVL page, with constants plugged in. The original source is Sorting and searching by Donald E. Knuth (2nd Edition).