Wednesday, August 18, 2010

《Introduction to Algorithms》(2nd Edition) 一个错误的题目6.3-3

《算法导论》2e发现了一个错误的问题:6.3-3
Problem: Show that there are at most ceiling(n/2^(h+1)) nodes of height h in any n-element heap.
其中ceiling(x) = min {n >= x and n is an integer}。
考虑n = 4, 容易看到h = 1层node的个数为2,但ceiling(n/(2^(h+1)) = ceiling(4/2^(1+1)) = 1。2 > 1并不是at most 1。
应该是at least:
对于h=0,结论显然成立。
对 于h > 0,该层必然满,节点数=2^(floor(log2(n)) - h) >= 2^(log2(n) - 1 - h) = n / 2^(h+1),考虑到>=左边是个整数,因此对于n / 2^(h+1)取个ceiling仍然也是正确的。

2 comments:

  1. when n=4, there are two nodes have height equal to 0 , one node has height equal to 1, and root with height two. IT IS CORRECT.

    ReplyDelete
  2. 你举的例子里面,h=1的只有一个node。

    ReplyDelete