Mark Allen Weiss 数据结构与算法分析 课后习题答案4.pdf
《Mark Allen Weiss 数据结构与算法分析 课后习题答案4.pdf》由会员分享,可在线阅读,更多相关《Mark Allen Weiss 数据结构与算法分析 课后习题答案4.pdf(11页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter 4:Trees4.1(a)A?.(b)G?,H?,I?,L?,M?,and K?.4.2For node B?:(a)A?.(b)D?and E?.(c)C?.(d)1.(e)3.4.34.4.4There are N?nodes.Each node has two pointers,so there are 2N?pointers.Each node butthe root has one incoming pointer from its parent,which accounts for N?1 pointers.Therest are NULL.?4.5Proof is
2、 by induction.The theorem is trivially true for H?=0.Assume true for H?=1,2,.,k?.A tree of height k?+1 can have two subtrees of height at most k?.These can have at most2k?+11 nodes each by the induction hypothesis.These 2k?+22 nodes plus the root prove thetheorem for height k?+1 and hence for all he
3、ights.4.6This can be shown by induction.Alternatively,let N?=number of nodes,F?=number offull nodes,L?=number of leaves,and H?=number of half nodes(nodes with one child).Clearly,N?=F?+H?+L?.Further,2F?+H?=N?1(see Exercise 4.4).Subtracting yieldsL?F?=1.4.7This can be shown by induction.In a tree with
4、 no nodes,the sum is zero,and in a one-nodetree,the root is a leaf at depth zero,so the claim is true.Suppose the theorem is true for alltrees with at most k?nodes.Consider any tree with k?+1 nodes.Such a tree consists of an i?node left subtree and a k?i?node right subtree.By the inductive hypothesi
5、s,the sum forthe left subtree leaves is at most one with respect to the left tree root.Because all leaves areone deeper with respect to the original tree than with respect to the subtree,the sum is atmost 12with respect to the root.Similar logic implies that the sum for leaves in the rightsubtree is
6、 at most 12,proving the theorem.The equality is true if and only if there are nonodes with one child.If there is a node with one child,the equality cannot be true becauseadding the second child would increase the sum to higher than 1.If no nodes have onechild,then we can find and remove two sibling
7、leaves,creating a new tree.It is easy to seethat this new tree has the same sum as the old.Applying this step repeatedly,we arrive at asingle node,whose sum is 1.Thus the original tree had sum 1.4.8(a)-*a b+c d e.(b)(a*b)*(c+d)-e.(c)a b*c d+*e-.-14-4.91234567912456794.11 This problem is not much dif
8、ferent from the linked list cursor implementation.We maintainan array of records consisting of an element field,and two integers,left and right.The freelist can be maintained by linking through the left field.It is easy to write the CursorNew?and CursorDispose?routines,and substitute them for malloc
9、 and free.4.12(a)Keep a bit array B?.If i?is in the tree,then B?i?is true;otherwise,it is false.Repeatedlygenerate random integers until an unused one is found.If there are N?elements already inthe tree,then M?N?are not,and the probability of finding one of these is(M?N?)/M?.Thus the expected number
10、 of trials is M?/(M?N?)=/(1).(b)To find an element that is in the tree,repeatedly generate random integers until analready-used integer is found.The probability of finding one is N?/M?,so the expectednumber of trials is M?/N?=.(c)The total cost for one insert and one delete is /(1)+=1+1/(1).Set-ting
11、 =2 minimizes this cost.4.15(a)N?(0)=1,N?(1)=2,N?(H?)=N?(H?1)+N?(H?2)+1.(b)The heights are one less than the Fibonacci numbers.4.16123456794.17 It is easy to verify by hand that the claim is true for 1 k?3.Suppose it is true for k?=1,2,3,.H?.Then after the first 2H?1 insertions,2H?1is at the root,an
12、d the right subtree isa balanced tree containing 2H?1+1 through 2H?1.Each of the next 2H?1insertions,namely,2H?through 2H?+2H?1 1,insert a new maximum and get placed in the right-15-subtree,eventually forming a perfectly balanced right subtree of height H?1.This followsby the induction hypothesis be
13、cause the right subtree may be viewed as being formed fromthe successive insertion of 2H?1+1 through 2H?+2H?1 1.The next insertion forces animbalance at the root,and thus a single rotation.It is easy to check that this brings 2H?tothe root and creates a perfectly balanced left subtree of height H?1.
14、The new key isattached to a perfectly balanced right subtree of height H?2 as the last node in the rightpath.Thus the right subtree is exactly as if the nodes 2H?+1 through 2H?+2H?1wereinserted in order.By the inductive hypothesis,the subsequent successive insertion of2H?+2H?1+1 through 2H?+1 1 will
15、 create a perfectly balanced right subtree of heightH?1.Thus after the last insertion,both the left and the right subtrees are perfectly bal-anced,and of the same height,so the entire tree of 2H?+1 1 nodes is perfectly balanced(andhas height H?).4.18 The two remaining functions are mirror images of
16、the text procedures.Just switch Right?and Left?everywhere.4.20 After applying the standard binary search tree deletion algorithm,nodes on the deletion pathneed to have their balance changed,and rotations may need to be performed.Unlike inser-tion,more than one node may need rotation.4.21(a)O?(log lo
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Mark Allen Weiss 数据结构与算法分析 课后习题答案4 数据结构 算法 分析 课后 习题 答案
限制150内