Minimum number of node in AVL tree?

佐手、 提交于 2019-12-10 13:43:49

问题


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.


回答1:


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



回答2:


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.

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




回答3:


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




回答4:


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!




回答5:


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.




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/21347187/minimum-number-of-node-in-avl-tree

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!