-1

Can we find generalized formula to count minimum number of nodes in AVL tree without recursive relation formula as when we have to found number of minimum nodes in AVL tree with height 1000 then it will fail because it takes very long time to solve on paper?

1 Answers1

0

This is a great place to draw some pictures and look for a pattern.

Here's the smallest AVL trees of heights 0 and 1:

*

*
|
*

The smallest AVL tree of height 2 would be made by using trees of heights 0 and 1 (if you used heights 1 and 1, you could improve the number of nodes by using trees of height 0 and 1), and the smallest option is

  *
 / \
*   *
    |
    *

Then, the smallest tree of height 3 would use trees of heights 1 and 2, which would look like this:

   *
 /   \
*     *
|    / \
*   *   *
        |
        *

And, more generally, the smallest tree of height k, where k ≥ 2, is given by taking the two smallest trees of heights k-1 and k-2 and linking them together with a new node at the root.

This gives a recurrence relation:

T(n) = T(n - 1) + T(n - 2) + 1

T(0) = 1

T(1) = 2

Iterating this recurrence gives this pattern:

h = 0   1   2   3   4   5   6   7   8
    1   2   4   7  12  20  33  54  88

There are a couple of things you could do next:

  • Could you write some code to evaluate this recurrence relation on larger and larger values of n? That might give you a function that directly computes the number you need.
  • The recurrence T(n) = T(n-2) + T(n-1) + 1 looks a lot like the Fibonacci recurrence F(n) = F(n-2) + F(n-1). Do you see any relation between the T(n) numbers and the Fibonacci sequence? That might let you directly compute the answer.

Hope this helps!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065