《算法导论》第三版第12章 二叉搜索树 练习&思考题 个人答案

12.1 什么是二叉搜索树

12.1-1

高度为 2:《算法导论》第三版第12章 二叉搜索树 练习&思考题 个人答案_第1张图片
高度为3:
《算法导论》第三版第12章 二叉搜索树 练习&思考题 个人答案_第2张图片
高度为4:
《算法导论》第三版第12章 二叉搜索树 练习&思考题 个人答案_第3张图片
高度为5:
《算法导论》第三版第12章 二叉搜索树 练习&思考题 个人答案_第4张图片
高度为6:
《算法导论》第三版第12章 二叉搜索树 练习&思考题 个人答案_第5张图片

12.1-2

解:最小堆的结点值总不大于孩子结点的值,而二叉搜索树的结点值不小于左子树元素结点的值,不大于右子树元素结点的值;最小堆性质无法在O(n)时间内按序输出一棵有n个结点树的关键字,因为堆(以最小堆为例)无法告知大于当前结点的最小元素是在左子树还是在右子树。
换一个角度考虑,如果真的有这种方法,意味着存在O(n)时间的比较排序算法,而这被证明是不存在的。

12.1-3

解:

INORDER-TREE-WALK(T)
let S be an empty stack
current = T.root
done = 0
while !done
    if current != NIL
        PUSH(S, current)
        current = current.left
    else
        if !S.EMPTY()
            current = POP(S)
            print current
            current = current.right
        else done = 1

12.1-4

解:

PREORDER-TREE-WALK(x)
if x != NIL
    print x.key
    PREORDER-TREE-WALK(x.left)
    PREORDER-TREE-WALK(x.right)
POSTORDER-TREE-WALK(x)
if x != NIL
    POSTORDER-TREE-WALK(x.left)
    POSTORDER-TREE-WALK(x.right)
    print x.key

递归真的好用。

12.1-5

证明:反证法,假设我们可以使用小于 Ω ( n lg ⁡ n ) \Omega(n\lg n) Ω(nlgn)的时间,因为遍历二叉搜索树只需要 Θ ( n ) \Theta(n) Θ(n),我们就可以得到小于 Ω ( n lg ⁡ n ) \Omega(n\lg n) Ω(nlgn)的比较排序算法,这与第8章的证明相矛盾。(也就类似于12.1-3的思路了)

12.2 查询二叉搜索树

12.2-1

解:c(先找到911后找到912不可能)和e(先找到347后找到299不可能)。

12.2-2

解:

TREE-MINIMUM(x)
while x.left != NIL
    return TREE-MINIMUM(x.left)
return x
TREE-MAXIMUM(x)
while x.right != NIL
    return TREE-MAXIMUM(x.right)
return x

12.2-3

解:

TREE-PREDECESSOR(x)
if x.left != NIL
    return TREE-MAXIMUM(x.left)
y = x.p
while y != NIL and x == y.left
    x = y
    y = y.p
return y

12.2-4

思路:例如在查找过程中某一结点x的右孩子的两个孩子结点选择了右边,x的右孩子的左孩子就归到A集合,但该数还是比B集合中的x大。

12.2-5

证明:结点x的后继s一定位于x的右子树,而后继s如果有左孩子,该左孩子肯定还是位于x的右子树,那么该左孩子的值就介于x和s之间,那么s就不是x的后继了,矛盾;对称地可证明前驱没有右孩子。

12.2-6

证明:首先,我们确定 y y y必须是 x x x的祖先。如果 y y y不是 x x x的祖先,那么让 z z z表示 x x x y y y的第一个共同祖先。根据二叉搜索树属性, x < z < y x<z<y x<z<y,因此 y y y不能是 x x x的后继。接下来注意 y . l e f t y.left y.left必须是 x x x的祖先,因为如果不是,那么 y . r i g h t y.right y.right将是 x x x的祖先,暗示 x > y x> y x>y。最后,假设 y y y不是 x x x的最底层祖先,其左孩子也是 x x x的祖先。让 z z z表示这个最底层祖先。那么 z z z必须在 y y y的左子树中,这意味着 z < y z <y z<y,这与 y y y x x x的后继相矛盾。

12.2-7

证明(来自参考答案):
Note that a call to TREE-MINIMUM \text{TREE-MINIMUM} TREE-MINIMUM followed by n − 1 n - 1 n1 calls to TREE-SUCCESSOR \text{TREE-SUCCESSOR} TREE-SUCCESSOR performs exactly the same inorder walk of the tree as does the procedure INORDER-TREE-WALK \text{INORDER-TREE-WALK} INORDER-TREE-WALK. INORDER-TREE-WALK \text{INORDER-TREE-WALK} INORDER-TREE-WALK prints the TREE-MINIMUM \text{TREE-MINIMUM} TREE-MINIMUM first, and by
definition, the TREE-SUCCESSOR \text{TREE-SUCCESSOR} TREE-SUCCESSOR of a node is the next node in the sorted order determined by an inorder tree walk.
This algorithm runs in Θ ( n ) \Theta(n) Θ(n) time because:

It requires O ( n ) O(n) O(n) time to do the n procedure calls.
It traverses each of the n − 1 n - 1 n1 tree edges at most twice, which takes O ( n ) O(n) O(n) time.

To see that each edge is traversed at most twice (once going down the tree and once going up), consider the edge between any node u u u and either of its children, node v v v. By starting at the root, we must traverse ( u , v ) (u, v) (u,v) downward from u u u to v v v, before traversing it upward from v v v to u u u. The only time the tree is traversed downward is in code of TREE-MINIMUM \text{TREE-MINIMUM} TREE-MINIMUM, and the only time the tree is traversed upward is in code of TREE-SUCCESSOR \text{TREE-SUCCESSOR} TREE-SUCCESSOR when we look for the successor of a node that has no right subtree.
Suppose that is u u u's left child.

Before printing u u u, we must print all the nodes in its left subtree, which is rooted at v v v, guaranteeing the downward traversal of edge ( u , v ) (u, v) (u,v).
After all nodes in u u u's left subtree are printed, u u u must be printed next. Procedure TREE-SUCCESSOR \text{TREE-SUCCESSOR} TREE-SUCCESSOR traverses an upward path to u u u from the maximum element (which has no right subtree) in the subtree rooted at . This path clearly includes edge ( u , v ) (u, v) (u,v), and since all nodes in u u u's left subtree are printed, edge ( u , v ) (u, v) (u,v) is never traversed again.

Now suppose that v v v is u u u's right child.

After u u u is printed, TREE-SUCCESSOR ( u ) \text{TREE-SUCCESSOR}(u) TREE-SUCCESSOR(u) is called. To get to the minimum element in u u u's right subtree (whose root is v v v), the edge ( u , v ) (u, v) (u,v) must be traversed downward.
After all values in u u u's right subtree are printed, TREE-SUCCESSOR \text{TREE-SUCCESSOR} TREE-SUCCESSOR is called on the maximum element (again, which has no right subtree) in the subtree rooted at v v v. TREE-SUCCESSOR \text{TREE-SUCCESSOR} TREE-SUCCESSOR traverses a path up the tree to an element after u u u, since u u u was already printed. Edge ( u , v ) (u, v) (u,v) must be traversed upward on this path, and since all nodes in u u u's right subtree have been printed, edge ( u , v ) (u, v) (u,v) is never traversed again.

Hence, no edge is traversed twice in the same direction.
Therefore, this algorithm runs in Θ ( n ) \Theta(n) Θ(n) time.

12.2-8

证明:假设 x x x是起始节点, y y y是结束节点。 x x x y y y之间的距离最多为 2 h 2h 2h,连接 k k k节点的所有边都被访问两次,因此需要 O ( k + h ) O(k + h) O(k+h)时间。

12.2-9

证明:
假设 x x x y y y的左孩子,那么 y . k e y > x . k e y y.key>x.key y.key>x.key,以及 y y y可能有的右子树的关键字都大于 y . k e y y.key y.key,且 x x x是叶结点没有右孩子,所以 y . k e y y.key y.key T T T树中大于 x . k e y x.key x.key的最小关键字;
假设 x x x y y y的右孩子,那么 y . k e y < x . k e y y.key<x.key y.key<x.key,以及 y y y可能有的左子树的关键字都小于 y . k e y y.key y.key,且 x x x是叶结点没有左孩子,所以 y . k e y y.key y.key T T T树中小于 x . k e y x.key x.key的最大关键字。

12.3 插入和删除

12.3-1

解:

RECURSIVE-TREE-INSERT(T, z)
if T.root = NIL
    T.root = z
else INSERT(NIL, T.root, z)
INSERT(p, x, z)
if x == NIL
    z.p = p
    if z.key < p.key
        p.left = z
    else 
        p.right = z
else if z.key < x.key
    INSERT(x, x.left, z)
else 
    INSERT(x, x.right, z)

12.3-2

证明:易证路径是一样的,又因为搜索时检查的节点数也包括搜索到的节点,所以……

12.3-3

解:

TREE-SORT(A)
let T be an empty binary search tree
for i = 1 to A.length
    TREE-INSERT(A[i])
INORDER-TREE-WALK(T.root)

最坏情况: Θ ( n 2 ) \Theta(n^2) Θ(n2) - 当重复的 TREE-INSERT \text {TREE-INSERT} TREE-INSERT操作产生一个线性结点链时。
最好情况: Θ ( n lg ⁡ n ) \Theta(n\lg n) Θ(nlgn) - 当重复的 TREE-INSERT \text {TREE-INSERT} TREE-INSERT操作产生高度为 Θ ( lg ⁡ n ) \Theta(\lg n) Θ(lgn)的二叉树时。

12.3-4

解:不一样

先删除A,再删除B:

  A        C        C
 / \      / \        \
B   D    B   D        D
   /
  C
先删除B,再删除A:
  A        A        D
 / \        \      /
B   D        D    C
   /        /
  C        C

12.3-5

解:

PARENT(z)
if z == NIL
    m = T.root
else 
    m = z.left
while m != x
    p = m
    m = m.right
return p
GET-PARENT(T, x)
if x.right == NIL
    if x == x.succ.left
        x.p = x.succ
    else
        z = x.succ
        x.p = PARENT(z)
else
    y = TREE-MAXIMUM(x.right)
    if x == y.succ.left
        x.p = y.succ
    else
        z = y.succ
        x.p = PARENT(z)

12.3-6

思路:可以在原函数第5行添加一个RANDOM(0,1),返回0选择前驱,返回1选择后继即可。

12.4 随机构建二叉搜索树

12.4-1

证明:
∑ i = 0 n − 1 ( i + 3 3 ) = ∑ i = 0 n − 1 ( i + 3 ) ( i + 2 ) ( i + 1 ) 6 = 1 6 ∑ i = 0 n − 1 i 3 + 6 i 2 + 11 i + 6 = 1 6 ( ( n − 1 ) 2 n 2 4 + 6 ( n − 1 ) n ( 2 n − 1 ) 6 + 11 n ( n − 1 ) 2 + 6 n ) = n ( n + 1 ) ( n + 2 ) ( n + 3 ) 24 = ( n + 3 4 ) . \begin{aligned} \sum_{i = 0}^{n - 1} \binom{i + 3}{3} & = \sum_{i = 0}^{n - 1} \frac{(i + 3)(i + 2)(i + 1)}{6} \\ & = \frac{1}{6} \sum_{i = 0}^{n - 1} i^3 + 6i^2 + 11i + 6 \\ & = \frac{1}{6} (\frac{(n - 1)^2 n^2}{4} + \frac{6(n - 1)n(2n - 1)}{6} + \frac{11n(n - 1)}{2} + 6n) \\ & = \frac{n(n + 1)(n + 2)(n + 3)}{24} \\ & = \binom{n + 3}{4}. \end{aligned} i=0n1(3i+3)=i=0n16(i+3)(i+2)(i+1)=61i=0n1i3+6i2+11i+6=61(4(n1)2n2+66(n1)n(2n1)+211n(n1)+6n)=24n(n+1)(n+2)(n+3)=(4n+3).

12.4-2

证明(来自参考答案):
We will answer the second part first. We shall show that if the average depth of a p node is Θ ( lg ⁡ n ) \Theta(\lg n) Θ(lgn), then the height of the tree is O ( n lg ⁡ n ) O(n\lg n) O(nlgn). Then we will answer the first part by exhibiting that this bound is tight: there is a binary search tree with p average node depth Θ ( lg ⁡ n ) \Theta(\lg n) Θ(lgn) and height Θ ( n lg ⁡ n ) = ω ( lg ⁡ n ) \Theta(\sqrt{n\lg n}) = \omega(\lg n) Θ(nlgn )=ω(lgn).
Lemma
If the average depth of p a node in an n n n-node binary search tree is Θ ( lg ⁡ n ) \Theta(\lg n) Θ(lgn), then the height of the tree is O ( n lg ⁡ n ) O(\sqrt{n\lg n}) O(nlgn ).
Proof
Suppose that an n n n-node binary search tree has average depth Θ ( lg ⁡ n ) \Theta(\lg n) Θ(lgn) and height h h h. Then there exists a path from the root to a node at depth h h h, and the depths of the nodes on this path are 0 , 1 , … , h 0, 1, \ldots, h 0,1,,h. Let P P P be the set of nodes on this path and Q Q Q be all other nodes. Then the average depth of a node is
1 n ( ∑ x ∈ P depth( x ) + ∑ y ∈ Q depth( y ) ) ≥ 1 n ∑ x ∈ P depth( x ) = 1 n ∑ d = 0 h d = 1 n ⋅ Θ ( h 2 ) . \begin{aligned} \frac{1}{n} \Big(\sum_{x \in P} \text{depth($x$)} + \sum_{y \in Q} \text{depth($y$)}\Big) & \ge \frac{1}{n} \sum_{x \in P} \text{depth($x$)} \\ & = \frac{1}{n} \sum_{d = 0}^h d \\ & = \frac{1}{n} \cdot \Theta(h^2). \end{aligned} n1(xPdepth(x)+yQdepth(y))n1xPdepth(x)=n1d=0hd=n1Θ(h2).
For the purpose of contradiction, suppose that h h h is not O ( n lg ⁡ n ) O(\sqrt{n\lg n}) O(nlgn ), so that h = ω ( n lg ⁡ n ) h = \omega(\sqrt{n\lg n}) h=ω(nlgn ). Then we have
1 n ⋅ Θ ( h 2 ) = 1 n ⋅ ω ( n lg ⁡ n ) = ω ( lg ⁡ n ) , \begin{aligned} \frac{1}{n} \cdot \Theta(h^2) & = \frac{1}{n} \cdot \omega(n\lg n) \\ & = \omega(\lg n), \end{aligned} n1Θ(h2)=n1ω(nlgn)=ω(lgn),
which contradicts the assumption that the average depth is Θ ( lg ⁡ n ) \Theta(\lg n) Θ(lgn). Thus, the height is O ( n lg ⁡ n ) O(\sqrt{n\lg n}) O(nlgn ).
Here is an example of an n n n-node binary search tree with average node depth Θ ( lg ⁡ n ) \Theta(\lg n) Θ(lgn) but height ω ( lg ⁡ n ) \omega(\lg n) ω(lgn):
《算法导论》第三版第12章 二叉搜索树 练习&思考题 个人答案_第6张图片
In this tree, n − n lg ⁡ n n - \sqrt{n\lg n} nnlgn nodes are a complete binary tree, and the other n lg ⁡ n \sqrt{n\lg n} nlgn nodes protrude from below as a single chain. This tree has height
Θ ( lg ⁡ ( n − n lg ⁡ n ) ) + n lg ⁡ n = Θ ( n lg ⁡ n ) = ω ( lg ⁡ n ) . \begin{aligned} \Theta(\lg(n - \sqrt{n\lg n})) + \sqrt{n\lg n} & = \Theta(\sqrt{n\lg n}) \\ & = \omega(\lg n). \end{aligned} Θ(lg(nnlgn ))+nlgn =Θ(nlgn )=ω(lgn).
To compute an upper bound on the average depth of a node, we use O ( lg ⁡ n ) O(\lg n) O(lgn) as an upper bound on the depth p of each of the n − n lg ⁡ n n - \sqrt{n\lg n} nnlgn nodes in the complete binary tree part and O ( lg ⁡ n + n lg ⁡ n ) O(\lg n + \sqrt{n\lg n}) O(lgn+nlgn ) as an upper bound on the depth of each of the n lg ⁡ n \sqrt{n\lg n} nlgn nodes in the protruding chain. Thus, the average depth of a node is bounded from above by
1 n ⋅ O ( n lg ⁡ n ( lg ⁡ n + n lg ⁡ n ) + ( n − n lg ⁡ n ) lg ⁡ n ) = 1 n ⋅ O ( n lg ⁡ n ) = O ( lg ⁡ n ) . \begin{aligned} \frac{1}{n} \cdot O(\sqrt{n\lg n}(\lg n + \sqrt{n\lg n}) + (n - \sqrt{n\lg n})\lg n) & = \frac{1}{n} \cdot O(n\lg n) \\ & = O(\lg n). \end{aligned} n1O(nlgn (lgn+nlgn )+(nnlgn )lgn)=n1O(nlgn)=O(lgn).
To bound the average depth of a node from below, observe that the bottommost level of the complete binary tree part has Θ ( n − n − lg ⁡ n ) \Theta(n - \sqrt{n - \lg n}) Θ(nnlgn ) nodes, and each of these nodes has depth Θ ( lg ⁡ n ) \Theta(\lg n) Θ(lgn). Thus, the average node depth is at least
1 n ⋅ Θ ( ( n − n − lg ⁡ n ) lg ⁡ n ) = 1 n ⋅ Ω ( n lg ⁡ n ) = Ω ( lg ⁡ n ) . \begin{aligned} \frac{1}{n} \cdot \Theta((n - \sqrt{n - \lg n})\lg n) & = \frac{1}{n} \cdot \Omega(n\lg n) \\ & = \Omega(\lg n). \end{aligned} n1Θ((nnlgn )lgn)=n1Ω(nlgn)=Ω(lgn).
Because the average node depth is both O ( lg ⁡ n ) O(\lg n) O(lgn) and Ω ( lg ⁡ n ) \Omega(\lg n) Ω(lgn), it is Θ ( lg ⁡ n ) \Theta(\lg n) Θ(lgn).

12.4-3

证明:当 n = 3 n = 3 n=3时,共有5种二叉搜索树,但排列有6种。

12.4-4

略。

12.4-5

证明(来自参考答案):
Let A ( n ) A(n) A(n) denote the probability that when quicksorting a list of length n n n, some pivot is selected to not be in the middle n 1 − k / 2 n^{1 - k / 2} n1k/2 of the numberes. This doesn’t happen with probability 1 n k / 2 \frac{1}{n^{k / 2}} nk/21. Then, we have that the two subproblems are of size n 1 , n 2 n_1, n_2 n1,n2 with n 1 + n 2 = n − 1 n_1 + n_2 = n - 1 n1+n2=n1, then
A ( n ) ≤ 1 n k / 2 + T ( n 1 ) + T ( n 2 ) . A(n) \le \frac{1}{n^{k / 2}} + T(n_1)+T(n_2). A(n)nk/21+T(n1)+T(n2).
Since we bounded the depth by O ( 1 / lg ⁡ n ) O(1 / \lg n) O(1/lgn) let a i , j i {a_{i, j}}_i ai,ji be all the subproblem sizes left at depth j j j,
A ( n ) ≤ 1 n k / 2 ∑ j ∑ i 1 a . A(n) \le \frac{1}{n^{k / 2}} \sum_j\sum_i \frac{1}{a}. A(n)nk/21jia1.

思考题

12-1(带有相同关键字的二叉搜索树)

a.

解:每次插入都会将元素添加到最右边的叶子的右侧,因为第11行的不等式将始终计算为false。时间复杂度为 ∑ i = 1 n i ∈ Θ ( n 2 ) \sum_{i = 1}^ni \in \Theta(n^2) i=1niΘ(n2)

b.

解:这种策略将导致两个子树中的每个子树的大小最多只有一个。这意味着高度将是 Θ ( lg ⁡ n ) \Theta(\lg n) Θ(lgn)。因此,总运行时间为 ∑ i = 1 n lg ⁡ n ∈ Θ ( n lg ⁡ n ) \sum_{i = 1}^n \lg n \in \Theta(n \lg n) i=1nlgnΘ(nlgn)

c.

解:只需要线性时间,因为树本身的高度为 0 0 0,单个插入列表可以在恒定时间内完成。

d.

解:
最坏情况:每个随机选择都在右侧(或全部在左侧),这将导致与此问题的第一部分相同的行为, Θ ( n 2 ) \Theta(n ^ 2) Θ(n2)
期望运行时间:当随机选择时,树将大致平衡,因此深度大致为 lg ⁡ ( n ) \lg(n) lg(n) Θ ( n lg ⁡ n ) \Theta(n \lg n) Θ(nlgn)

12-2(基数树)

思路:对类似图12-5的树进行前序遍历即可。

12-3(随机构建二叉搜索树中的平均结点深度)

a.

易证。

b.

证明: T L T_L TL T R T_R TR一共有n-1个元素,每个元素的深度相对于T来说都少了1,而T的根结点深度为0。

c.

证明思路:把i看成左子树的元素个数易证。

d.

证明思路:因为 P ( 0 ) = 0 P(0) = 0 P(0)=0,又因为对称性易证。

e.

证明(来自参考答案):
Observe that if, in the recurrence (7.6) \text{(7.6)} (7.6) in part © Problem 7-3, we replace E [ T ( ⋅ ) ] \text E[T(\cdot)] E[T()] by P ( ⋅ ) P(\cdot) P() and we replace q q q by k k k, we get almost the same recurrence as in part (d) of Problem 12-3. The remaining difference is that in Problem 12-3(d), the summation starts at 1 1 1 rather than 2 2 2. Observe, however, that a binary tree with just one node has a total path length of 0 0 0, so that P ( 1 ) = 0 P(1) = 0 P(1)=0. Thus, we can rewrite the recurrence in Problem 12-3(d) as
P ( n ) = 2 n ∑ k = 2 n − 1 P ( k ) + Θ ( n ) P(n) = \frac{2}{n} \sum_{k = 2}^{n - 1} P(k) + \Theta(n) P(n)=n2k=2n1P(k)+Θ(n)
and use the same technique as was used in Problem 7-3 to solve it.
We start by solving part (d) of Problem 7-3: showing that
∑ k = 2 n − 1 k lg ⁡ k ≤ 1 2 n 2 lg ⁡ n − 1 8 n 2 . \sum_{k = 2}^{n - 1} k\lg k \le \frac{1}{2}n^2\lg n - \frac{1}{8}n^2. k=2n1klgk21n2lgn81n2.
Following the hint in Problem 7-3(d), we split the summation into two parts:
∑ k = 2 n − 1 k lg ⁡ k = ∑ k = 2 ⌈ n / 2 ⌉ − 1 k lg ⁡ k + ∑ k = ⌈ n / 2 ⌉ n − 1 k lg ⁡ k . \sum_{k = 2}^{n - 1} k\lg k = \sum_{k = 2}^{\lceil n / 2\rceil - 1} k\lg k + \sum_{k = \lceil n / 2\rceil}^{n - 1} k\lg k. k=2n1klgk=k=2n/21klgk+k=n/2n1klgk.
The lg ⁡ k \lg k lgk in the first summation on the right is less than lg ⁡ ( n / 2 ) = lg ⁡ n − 1 \lg(n / 2) = \lg n - 1 lg(n/2)=lgn1, and the lg ⁡ k \lg k lgk in the second summation is less than lg ⁡ n \lg n lgn. Thus,
∑ k = 2 n − 1 k lg ⁡ k < ( lg ⁡ n − 1 ) ∑ k = 2 ⌈ n / 2 ⌉ − 1 k + lg ⁡ n ∑ k = ⌈ n / 2 ⌉ n − 1 k = lg ⁡ n ∑ k = 2 n − 1 k − ∑ k = 2 ⌈ n / 2 ⌉ − 1 k ≤ 1 2 n ( n − 1 ) lg ⁡ n − 1 2 ( n 1 − 1 ) n 2 ≤ 1 2 n 2 lg ⁡ n − 1 8 n 2 \begin{aligned} \sum_{k = 2}^{n - 1} k\lg k & < (\lg n - 1) \sum_{k = 2}^{\lceil n / 2\rceil - 1} k + \lg n \sum_{k = \lceil n / 2\rceil}^{n - 1} k \\ & = \lg n \sum_{k = 2}^{n - 1} k - \sum_{k = 2}^{\lceil n / 2 \rceil - 1} k \\ & \le \frac{1}{2} n(n - 1)\lg n - \frac{1}{2}\Big(\frac{n}{1} - 1\Big) \frac{n}{2} \\ & \le \frac{1}{2} n^2\lg n - \frac{1}{8} n^2 \end{aligned} k=2n1klgk<(lgn1)k=2n/21k+lgnk=n/2n1k=lgnk=2n1kk=2n/21k21n(n1)lgn21(1n1)2n21n2lgn81n2
if n ≥ 2 n \ge 2 n2.
Now we show that the recurrence
P ( n ) = 2 n ∑ k = 2 n − 1 P ( k ) + Θ ( n ) P(n) = \frac{2}{n} \sum_{k = 2}^{n - 1} P(k) + \Theta(n) P(n)=n2k=2n1P(k)+Θ(n)
has the solution P ( n ) = O ( n lg ⁡ n ) P(n) = O(n\lg n) P(n)=O(nlgn). We use the substitution method. Assume inductively that P ( n ) ≤ a n lg ⁡ n + b P(n) \le an\lg n + b P(n)anlgn+b for some positive constants a a a and b b b to be determined. We can pick a a a and b b b sufficiently large so that a n lg ⁡ n + b ≥ P ( 1 ) an\lg n + b \ge P(1) anlgn+bP(1). Then, for n > 1 n > 1 n>1, we have by substitution
P ( n ) = 2 n ∑ k = 2 n − 1 P ( k ) + Θ ( n ) ≤ 2 n ∑ k = 2 n − 1 ( a k lg ⁡ k + b ) + Θ ( n ) = 2 a n ∑ k = 2 n − 1 k lg ⁡ k + 2 b n ( n − 2 ) + Θ ( n ) ≤ 2 a n ( 1 2 n 2 lg ⁡ n − 1 8 n 2 ) + 2 b n ( n − 2 ) + Θ ( n ) ≤ a n lg ⁡ n − a 4 n + 2 b + Θ ( n ) = a n lg ⁡ n + b + ( Θ ( n ) + b − a 4 n ) ≤ a n lg ⁡ n + b , \begin{aligned} P(n) & = \frac{2}{n} \sum_{k = 2}^{n - 1} P(k) + \Theta(n) \\ & \le \frac{2}{n} \sum_{k = 2}^{n - 1} (ak\lg k + b) + \Theta(n) \\ & = \frac{2a}{n} \sum_{k = 2}^{n - 1} k\lg k + \frac{2b}{n} (n - 2) + \Theta(n) \\ & \le \frac{2a}{n} \Big(\frac{1}{2} n^2\lg n - \frac{1}{8}n^2\Big) + \frac{2b}{n}(n - 2) + \Theta(n) \\ & \le an\lg n - \frac{a}{4}n + 2b + \Theta(n) \\ & = an\lg n + b + \Big(\Theta(n) + b - \frac{a}{4}n\Big) \\ & \le an\lg n + b, \end{aligned} P(n)=n2k=2n1P(k)+Θ(n)n2k=2n1(aklgk+b)+Θ(n)=n2ak=2n1klgk+n2b(n2)+Θ(n)n2a(21n2lgn81n2)+n2b(n2)+Θ(n)anlgn4an+2b+Θ(n)=anlgn+b+(Θ(n)+b4an)anlgn+b,
since we can choose a a a large enough so that a 4 n \frac{a}{4}n 4an dominates Θ ( n ) + b \Theta(n) + b Θ(n)+b. Thus, P ( n ) = O ( n lg ⁡ n ) P(n) = O(n\lg n) P(n)=O(nlgn).

f.

我们在将元素插入二叉搜索树的子树和在快速排序中对子序列进行排序之间进行类比。注意,一旦元素 x x x被选为子树 T T T的根,所有将在 x x x之后插入 T T T的元素将被比作 x x x。 类似地,观察一旦元素 y y y被选为子元素 S S S中的主元, S S S中的所有其他元素将被拿来与 y y y比较。因此,比较与插入二叉搜索树时所做的比较相同的快速实现只是按照与元素插入树中的顺序相同的顺序来考虑主元。

12-4(不同二叉树的数目)

a.

证明思路:先确定根结点(n种等可能),左右子树的元素数就确定了,再分别对左右子树进行类似分析。

b.

解:
B ( x ) 2 = ( b 0 x 0 + b 1 x 1 + b 2 x 2 + ⋯   ) 2 = b 0 2 x 0 + ( b 0 b 1 + b 1 b 0 ) x 1 + ( b 0 b 2 + b 1 b 1 + b 2 b 0 ) x 2 + ⋯ = ∑ k = 0 0 b k b 0 − k x 0 + ∑ k = 0 1 b k b 1 − k x 1 + ∑ k = 0 2 b k b 2 − k x 2 + ⋯ \begin{aligned} B(x)^2 & = (b_0 x^0 + b_1 x^1 + b_2 x^2 + \cdots)^2 \\ & = b_0^2 x^0 + (b_0 b_1 + b_1 b_0) x^1 + (b_0 b_2 + b_1 b_1 + b_2 b_0) x^2 + \cdots \\ & = \sum_{k = 0}^0 b_k b_{0 - k} x^0 + \sum_{k = 0}^1 b_k b_{1 - k} x^1 + \sum_{k = 0}^2 b_k b_{2 - k} x^2 + \cdots \end{aligned} B(x)2=(b0x0+b1x1+b2x2+)2=b02x0+(b0b1+b1b0)x1+(b0b2+b1b1+b2b0)x2+=k=00bkb0kx0+k=01bkb1kx1+k=02bkb2kx2+
x B ( x ) 2 + 1 = 1 + ∑ k = 0 0 b k b 1 − 1 − k x 1 + ∑ k = 0 2 b k b 2 − 1 − k x 3 + ∑ k = 0 2 b k b 3 − 1 − k x 2 + ⋯ = 1 + b 1 x 1 + b 2 x 2 + b 3 x 3 + ⋯ = b 0 x 0 + b 1 x 1 + b 2 x 2 + b 3 x 3 + ⋯ = ∑ n = 0 ∞ b n x n = B ( x ) . \begin{aligned} xB(x)^2 + 1 & = 1 + \sum_{k = 0}^0 b_k b_{1 - 1 - k} x^1 + \sum_{k = 0}^2 b_k b_{2-1 - k} x^3 + \sum_{k = 0}^2 b_k b_{3-1 - k} x^2 + \cdots \\ & = 1 + b_1 x^1 + b_2 x^2 + b_3 x^3 + \cdots \\ & = b_0 x^0 + b_1 x^1 + b_2 x^2 + b_3 x^3 + \cdots \\ & = \sum_{n = 0}^\infty b_n x^n \\ & = B(x). \end{aligned} xB(x)2+1=1+k=00bkb11kx1+k=02bkb21kx3+k=02bkb31kx2+=1+b1x1+b2x2+b3x3+=b0x0+b1x1+b2x2+b3x3+=n=0bnxn=B(x).
x B ( x ) 2 + 1 = x ⋅ 1 4 x 2 ( 1 + 1 − 4 x − 2 1 − 4 x ) + 1 = 1 4 x ( 2 − 2 1 − 4 x ) − 1 + 1 = 1 2 x ( 1 − 1 − 4 x ) = B ( x ) . \begin{aligned} x B(x)^2 + 1 & = x \cdot \frac{1}{4x^2} (1 + 1 - 4x - 2\sqrt{1 - 4x}) + 1 \\ & = \frac{1}{4x} (2 - 2\sqrt{1 - 4x}) - 1 + 1 \\ & = \frac{1}{2x} (1 - \sqrt{1 - 4x}) \\ & = B(x). \end{aligned} xB(x)2+1=x4x21(1+14x214x )+1=4x1(2214x )1+1=2x1(114x )=B(x).

c.

f ( x ) = 1 − 4 x f(x) = \sqrt{1 - 4x} f(x)=14x ,导数的分子是
2 ⋅ ( 1 ⋅ 2 ) ⋅ ( 3 ⋅ 2 ) ⋅ ( 5 ⋅ 2 ) ⋯ = 2 k ⋅ ∏ i = 0 k − 2 ( 2 k + 1 ) = 2 k ⋅ ( 2 ( k − 1 ) ) ! 2 k − 1 ( k − 1 ) ! = 2 ( 2 ( k − 1 ) ) ! ( k − 1 ) ! . \begin{aligned} 2 \cdot (1 \cdot 2) \cdot (3 \cdot 2) \cdot (5 \cdot 2) \cdots & = 2^k \cdot \prod_{i = 0}^{k - 2} (2k + 1) \\ & = 2^k \cdot \frac{(2(k - 1))!}{2^{k - 1}(k - 1)!} \\ & = \frac{2(2(k - 1))!}{(k - 1)!}. \end{aligned} 2(12)(32)(52)=2ki=0k2(2k+1)=2k2k1(k1)!(2(k1))!=(k1)!2(2(k1))!.
f ( x ) = 1 − 2 x − 2 x 2 − 4 x 3 − 10 x 4 − 28 x 5 − ⋯   . f(x) = 1 - 2x - 2x^2 - 4 x^3 - 10x^4 - 28x^5 - \cdots. f(x)=12x2x24x310x428x5.
系数是 2 ( 2 ( k − 1 ) ) ! k ! ( k − 1 ) ! \frac{2(2(k - 1))!}{k!(k - 1)!} k!(k1)!2(2(k1))!.
B ( x ) = 1 2 x ( 1 − f ( x ) ) = 1 + x + 2 x 2 + 5 x 3 + 14 x 4 + ⋯ = ∑ n = 0 ∞ ( 2 n ) ! ( n + 1 ) ! n ! x = ∑ n = 0 ∞ 1 n + 1 ( 2 n ) ! n ! n ! x = ∑ n = 0 ∞ 1 n + 1 ( 2 n n ) x . \begin{aligned} B(x) & = \frac{1}{2x}(1 - f(x)) \\ & = 1 + x + 2x^2 + 5x^3 + 14x^4 + \cdots \\ & = \sum_{n = 0}^\infty \frac{(2n)!}{(n + 1)!n!} x \\ & = \sum_{n = 0}^\infty \frac{1}{n + 1} \frac{(2n)!}{n!n!} x \\ & = \sum_{n = 0}^\infty \frac{1}{n + 1} \binom{2n}{n} x. \end{aligned} B(x)=2x1(1f(x))=1+x+2x2+5x3+14x4+=n=0(n+1)!n!(2n)!x=n=0n+11n!n!(2n)!x=n=0n+11(n2n)x.
b n = 1 n + 1 ( 2 n n ) . b_n = \frac{1}{n + 1} \binom{2n}{n}. bn=n+11(n2n).

d.

b n = 1 n + 1 ( 2 n ) ! n ! n ! ≈ 1 n + 1 4 π n ( 2 n / e ) 2 n 2 π n ( n / e ) 2 n = 1 n + 1 4 n π n = ( 1 n + ( 1 n + 1 − 1 n ) ) 4 n π n = ( 1 n − 1 n 2 + n ) 4 n π n = 1 n ( 1 − 1 n + 1 ) 4 n π n = 4 n π n 3 2 ( 1 + O ( 1 / n ) ) . \begin{aligned} b_n & = \frac{1}{n + 1} \frac{(2n)!}{n!n!} \\ & \approx \frac{1}{n + 1} \frac{\sqrt{4 \pi n}(2n / e)^{2n}}{2 \pi n (n / e)^{2n}} \\ & = \frac{1}{n + 1} \frac{4^n}{\sqrt{\pi n} } \\ & = (\frac{1}{n} + (\frac{1}{n + 1} - \frac{1}{n})) \frac{4^n}{\sqrt{\pi n}} \\ & = (\frac{1}{n} - \frac{1}{n^2 + n}) \frac{4^n}{\sqrt{\pi n}} \\ & = \frac{1}{n} (1 - \frac{1}{n + 1}) \frac{4^n}{\sqrt{\pi n}} \\ & = \frac{4^n}{\sqrt{\pi}{n^{\frac{3}{2}}}} (1 + O(1 / n)). \end{aligned} bn=n+11n!n!(2n)!n+112πn(n/e)2n4πn (2n/e)2n=n+11πn 4n=(n1+(n+11n1))πn 4n=(n1n2+n1)πn 4n=n1(1n+11)πn 4n=π n234n(1+O(1/n)).

你可能感兴趣的:(算法)