简体   繁体   English

给出一个n节点二叉搜索树的高度的渐近上限,其中一个节点的平均深度为Θ(lg n)

[英]Give an asymptotic upper bound on the height of an n-node binary search tree in which the average depth of a node is Θ(lg n)

Recently, I'm trying to solve all the exercises in CLRS. 最近,我正在尝试解决CLRS中的所有练习。 but there are some of them i can't figure out. 但是有些我不知道。 Here is one of them, from CLRS exercise 12.4-2: 这是CLRS练习12.4-2中的其中之一:

Describe a binary search tree on n nodes such that the average depth of a node in the tree is Θ(lg n) but the height of the tree is ω(lg n). 在n个节点上描述一个二叉搜索树,以使树中一个节点的平均深度为Θ(lg n),但树的高度为ω(lg n)。 Give an asymptotic upper bound on the height of an n-node binary search tree in which the average depth of a node is Θ(lg n). 给出一个n节点二分搜索树的高度的渐近上限,其中一个节点的平均深度为Θ(lg n)。

Can anyone share some ideas or references to solve this problem? 任何人都可以分享一些想法或参考来解决此问题吗? Thanks. 谢谢。

So let's suppose that we build the tree this way: given n nodes, take f(n) nodes and set them aside. 因此,假设我们以这种方式构建树:给定n个节点,取f(n)个节点并将它们放在一边。 Then build a tree by building a perfect binary tree where the root has a left subtree that's a perfect binary tree of n - f(n) - 1 nodes and a right subtree that's a chain of length f(n). 然后,通过构建一个完美的二叉树来构建一棵树,其中的根具有一个左子树,该子树是n-f(n)-1个节点的完美二叉树,而一个右子树的长度为f(n)链。 We'll pick f(n) later. 稍后我们将选择f(n)。

So what's the average depth in the tree? 那么,树的平均深度是多少? Since we just want an asymptotic bound, let's pick n such that n - f(n) - 1 is one less than a perfect power of two, say, 2^k - 1. In that case, the sum of the heights in this part of the tree is 1*2 + 2*3 + 4*4 + 8*5 + ... + 2^(k-1) * k, which is (IIRC) about k 2^k, which is just about (n - f(n)) log (n - f(n)) by our choice of k. 由于我们只需要一个渐近边界,所以我们选择n使得n-f(n)-1比2的完美幂小2,例如2 ^ k-1。在这种情况下,该高度的和树的一部分是1 * 2 + 2 * 3 + 4 * 4 + 8 * 5 + ... + 2 ^(k-1)* k,大约等于k 2 ^ k的(IIRC) (n-f(n))对数(n-f(n))由我们选择的k In the other part of the tree, the total depth is about f(n)^2. 在树的另一部分,总深度约为f(n)^ 2。 This means that the average path length is about ((n - f(n))log (n - f(n)) + f(n)^2) / n. 这意味着平均路径长度约为((n-f(n))log(n-f(n))+ f(n)^ 2)/ n。 Also, the height of the tree is f(n). 而且,树的高度是f(n)。 So we want to maximize f(n) while keeping the average depth O(log n). 因此,我们要在保持平均深度O(log n)的同时最大化f(n)。

To do this, we need to find f(n) such that 为此,我们需要找到f(n)使得

  1. n - f(n) = Θ(n), or the log term in the numerator disappears and the height isn't logarithmic, n-f(n)=Θ(n),或者分子中的对数项消失并且高度不是对数,
  2. f(n)^2 / n = O(log n), or the second term in the numerator gets too big. f(n)^ 2 / n = O(log n),否则分子中的第二项变得太大。

If you pick f(n) = Θ(sqrt(n log n)), I think that 1 and 2 are satisfied maximally. 如果选择f(n)=Θ(sqrt(n log n)),我认为最大满足1和2。 So I'd wager (though I could be totally wrong about this) that this is as good as you can get. 因此,我敢打赌(尽管我对此可能完全错了),这是您所能得到的。 You get a tree of height Θ(sqrt(n log n)) that has average depth Θ(Log n). 您会得到一棵树的高度Θ(sqrt(n log n)),它的平均深度为Θ(Log n)。

Hope this helps! 希望这可以帮助! If my math is way off, please let me know. 如果我的数学差强人意,请告诉我。 It's late now and I haven't done my usual double-checking. 现在已经晚了,我还没有做惯的检查。 :-) :-)

If you are trying to maximize the height of a tree while minimizing the average depth of all the nodes of the tree, the unambiguous best shape would be an "umbrella" shape, eg a full binary tree with k nodes and height = lg k, where 0 < k < n, along with a single path, or "tail", of nk nodes coming out of one of the leaves of the full part. 如果您尝试在最大化树的高度的同时最小化树的所有节点的平均深度,那么明确的最佳形状将是“伞形”形状,例如具有k个节点且height = lg k的完整二叉树,其中0 <k <n,以及从完整部分的叶子之一出来的nk个节点的单个路径或“尾”。 The height of this tree is roughly lg k + n - k. 这棵树的高度大约为lg k + n-k。

Now let's compute the total depth of all the nodes. 现在,让我们计算所有节点的总深度。 The sum of the depths of the nodes of the full part is sum[ j * 2^j ], where the sum is taken from j=0 to j=lg k. 整个部分的节点深度的总和为sum [j * 2 ^ j],其中总和取自j = 0到j = lg k。 By some algebra, the dominant term of the result is 2k lg k. 通过一些代数,结果的主导项是2k lg k。

Next, the sum of the depths of the tail part is given by sum[i + lg k], where the sum is taken from i=0 to i=nk. 接下来,尾部的深度的总和由sum [i + lg k]给出,其中总和从i = 0到i = nk。 By some algebra, the result is approximately (nk)lg k + (1/2)(nk)^2. 通过一些代数,结果约为(nk)lg k +(1/2)(nk)^ 2。

Hence, summing the two parts above together and dividing by n, the average depth of all the nodes is (1 + k/n) lg k + (nk)^2 / (2n). 因此,将以上两个部分加在一起并除以n,所有节点的平均深度为(1 + k / n)lg k +(nk)^ 2 / /(2n)。 Note that because 0 < k < n, the first term here is O(lg n) no matter what k we choose. 注意,因为0 <k <n,所以无论我们选择什么k,这里的第一项都是O(lg n)。 Hence, we need only make sure the second term is O(lg n). 因此,我们只需要确保第二项是O(lg n)。 To do so, we require that (nk)^2 = O(n lg n), or k = n - O(sqrt(n lg n)). 为此,我们要求(nk)^ 2 = O(n lg n),或k = n-O(sqrt(n lg n))。 With this choice, the height of the tree is 通过这种选择,树的高度为

lg k + n - k = O( sqrt(n lg n) ) lg k + n-k = O(sqrt(n lg n))

this is asymptotically larger than the ordinary O(lg n), and is asymptotically the tallest you can make the tree while keeping the average depth to be O(lg n) 渐近地比普通的O(lg n)大,并且渐近地最高,您可以制作树,同时保持平均深度为O(lg n)

first maximize the height of the tree. 首先最大化树的高度。 (have a tree where each node only has one child node, so you have a long chain going downward). (有一棵树,其中每个节点只有一个子节点,因此您的长链向下延伸)。

Check the average depth. 检查平均深度。 (obviously the average depth will be too high). (显然,平均深度会过高)。

while the average depth is too high, you must decrease the height of the tree by one. 当平均深度过高时,必须将树的高度减小一。 There are many ways to decrease the height of the tree by one. 有许多种方法可以将树的高度减少一。 Choose the way which minimizes the average height. 选择最小化平均高度的方式。 (prove by induction that each time you should select the one that minimizes the average height). (通过归纳证明,每次您都应该选择使平均高度最小的一种)。 Keep going until you fall under the average height requirement. 继续前进,直到跌落到平均身高要求以下。 (eg calculate using induction a formula for the height and the average depth). (例如,使用归纳法计算高度和平均深度的公式)。

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

相关问题 T(n)= 2T(n / 2)+ n lg lg n的渐近上界和下界是什么? - What are the asymptotic upper and lower bounds for T(n) = 2T(n/2) + n lg lg n? 二叉树中节点的高度和深度概念之间的混淆 - Confusion between the notion of height and depth of a node in Binary tree O(n ^ 2)的渐近下界 - Asymptotic lower bound of O(n^2) 渐近符号{O(f(n)}是算法可以拥有的最慢的运行时间吗?) - isn't big Oh asymptotic notation {O(f(n)}the slowest runtime an algorithm can have?(it gives asymptotic upper bound which means slowest runtime) 在树中查找节点N. - Find node N in a tree 为什么每个二叉搜索树的高度不是O(log n) - Why is the height of every binary search tree not O(log n) 返回二叉搜索树平均深度的函数 - Function that return average depth of a binary search tree 用渐近符号 big-Oh, small-Oh Ω, ω, Ɵ 比较 lg(lg* n) 和 2^(lg*n ) - Compare lg(lg* n) and 2^(lg*n ) in terms of asymptotic notation big-Oh, small-Oh Ω, ω, Ɵ 如何在合并排序lg(n)+1中递归树的高度 - How come the height of recursion tree in merge sort lg(n)+1 具有N个节点的二叉树的数量,并具有相同的后序和顺序遍历 - Number of Binary Tree with N Node and has same Postorder & Inorder Traversal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM