An AVL tree is a rooted binary tree such that, for any node, the subtrees of its children have difference of height of at most 1. (An empty tree with no nodes has height 0, a tree with a single node has height 1.)
What is the fewest nodes an AVL tree with height 10 could have?
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Let N ( h ) denote minimum number of nodes in an AVL tree of height h . A node in a rooted tree is at an height h if and only if one of its child is at height h − 1 and the other is at height at most h − 1 . Since the height difference of the child at AVL tree does not exceed 1 , this leaves the following two possibilities :
(1) Both of its children are at height h − 1
(2) One of its child is at height h − 1 and the other is at height h − 2 .
Hence we have the following dynamic programming recursion N ( h ) = 1 + min ( 2 N ( h − 1 ) , N ( h − 1 ) + N ( h − 2 ) ) This can be readily solved for h = 1 0 with the initial condition N ( 0 ) = 0 , N ( 1 ) = 1 .
Problem Loading...
Note Loading...
Set Loading...
Note that any subtree (a node together with all descendants of it) of an AVL tree is also an AVL tree. Consider an AVL tree of height n . By definition of height, one of its subtrees must have height n − 1 . Since it's an AVL tree, the other subtree must have height of at least n − 2 .
Let a n be the minimum number of nodes that an AVL tree of height n can have. Clearly, a 0 = 0 , a 1 = 1 . The above observation shows that a n ≥ 1 + a n − 1 + a n − 2 (1 node for the root, plus a n − 1 for the subtree with height n − 1 , plus a n − 2 for the subtree with height at least n − 2 ).
In fact, equality can be achieved by taking the smallest possibility; that is, to construct the minimum AVL tree of height n , take two AVL trees of height n − 1 and n − 2 , and attach them as subtrees of the root. This works because the two subtrees don't interact other than the condition that their height difference is at most 1, and we have satisfied it by selecting one with height n − 1 and one with height n − 2 .
Thus, a n = 1 + a n − 1 + a n − 2 . Following the recurrence gives a 1 0 = 1 4 3 .
Additionally, consider b n = 1 + a n . We have b 0 = 1 , b 1 = 2 , and b n = b n − 1 + b n − 2 for all n ≥ 2 . This is the recurrence of the Fibonacci numbers, and in fact b n = F n + 2 which can be proven easily by induction. Thus we obtain the general formula a n = F n + 2 − 1 , explaining why a 1 0 = F 1 2 − 1 = 1 4 4 − 1 = 1 4 3 .