《算法导论》第三版第13章 红黑树 练习思考题 个人答案
13.1 紅黑樹的性質(zhì)
13.1-1
解:
完全二叉搜索樹:
黑高為2的紅黑樹:
黑高為3的紅黑樹:
黑高為4的紅黑樹:
13.1-2
解:如果標(biāo)紅,不滿足性質(zhì)4,因為35是36的父結(jié)點,也是紅色;如果標(biāo)黑,不滿足性質(zhì)5,該條路徑黑高超過其他路徑。
13.1-3
解:可以驗證所得到的樹仍然滿足5個條件,所以是紅黑樹。
13.1-4
解:
(1)如果“吸收”前兩個子結(jié)點都已經(jīng)是黑色了,度為2;
(2)如果“吸收”前兩個子結(jié)點一個是紅一個是黑,度為3;
(3)如果“吸收”前兩個子結(jié)點都是紅色,度為4。
所得到的樹,所有葉結(jié)點的深度相同。
13.1-5
證明:在最長的路徑中,至少每個其他結(jié)點都是黑色的(一紅一黑一紅一黑)。在最短路徑中,最多每個結(jié)點都是黑色的。由于兩條路徑包含相同數(shù)量的黑色結(jié)點,因此最長路徑的長度最多為最短路徑長度的兩倍。
13.1-6
解:最多一層紅一層黑,葉結(jié)點紅,22k?12^{2k} - 122k?1個;最少全黑,2k?12^k - 12k?1個。
13.1-7
解:最大比值是2,每個黑色父結(jié)點有兩個紅色子結(jié)點;最小比值是0,全黑。
13.2 旋轉(zhuǎn)
13.2-1
解:
RIGHT-ROTATE(T, x) y = x.left x.left = y.right if y.right != T.nily.right.p = x y.p = x.p if x.p == T.nilT.root = y elseif x == x.p.rightx.p.right = y else x.p.left = y y.right = x x.p = y13.2-2
證明:每個子結(jié)點都可以旋轉(zhuǎn)到它的父結(jié)點上,只有根結(jié)點沒法轉(zhuǎn)。
13.2-3
解:a深度-1,b深度不變,c深度+1。
13.2-4
證明(機(jī)翻自參考答案):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.
(由于練習(xí)要求二叉搜索樹而不是更特殊的紅黑樹,我們假設(shè)葉子是完全成熟的節(jié)點而忽略了哨兵。)
Taking the book’s hint, we start by showing that with at most n?1n - 1n?1 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.
(根據(jù)本書的提示,我們首先展示最多n?1n-1n?1次右旋,可以將任何二叉搜索樹轉(zhuǎn)換為一個單鏈。這個想法很簡單。讓我們將右脊定義為根,并且后繼只能通過右指針到達(dá)。右向單鏈的二叉搜索樹在右脊中具有所有n個結(jié)點。)
As long as the tree is not just a right spine, repeatedly find some node yyy on the right spine that has a non-leaf left child xxx and then perform a right rotation on yyy:
(只要樹不僅僅是一個右脊,在右脊上重復(fù)找到一個節(jié)點yyy,其中有一個非葉左子結(jié)點xxx,然后在yyy上執(zhí)行右旋:)
(In the above figure, note that any of α\alphaα, β\betaβ, and γ\gammaγ can be an empty subtree.) Observe that this right rotation adds xxx 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 111. 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?1n - 1n?1 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γ中的任何一個都可以是一個空子樹。)觀察到這個右旋將xxx添加到右脊,而沒有其他節(jié)點離開右脊。因此,這種右旋使右脊中的節(jié)點數(shù)量增加111。任何二叉搜索樹都從右脊以至少一個結(jié)點(根結(jié)點)開始。此外,如果有任何結(jié)點不在右脊上,則至少一個這樣的結(jié)點在右脊上具有父節(jié)點。因此,將所有節(jié)點放在右脊柱中需要最多n?1n-1n?1次右旋,以使樹由右向單鏈組成。)
If we knew the sequence of right rotations that transforms an arbitrary binary search tree TTT 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 TTT.
(如果我們知道將任意二叉搜索樹TTT轉(zhuǎn)換為單個右向鏈T′T'T′的右旋序列,那么我們可以反向執(zhí)行此序列,將每個右旋反向旋轉(zhuǎn)到其反向左旋直到將T′T'T′轉(zhuǎn)換回TTT。)
Therefore, here is how we can transform any binary search tree T1T_1T1? into any other binary search tree T2T_2T2?. Let T′T'T′ be the unique right-going chain consisting of the nodes of T1T_1T1? (which is the same as the nodes of T2T_2T2?). Let r=?r1,r2,…,rk?r = \langle r_1, r_2, \ldots, r_k \rangler=?r1?,r2?,…,rk?? be a sequence of right rotations that transforms T1T_1T1? to T′T'T′, and let r′=?r1′,r2′,…,rk′′?r' = \langle r_1', r_2', \ldots, r_{k'}' \rangler′=?r1′?,r2′?,…,rk′′?? be a sequence of right rotations that transforms T2T_2T2? to T′T'T′. We know that there exist sequences rrr and r′r'r′ with kkk, k′≤n?1k' \le n - 1k′≤n?1. For each right rotation ri′r_i'ri′?, let li′l_i'li′? be the corresponding inverse left rotation. Then the sequence ?r1,r2,…,rk,lk′′,lk′?1′,…,l2′,l1′?\langle r_1, r_2, \ldots, r_k, l_{k'}', l_{k' - 1}', \ldots, l_2', l_1' \rangle?r1?,r2?,…,rk?,lk′′?,lk′?1′?,…,l2′?,l1′?? transforms T1T_1T1? to T2T_2T2? in at most 2n?22n - 22n?2 rotations.
(因此,以下是我們?nèi)绾螌⑷魏味嫠阉鳂?span id="ze8trgl8bvbq" class="katex--inline">T1T_1T1?轉(zhuǎn)換為任何其他二叉搜索樹T2T_2T2?。 讓T′T'T′成為由T1T_1T1?結(jié)點組成的唯一右鏈(與T2T_2T2?的結(jié)點相同)。 設(shè)r=?r1,r2,…,rk?r = \langle r_1,r_2,\ldots,r_k \rangler=?r1?,r2?,…,rk??是一系列右旋,將T1T_1T1?轉(zhuǎn)換為T′T'T′,讓r′=?r1′,r2′,…,rk′′?r'= \langle r_1',r_2',\ldots ,r_ {k'}'\rangler′=?r1′?,r2′?,…,rk′′??是一系列右旋,將T2T_2T2?轉(zhuǎn)換為T′T'T′。 我們知道存在序列rrr和r′r'r′和kkk,k′≤n?1k'\le n - 1k′≤n?1。 對于每個右旋ri′r_i'ri′?,讓li′l_i'li′?為相應(yīng)的反向左旋。 然后序列?r1,r2,…,rk,lk′′,lk′?1′,…,l2′,l1′?\langle r_1,r_2,\ldots,r_k,l_ {k'}',l_ {k' - 1}',\ldots,l_2',l_1'\rangle?r1?,r2?,…,rk?,lk′′?,lk′?1′?,…,l2′?,l1′??將T1T_1T1?轉(zhuǎn)換為T2T_2T2? 最多2n?22n - 22n?2次旋轉(zhuǎn)。)
13.2-5
我們可以使用O(n)O(n)O(n)調(diào)用將T2T_2T2?中的根節(jié)點旋轉(zhuǎn)到T1T_1T1?的根,然后分別在兩個子樹中使用相同的操作。共有nnn個結(jié)點,因此上限是O(n2)O(n ^ 2)O(n2)。
13.3 插入
13.3-1
解:性質(zhì)5會被破壞。
13.3-2
解:
13.3-3
解(來自參考答案):
In Figure 13-5, nodes AAA, BBB, and DDD have black-height k+1k + 1k+1 in all cases, because each of their subtrees has black-height kkk and a black root. Node CCC has black-height k+1k + 1k+1 on the left (because its red children have black-height k+1k + 1k+1) and black-height k+2k + 2k+2 on the right (because its black children have black-height k+1k + 1k+1).
In Figure 13-6, nodes AAA, BBB, and CCC have black-height k+1k + 1k+1 in all cases. At left and in the middle, each of AAA's and BBB's subtrees has black-height kkk and a black root, while CCC has one such subtree and a red child with black-height k+1k + 1k+1. At the right, each of AAA's and CCC's subtrees has black-height kkk and a black root, while BBB's red children each have black-height k+1k + 1k+1.
Property 5 is preserved by the transformations. We have shown above that the black-height is well-defined within the subtrees pictured, so property 5 is preserved within those subtrees. Property 5 is preserved for the tree containing the subtrees pictured, because every path through these subtrees to a leaf contributes k+2k + 2k+2 black nodes.
13.3-4
證明:只有在情況1或3會將某結(jié)點設(shè)為RED,在這兩種情況下也只有z.p.p被標(biāo)紅。如果z.p.p是哨兵,z.p就是根結(jié)點。通過循環(huán)不變式的(b)和RB-INSERT-FIXUP的第1行,如果z.p是根,我們已經(jīng)退出循環(huán)。唯一的細(xì)微之處在于情況2,我們在把 z.p.p標(biāo)紅之前設(shè)置z = z.p。因為我們在重新著色之前旋轉(zhuǎn),所以z.p.p的標(biāo)識在情況2之前和之后是相同的,所以沒有問題。
13.3-5
證明:
情況1:z和z.p.p是RED,如果循環(huán)終止,則z不能是root,因此修復(fù)后z是RED。
情況2:z和z.p是RED,旋轉(zhuǎn)后z.p不能是root,因此修復(fù)后z.p是RED。
情況3:z是RED且z不能是root,因此修復(fù)后z是RED。
無論哪種情況都會有至少一個紅結(jié)點。
13.3-6
解(翻譯自參考答案):
使用堆棧記錄插入結(jié)點的路徑,然后parent是堆棧中的頂部元素。
情況1:出棧z.pz.pz.p和z.p.pz.p.pz.p.p。
情況2:出棧z.pz.pz.p和z.p.pz.p.pz.p.p,然后入棧z.p.pz.p.pz.p.p和zzz。
情況3:出棧z.pz.pz.p,z.p.pz.p.pz.p.p和z.p.p.pz.p.p.pz.p.p.p,然后入棧z.pz.pz.p。
13.4 刪除
13.4-1
情況1:轉(zhuǎn)換為情況2,3,4
情況2:如果終止,子樹的根結(jié)點(新的x)被設(shè)置為黑色。
情況3:轉(zhuǎn)換為情況4。
情況4:根結(jié)點被設(shè)為黑色。
13.4-2
假設(shè)在RB-DELETE\text{RB-DELETE}RB-DELETE中xxx和x.px.px.p都是紅色。這只可能在第9行的else情況下發(fā)生。由于是從紅黑樹中刪除,因此在RB-TRANSPLANT\text {RB-TRANSPLANT}RB-TRANSPLANT第14行的調(diào)用中成為xxx兄弟的y.p的另一個孩子必須是黑色的,所以xxx是x.px.px.p唯一的孩子。 RB-DELETE-FIXUP(T,x)\text{RB-DELETE-FIXUP}(T,x)RB-DELETE-FIXUP(T,x)的while循環(huán)條件會立即被違反,所以只需設(shè)置x.color=blackx.color = blackx.color=black,恢復(fù)屬性4。
13.4-3
13.4-4
由于www可能是T.nilT.nilT.nil,因此必須包含RB-DELETE-FIXUP(T,x)\text {RB-DELETE-FIXUP}(T,x)RB-DELETE-FIXUP(T,x)中檢查或修改w的任何行。但是,如第185頁所述,www永遠(yuǎn)不會是T.nilT.nilT.nil,因此不需要包含這些行。
13.4-5
(來自參考答案)
Our count will include the root (if it is black).
Case 1:
For each subtree, it is 222 both before and after.
Case 2:
For α\alphaα and β\betaβ, it is 1+count(c)1 + \text{count}(c)1+count(c) in both cases.
For the rest of the subtrees, it is from 2+count(c)2 + \text{count}(c)2+count(c) to 1+count(c)1 + \text{count}(c)1+count(c).
This decrease in the count for the other subtreese is handled by then having xxx represent an additional black.
Case 3:
For ?\epsilon? and ζ\zetaζ, it is 2+count(c)2+\text{count}(c)2+count(c) both before and after.
For all the other subtrees, it is 1+count(c)1+\text{count}(c)1+count(c) both before and after.
Case 4:
For α\alphaα and β\betaβ, it is from 1+count(c)1 + \text{count}(c)1+count(c) to 2+count(c)2 + \text{count}(c)2+count(c).
For γ\gammaγ and δ\deltaδ, it is 1+count(c)+count(c′)1 + \text{count}(c) + \text{count}(c')1+count(c)+count(c′) both before and after.
For ?\epsilon? and ζ\zetaζ, it is 1+count(c)1 + \text{count}(c)1+count(c) both before and after.
This increase in the count for α\alphaα and β\betaβ is because xxx before indicated an extra black.
13.4-6
僅當(dāng)xxx的兄弟www為紅色時才會出現(xiàn)情況1。如果x.px.px.p是紅色的,那么連續(xù)會有兩個紅色,即x.px.px.p(也是w.pw.pw.p)和www,即使在調(diào)用RB-DELETE\text{RB-DELETE}RB-DELETE之前也會連續(xù)出現(xiàn)這兩個紅色。
13.4-7
不,紅黑樹不一定是一樣的。 以下是兩個示例:一個是樹的形狀發(fā)生變化,另一個是形狀保持不變但節(jié)點顏色發(fā)生變化。
思考題
13-1(持久動態(tài)集合)
a.
b.
c.
d.
e.
13-2(紅黑樹上的連接操作)
a.
b.
c.
d.
e.
f.
13-3(AVL樹)
a.
b.
c.
d.
13-4(treap樹)
a.
b.
c.
d.
e.
f.
g.
h.
i.
j.
總結(jié)
以上是生活随笔為你收集整理的《算法导论》第三版第13章 红黑树 练习思考题 个人答案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 省选之前的未完成的计划(截至到省选)
- 下一篇: ORA-01033: ORACLE in