Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 969
darkserith Avatar asked Jun 11 '15 00:06

darkserith


People also ask

What is the minimum and maximum number of nodes in an AVL tree of height 5?

So, minimum number of nodes required to construct AVL tree of height-5 = 20.

What is the minimum and maximum number of nodes in an AVL tree of height 6?

Bookmark this question. Show activity on this post. However, I don't really get how to use this function, say if we have a AVL height of 6. The answer tells me that Minimum = 7 + 4 + 1 =12.

How do you find the maximum height of an AVL tree with P nodes?

What is the maximum height of an AVL tree with p nodes? Explanation: Consider height of tree to be 'he', then number of nodes which totals to p can be written in terms of height as N(he)=N(he-1)+1+N(he-2).


2 Answers

It's important to note the following defining characteristics of an AVL Tree.

AVL Tree Property

  • The nodes of an AVL tree abide by the BST property
  • AND The heights of the left and right sub-trees of any node differ by no more than 1.

Theorem: The AVL property is sufficient to maintain a worst case tree height of O(log N).

Note the following diagram. AVL Tree

- T1 is comprised of a T0 + 1 node, for a height of 1.
- T2 is comprised of T1 and a T0 + 1 node, giving a height of 2.
- T3 is comprised of a T2 for the left sub-tree and a T1 for the right sub-tree + 1 node, for a height of 3.
- T4 is comprised of a T3 for the left sub-tree and a T2 for the right sub-tree + 1 node, for a height of 4.

If you take the ceiling of O(log N), where N represents the number of nodes in an AVL tree, you get the height.

Example) T4 contains 12 nodes. [ceiling]O(log 12) = 4.

See the pattern developing here??

**The worst-case height is enter image description here

like image 146
Evan Bechtol Avatar answered Oct 03 '22 04:10

Evan Bechtol


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).1

Well the minimum height2 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:


Formulas:

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 height2.

Given n nodes3:

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.

2Technically, the height of a tree is the longest path length (in edges) between the root and any leaf node. However the OP's textbook uses a common alternate definition of height as the number of levels in a tree. For consistency with the OP and Wikipedia, we use that definition in this post as well.

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

like image 30
River Avatar answered Oct 03 '22 03:10

River