Question

I know the formula of finding minimum number of node in a AVL tree is

S(h) = S(h-1) + S(h-2) + 1

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. But how do you get this number? I mean when you plug in 6 isn't it (6-1) + (6-2) + 1?

Can anyone explain to me how to solve this? My teacher haven't talk about this yet but I really want to figure this out myself in order to be prepared for the test next week.

Était-ce utile?

La solution

In S(h) = S(h-1) + S(h-2) + 1,

S(h) is a recursive function/formula. A recursive function calls itself (in a smaller or simpler way) inside its body.

Note that a recursive function must have some base cases, in this case:

S(1) = 0
S(2) = 1

So let's say h = 6, then S(h = 6) will be (just replacing):

S(6) = S(6-1) + S(6-2) + 1
S(6) = S(5) + S(4) + 1 
S(6) = 2*S(4) + S(3) + 1 + 1
S(6) = 2*(S(3) + S(2) + 1) + S(3) + 2
S(6) = 3*S(3) + 2*S(2) + 4
S(6) = 3*(S(2) + S(1) + 1) + 2*S(2) + 4
S(6) = 5*S(2) + 3*S(1) + 7
S(6) = 5*1 + 3*0 + 7
S(6) = 12

Autres conseils

The minimum number of nodes in an AVL tree for a tree with a height of 6 is not 20, it should be 33.

The following equation should demonstrate the recursive call of the N(h) function.

Since we know that N(0)=1 ,N(1) = 2, N(2) = 4, we can reduce the following equation to these knowns for h = 6.

Because AVL tree is balanced BST, for each height h, we must form a h-1 and h-2 tree before moving forward to construct a height h AVL tree by adding another node. Thus, formula N(h)=1+N(h-1)+N(h-2)

N(3)=1+N(3-1)+N(3-2)=1+N(2)+N(1)=7

N(4)=1+N(4-1)+N(4-2)=1+N(3)+N(2)=12

N(5)=1+N(5-1)+N(5-2)=1+N(4)+N(3)=20

N(6)=1+N(6-1)+N(6-2)=1+N(5)+N(4)=33

I hope this may help you

For the function N(h) = 1 + N(h - 1) + N(h - 2)

MIT Recitation 04 states the base cases for this recursive function are: N(1) = 1; N(2) = 2

therefore

N(3) = 1 + N(2) + N(1) = 1 + 2 + 1 = 4

N(4) = 1 + N(3) + N(2) = 1 + 4 + 2 = 7

N(5) = 1 + N(4) + N(3) = 1 + 7 + 4 = 12

N(6) = 1 + N(5) + N(4) = 1 + 12 + 7 = 20

N(7) = 1 + N(6) + N(5) = 1 + 20 + 12 = 33

N(8) = 1 + N(7) + N(6) = 1 + 33 + 20 = 54

and so on, just keep plugging the numbers in from previous answers...

https://courses.csail.mit.edu/6.006/spring11/rec/rec04.pdf

Just a quick note to the question above, the minimum number of nodes in an AVL tree for a tree with a height of 6 is not 12, it should be 20. The following equation should demonstrate the recursive call of the S(h) function.

Since we know that S(1) = 1, S(2) = 2, & S(3) = 4, we can reduce the following equation to these knowns for h = 6.

S(h) = S(h-1) + S(h-2) + 1
S(6) = S(5) + S(4) + 1                           // recursive S(5) & S(4)
S(6) = (S(4) + S(3) + 1) + (S(3) + S(2) + 1) + 1 // don't forget '+1'
S(6) = [(S(3) + S(2) + 1) + S(3) + 1] + (S(3) + S(2) + 1) + 1

// now sub in the values
S(6) = [(4 + 2 + 1) + 4 + 1] + (4 + 2 + 1) + 1
S(6) = 4 + 2 + 1 + 4 + 1 + 4 + 2 + 1 + 1
S(6) = 20

I hope this helps. Please let me know if I overlooked something!

You're confusing S(h-1) with S(h)-1, the first is the (minimum) size of a tree with height h-1, the second the size of a tree of height h, then subtract one from that.

using the Fibonacci sequence in two ways: the first way is less complex but not as efficient as the second one. In order to understand the second way you need to know some math, which I wont explain here unless you really wish for it or check out wiki for some answers first way:

public int findMinNodes(int h){
   if(h<0)
      return 0;
   int a=1;
   int b=2;
   int c;
   for(int i=1;i<h;i++){
      c=a+b+1;
      a=b;
      b=c;
      }
   return b;
}

second way:

public static int findMinNodes(int h){
       return (int)(Math.round(((Math.sqrt(5)+2)/
            Math.sqrt(5))*Math.pow((1+
            Math.sqrt(5))/2,h)-1));
        }

Note:if you try the second method with really large inputs (say h=6000) your answer will display "infinity" that is due to the Math methods.

Min nodes in avl tree with height h are when it has a balancing factor of either 1 or-1. In that kind of avl tree One sub tree has height h-1 and other sub tree's height is h-2. Therefore we calculate no. Of nodes of tree of height h-1 and h-2 recursively and add 1 to it. 1 is added to count root node of previous tree.

The question is a bit old, but I've just studied the subject and so I can provide a detailed answer.

If you just want the answer : The minimum of nodes in an AVL tree of height h is f(h+2) - 1 where f is the Fibonacci sequence, defined as follow : f(0) = 1, f(1) = 1, f(n+2) = f(n+1) + f(n).

Proof :

We can prove this relationship by proving the two following proposition by recursion :

Let s(n) be the minimum number of nodes in an AVL tree of height n.

We have : s(0) = 1, s(1) = 2, s(n+2) = s(n+1) + s(n) + 1.

Proposition 1 : s(n) = f(0) + f(1) + f(2) + ... + f(n)

This can quite easily be showed by recursion :

First, at rank 0 and 1, we have : s(0) = f(0), s(1) = 2 = f(0) + f(1)

Now let's show that if the propostion is true at rank n and n+1, then it is true at rank n+2 :

s(n+2) = s(n+1) + s(n) + 1 [formula for s(n+2)]
       = (f(0) + f(1) + ... + f(n+1)) + (f(0) + f(1) + ... + f(n)) + 1 [apply Proposition 1 at rank n and n+1]
       = f(0) + ((f(1) + f(0)) + (f(2) + f(1)) + ... + (f(n+1) + f(n))) + 1 [rearrange the sums]
       = f(0) + (f(2) + f(3) + ... + f(n+2)) + 1 [apply f(n+2) = f(n+1) + f(n)]
       = f(0) + f(1) + (f(2) + f(3) + ... + f(n+2)) [f(1) = 1 and rearrange]
       = f(0) + f(1) + ... + f(n+2)

Which by recursion shows that the property is true for any n greater than 0.

Proposition 2 : f(n+2) - 1 = f(0) + f(1) + f(2) + ... + f(n)

This proof is left as an exercice to the reader, but it's exactly the same principle as the previous one.

Now we have, by applying props 1 and 2 successively : s(n) = f(0) + f(1) + ... + f(n) = f(n+2) - 1

There is a general expression for the Fibonacci sequence, so we can apply it for n+2 to very quickly compute the minimum number of nodes in an AVL tree of any height.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top