- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在阅读 Mark Allen Weiss 的《数据结构和算法》一书中关于树的一章。这是文本片段。
Let D(n) be the internal path length for some tree T of n nodes. D(1) = 0. An n-node tree consists of an i-node left subtree and an (n - i - 1)-node right subtree, plus a root at depth zero for 0<= i < n. D(i) is the internal path length of the left subtree with respect to its root. In the main tree, all these nodes are one level deeper. The same holds for the right subtree. Thus, we get the recurrence
D(n) = D(i) + D(n - i -1) + n -1
If all subtree sizes are equally likely, which is true for binary search trees (since the subtree size depends only on the relative rank of the first element inserted into the tree), but not binary trees, then the average value of both D(i) and D(n - i -1) is (1/n) sum from j =0 to n-1 of D(j). This yields
D(n) = (2/n)(sum from j = 0 to n-1 of D(j)) + (n-1).
The above recurrence obtains an average values of D(n) = O(nlogn).
以下是我对上述文本片段的问题。
谢谢!
最佳答案
关于你的第一点:
在二叉树中,左子树的大小对应于小于根的元素的个数,右子树的大小对应于大于根的元素的个数。
因此,子树的大小仅取决于插入的第一个元素的相对等级。
关于你的第二点,我没有解决方案,但我会这样开始:
您可以先转换总和:
你知道 sum(j=0 to n, of j ) = n*(n-1)/2
然后 n-1 = 2/n*sum(j=0 to n-1, of 1 ) +2/n*n = 2/n*sum(j=0 to n-1, of 1 j) + 2
由于 D(n) = (2/n)(从 j = 0 到 D(j) 的 n-1 的总和)+ (n-1)
,您得到新公式
D(n) = (2/n)(sum from j = 0 to n-1 of (D(j) + j)) + 2 (1)
现在你可以用 Dn-1 来表达 Dn
你会发现(如果我是对的):
D(n)= (n+1)/n*D(n-1) + 2 (2)
然后尝试将 Dn 表示为 n*Sum(1/k) 等价于 nln(n)...
由上面的公式(2)得到(你可以试试写):
D(n) = n+1 * SUM( 2 /k for k=1 to n) +2 ... which is a O(n ln(n))
如果你对这个证明有更多问题,请告诉我
希望对你有帮助
编辑:关于 (2) 的详细信息
D(n) = (2/n)(sum from j = 0 to n-1 of (D(j) + j)) + 2
=> D(n) = (2/n)(sum from j = 0 to n-2 of (D(j) + j)) + 2 + (2/n)*(D(n-1)+n-1)
=> D(n) = ((n-1)/n)* [ 2/(n-1) *(sum from j = 0 to n-2 of (D(j) + j)) + 2] + 2 + (2/n)*D(n-1)
note: the +2 between the brackets comes from (2/n)*(D(n-1)+n-1) = (2/n)*D(n-1) + 2 *(n-1)/n
between the brackets [] you have D(n-1) then :
=> D(n) = ((n-1)/n)* D(n-1) + 2 + (2/n)*D(n-1)
=> D(n) = (n+1)/n*D(n-1) + 2
关于algorithm - 二叉搜索树内部路径长度分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7227653/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
我在使用 fork 和 pipes 制作一个用于学习目的的简单程序时遇到了问题。我想要一个 child 向 parent 发送一些数据,然后这个( parent )再次将它发送给 child 。 结果
我正在制作一个需要同时做 3 件事的 python 脚本。什么是实现此目的的好方法,就像我听说的关于 GIL 的方法一样,我不再那么倾向于使用线程了。 脚本需要做的两件事将非常活跃,他们将有很多工作要
有没有办法运行sshd以便它可以(至少对于有限数量的登录)成功返回提示(可能是 busybox),即使 fork 不可用(例如,PID 不足)? 在我看来,这应该是可能的,例如,sshd 预 fork
我意识到 Bootstrap 将使用 v4 切换到 rem。但是,我使用的是当前版本 (v3),我想使用 rem。 原因?我希望网站上有可以为最终用户缩放字体大小的按钮。我相信最好的实现方式是使用 r
我试图在这个程序中将信息从子进程传递到父进程。这是到目前为止的代码,仍在清理它: #include #include #include #include main() { char *
我试图理解 fork 在 C 中是如何工作的,但我在某个地方误解了一些东西。 我去年遇到了一位教授给我的测试,但我无法回复它:我们有 3 个任务(进程或线程),伪代码如下: Th1 { display
我在使用 fork() 之类的东西时遇到了一些麻烦。 我正在开发一个 shell,用户可以在其中编写将像在普通普通 shell 中一样执行的命令。 我有一个像这样的主要功能: void Shell::
我有一个 Python 主进程,以及由主进程使用 os.fork() 创建的一组或多个 worker . 我需要将大型且相当复杂的数据结构从工作程序传递回主进程。您会为此推荐哪些现有库? 数据结构是列
我对这个 fork 语句很陌生,我不知道 C 程序上的 fork 方法。你能告诉我这段代码的三个可能的输出是什么吗? #include #include int main(void) {
for(i=0;i #include int main() { for(int i=0;i<2;i++) { if(fork()==0) { printf("Hi %d %d
背景 我正在用 C 语言编写一个共享库,与 LD_PRELOAD 动态链接,这意味着拦截和覆盖预加载它的应用程序的网络调用,例如 socket()、connect()、recv()、send()等 在
我是一名优秀的程序员,十分优秀!