简体   繁体   English

AVL树中的最小节点数?

[英]Minimum number of node in AVL tree?

I know the formula of finding minimum number of node in a AVL tree is我知道在 AVL 树中找到最小节点数的公式是

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.但是,我真的不知道如何使用这个函数,假设我们的 AVL 高度为 6。答案告诉我最小值 = 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?我的意思是当你插入 6 时不是 (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.我的老师还没有谈论这个,但我真的很想自己弄清楚,以便为下周的考试做好准备。

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

S(h) is a recursive function/formula . S(h)递归函数/公式 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): 那么假设h = 6 ,则S(h = 6)将(仅替换):

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

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. 对于高度为6的树,AVL树中的最小节点数不是20,它应该是33.下面的等式应该演示N(h)函数的递归调用。

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. 由于我们知道N(0)= 1,N(1)= 2,N(2)= 4,我们可以将以下等式减少到h = 6的这些知识。

formula N(h)=1+N(h-1)+N(h-2) 公式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(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(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(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 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) 函数N(h)= 1 + N(h - 1)+ N(h - 2)

MIT Recitation 04 states the base cases for this recursive function are: N(1) = 1; 麻省理工学院复习04指出这个递归函数的基本情况是:N(1)= 1; N(2) = 2 N(2)= 2

therefore 因此

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

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

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

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

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

N(8) = 1 + N(7) + N(6) = 1 + 33 + 20 = 54 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 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. 只需快速注意上面的问题,AVL树中高度为6的树的最小节点数不是12,它应该是20.下面的等式应该演示S(h)函数的递归调用。

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(1)= 1,S(2)= 2,&S(3)= 4,我们可以将以下等式减少到这些已知的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! 如果我忽略了什么,请告诉我!

您将S(h-1)S(h)-1混淆,第一个是高度为h-1的树的(最小)尺寸,第二个是高度为h的树的大小, 然后从那。

using the Fibonacci sequence in two ways: the first way is less complex but not as efficient as the second one. 使用Fibonacci序列有两种方式:第一种方法不太复杂,但效率不如第二种方式。 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: 为了理解第二种方式你需要知道一些数学,我不会在这里解释,除非你真的希望它或首先检查wiki的一些答案:

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. 注意:如果您尝试使用非常大的输入(例如h = 6000)的第二种方法,您的答案将显示由数学方法引起的“无穷大”。

Min nodes in avl tree with height h are when it has a balancing factor of either 1 or-1. 具有高度h的avl树中的最小节点是其具有1或-1的平衡因子。 In that kind of avl tree One sub tree has height h-1 and other sub tree's height is h-2. 在那种avl树中,一个子树的高度为h-1,其他子树的高度为h-2。 Therefore we calculate no. 因此,我们计算不。 Of nodes of tree of height h-1 and h-2 recursively and add 1 to it. 高度为h-1和h-2的树的节点递归并加1。 1 is added to count root node of previous tree. 添加1以计算前一树的根节点。

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).如果您只想要答案:高度为 h 的 AVL 树中节点的最小值是 f(h+2) - 1 其中 f 是斐波那契数列,定义如下: 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.设 s(n) 是高度为 n 的 AVL 树中的最小节点数。

We have : s(0) = 1, s(1) = 2, s(n+2) = s(n+1) + s(n) + 1.我们有: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)命题 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)首先,在等级 0 和 1,我们有: 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 :现在让我们证明,如果命题在 n 和 n+1 级为真,那么它在 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.其中递归表明该属性对于任何大于 0 的 n 都为真。

Proposition 2 : f(n+2) - 1 = f(0) + f(1) + f(2) + ... + f(n)命题 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现在,通过连续应用道具 1 和 2: 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.斐波那契数列有一个通用表达式,因此我们可以将其应用于 n+2 以非常快速地计算任意高度的 AVL 树中的最小节点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM