《算法导论》第三版第13章 红黑树 练习&思考题 个人答案

13.1 红黑树的性质13.1-1解:完全二叉搜索树:黑高为2的红黑树:黑高为3的红黑树:黑高为4的红黑树:13.1-2解:如果标红,不满足性质4,因为35是36的父结点,也是红色;如果标黑,不满足性质5,该条路径黑高超过其他路径。13.1-3解:可以验证所得到的树仍然满足5个条件,所以是红黑树。13.1-4解:(1)如果“吸收”前两个子结点都已经是黑色了,度为2...
摘要由CSDN通过智能技术生成

13.1 红黑树的性质

13.1-1

解:
完全二叉搜索树:
完全二叉搜索树
黑高为2的红黑树:
黑高为2的红黑树
黑高为3的红黑树:
黑高为3的红黑树
黑高为4的红黑树:

黑高为4的红黑树

13.1-2

解:如果标红,不满足性质4,因为35是36的父结点,也是红色;如果标黑,不满足性质5,该条路径黑高超过其他路径。

13.1-3

解:可以验证所得到的树仍然满足5个条件,所以是红黑树。

13.1-4

解:
(1)如果“吸收”前两个子结点都已经是黑色了,度为2;
(2)如果“吸收”前两个子结点一个是红一个是黑,度为3;
(3)如果“吸收”前两个子结点都是红色,度为4。
所得到的树,所有叶结点的深度相同。

13.1-5

证明:在最长的路径中,至少每个其他结点都是黑色的(一红一黑一红一黑)。在最短路径中,最多每个结点都是黑色的。由于两条路径包含相同数量的黑色结点,因此最长路径的长度最多为最短路径长度的两倍。

13.1-6

解:最多一层红一层黑,叶结点红, 2 2 k − 1 2^{2k} - 1 22k1个;最少全黑, 2 k − 1 2^k - 1 2k1个。

13.1-7

解:最大比值是2,每个黑色父结点有两个红色子结点;最小比值是0,全黑。

13.2 旋转

13.2-1

解:

RIGHT-ROTATE(T, x)
y = x.left
x.left = y.right
if y.right != T.nil
    y.right.p = x
y.p = x.p
if x.p == T.nil
    T.root = y
elseif x == x.p.right
    x.p.right = y
else x.p.left = y
y.right = x
x.p = y

13.2-2

证明:每个子结点都可以旋转到它的父结点上,只有根结点没法转。

13.2-3

解:a深度-1,b深度不变,c深度+1。

13.2-4

证明(机翻自参考答案):Since the exercise asks about binary search trees rather than the more specific redblack trees, we assume here that leaves are full-fledged nodes, and we ignore the sentinels.
(由于练习要求二叉搜索树而不是更特殊的红黑树,我们假设叶子是完全成熟的节点而忽略了哨兵。)
Taking the book’s hint, we start by showing that with at most n − 1 n - 1 n1 right rotations, we can convert any binary search tree into one that is just a right-going chain. The idea is simple. Let us define the right spine as the root and all descendants of the root that are reachable by following only right pointers from the root. A binary search tree that is just a right-going chain has all n nodes in the right spine.
(根据本书的提示,我们首先展示最多 n − 1 n-1 n1次右旋,可以将任何二叉搜索树转换为一个单链。这个想法很简单。让我们将右脊定义为根,并且后继只能通过右指针到达。右向单链的二叉搜索树在右脊中具有所有n个结点。)
As long as the tree is not just a right spine, repeatedly find some node y y y on the right spine that has a non-leaf left child x x x and then perform a right rotation on y y y:
(只要树不仅仅是一个右脊,在右脊上重复找到一个节点 y y y,其中有一个非叶左子结点 x x x,然后在 y y y上执行右旋:)
13.2-4
(In the above figure, note that any of α \alpha α, β \beta β, and γ \gamma γ can be an empty subtree.) Observe that this right rotation adds x x x to the right spine, and no other nodes leave the right spine. Thus, this right rotation increases the number of nodes in the right spine by 1 1 1. Any binary search tree starts out with at least one node—the root—in the right spine. Moreover, if there are any nodes not on the right spine, then at least one such node has a parent on the right spine. Thus, at most n − 1 n - 1 n1 right rotations are needed to put all nodes in the right spine, so that the tree consists of a single right-going chain.
((在上图中,请注意 α \alpha α β \beta β γ \gamma γ中的任何一个都可以是一个空子树。)观察到这个右旋将 x x x添加到右脊,而没有其他节点离开右脊。因此,这种右旋使右脊中的节点数量增加 1 1 1。任何二叉搜索树都从右脊以至少一个结点(根结点)开始。此外,如果有任何结点不在右脊上,则至少一个这样的结点在右脊上具有父节点。因此,将所有节点放在右脊柱中需要最多 n − 1 n-1 n1次右旋,以使树由右向单链组成。)
If we knew the sequence of right rotations that transforms an arbitrary binary search tree T T T to a single right-going chain T ′ T' T, then we could perform this sequence in reverse—turning each right rotation into its inverse left rotation—to transform T ′ T' T back into T T T.
(如果我们知道将任意二叉搜索树 T T T转换为单个右向链 T ′ T' T的右旋序列,那么我们可以反向执行此序列,将每个右旋反向旋转到其反向左旋直到将 T ′ T' T转换回 T T T。)
Therefore, here is how we can transform any binary search tree T 1 T_1 T1 into any other binary search tree T 2 T_2 T2. Let T ′ T' T be the unique right-going chain consisting of the nodes of T 1 T_1 T1 (which is the same as the nodes of T 2 T_2 T2). Let r = ⟨ r 1 , r 2 , … , r k ⟩ r = \langle r_1, r_2, \ldots, r_k \rangle r=r1,r2,,r

  • 3
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值