📜  数据结构 |二叉树 |问题 9

📅  最后修改于: 2021-09-08 13:28:17             🧑  作者: Mango

权重平衡树是其中每个节点的二叉树。左子树中的节点数至少是右子树中节点数的一半,最多两倍。在 n 个节点上的这种树的最大可能高度(从根到最远叶子的路径上的节点数)最好用以下哪一项来描述?
一种) \log_2 n
b) \log_{4/3} n
C) \log_3 n
d) \log_{3/2} n
(一) A
(乙)
(C)
(四)答案: (D)
说明:设一棵有 n 个节点的树的最大可能高度由 H(n) 表示。

H(n) 的最大可能值可以使用以下递归近似写出

H(n) = H(2n/3) + 1     

上述递归的解为\log_{3/2} n .我们可以通过绘制递归树来简单地得到它。


4. 考虑使用以下算法在未排序的数组 A[1..n] 中搜索给定数字 x,该数组具有 n 个不同的值:

1)    Choose an i uniformly at random from 1..n; 
2)    If A[i] = x then Stop else Goto 1; 

假设 x 存在于 A 中,算法在终止之前进行的预期比较次数是多少?

一个
b) nl
c) 2n
d) n/2

答案(一)

如果你还记得硬币和骰子的问题,你就可以猜出上面的答案。

下面是答案的证明。

让预期的比较次数为 E。 E 的值是所有可能情况下以下表达式的总和。

number_of_comparisons_for_a_case * probability_for_the_case 

情况1

If A[i] is found in the first attempt 
  number of comparisons = 1
  probability of the case  = 1/n

案例二

If A[i] is found in the second attempt 
  number of comparisons = 2
  probability of the case  = (n-1)/n*1/n

案例3

If A[i] is found in the third attempt 
  number of comparisons = 2
  probability of the case  = (n-1)/n*(n-1)/n*1/n

这样的例子其实无穷无尽。因此,我们对 E 有以下无穷级数。

E  = 1/n + [(n-1)/n]*[1/n]*2 + [(n-1)/n]*[(n-1)/n]*[1/n]*3 + ….  (1)

等式(1)乘以(n-1)/n后,我们得到

E (n-1)/n = [(n-1)/n]*[1/n] + [(n-1)/n]*[(n-1)/n]*[1/n]*2 + 
                                 [(n-1)/n]*[(n-1)/n]*[(n-1)/n]*[1/n]*3 ……….(2)

从(1)中减去(2),我们得到

E/n = 1/n + (n-1)/n*1/n + (n-1)/n*(n-1)/n*1/n + …………

右边的表达式是一个具有无限元素的GP。让我们应用求和公式 (a/(1-r))

E/n = [1/n]/[1-(n-1)/n]  = 1
  E = n

这个问题的测验