- 1、本文档共7页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
3-solutions-clrs-13
Page 1 of 7 PSU CMPSC 465 Spring 2013
Homework Solutions – Unit 3: Chapter 13
CMPSC 465
Disclaimer: This is a draft of solutions that has been prepared by the TAs and the instructor makes no guarantees that
solutions presented here contain the level of detail that would be expected on an exam. Any errors or explanations you find
unclear should be reported to either of the TAs for corrections first.
Exercise 13.1-1
In the style of Figure 13.1(a), draw the complete binary search tree of height 3 on the keys {1, 2, …, 15}. Add the NIL leaves
and color the nodes in three different ways such that the black-heights of the resulting red-black trees are 2, 3, and 4.
Solution:
Page 2 of 7 PSU CMPSC 465 Spring 2013
Exercise 13.1-2
Draw the red-black tree that results after TREE-INSERT is called on the tree shown in the figure below with key 36. If the
inserted node is colored red, is the resulting tree a red-black tree? What if it is colored black?
Solution:
If the node with key 36 is inserted and colored red, the red-black tree becomes:
We can see that it violates following red-black tree property:
A red node in the red-black tree cannot have a red node as its child.
So the resulting tree is not a red-black tree.
If the node with key 36 is inserted and colored black, the red-black tree becomes:
We can see that it violates following red-black tree property:
For each node, all paths from the node to descendent leaves contain the same number of black nodes (e.g. consider node
with key 30).
So the resulting tree is not a red-black tree either.
36
36
Page 3 of 7 PSU CMPSC 465 Spring 2013
Exercise 13.1-5
Show that the longest simple path from a node x in a red-black tree to a descendant leaf has length at most twice that of the
shortest simple path from node x to a descendant leaf.
Proof:
In the longest path, at least every other node is black. In the shortest path, at most every node is black. Since the two
文档评论(0)