2022年MarkAllenWeiss数据结构与算法分析课后习题答案 .pdf
《2022年MarkAllenWeiss数据结构与算法分析课后习题答案 .pdf》由会员分享,可在线阅读,更多相关《2022年MarkAllenWeiss数据结构与算法分析课后习题答案 .pdf(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Chapter6:PriorityQueues(Heaps)6.1Yes. When an element is inserted, we compare it to the current minimum and change theminimumifthe new element is smaller.DeleteMinoperations are expensive in thisscheme.6.21326754151412910111381321264815149751113106.3The result of three DeleteMins, starting with both
2、 of the heapsin Exercise 6.2, is as fol-lows:4651371081514129114651271081514913116.46.5Theseare simple modi?cations to the code presentedin the text and meant asprogrammingexercises.6.6225. To seethis, start with i =1 and position at the root. Follow the path toward the lastnode, doubling i when tak
3、ing a left child, and doubling i and adding one when taking aright child.-29-名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - - - - - - - - 6.7(a) We show that H (N ), which is the sum of the heights of nodesin a complete binary treeof N nodes,is N - b(N)
4、, where b(N ) is the number of ones in the binary representationofN . Observe that for N = 0 and N = 1, the claim is true. Assume that it is true for valuesofk up to and including N - 1. Suppose the left and right subtrees have L and R nodes,respectively. Since the root hasheightlog N , we haveH (N
5、) =log N+ H (L ) + H (R)=log N+ L - b (L ) + R - b (R)= N - 1 + (log N- b(L ) - b(R)The second line follows from the inductive hypothesis, and the third follows becauseL + R = N - 1. Now the last node in the tree is in either the left subtree or the right sub-tree.Ifitis intheleftsubtree, then the r
6、ightsubtree isaperfect tree, andb(R) =log N- 1. Further, the binary representationof N and L are identical, with theexception that the leading 10 in N becomes1 in L . (For instance,if N = 37 = 100101, L =10101.) It is clear that the seconddigit of N must be zero if the last nodeis in the left sub-tr
7、ee. Thus in this case,b(L) = b(N ), andH (N ) = N - b (N )If the last node is in the right subtree,then b(L ) =log N . The binary representationof Ris identical to N , except that the leading 1 is not present. (For instance,if N = 27 = 101011,L = 01011.) Thus b(R) = b (N ) - 1, and againH (N ) = N -
8、 b (N )(b) Run a single-elimination tournament among eight elements. This requires seven com-parisonsand generatesordering information indicated by thebinomial tree shown here.abcdefghThe eighth comparison is betweenb and c. If c is less than b, then b is madeachild of c.Otherwise, both c and d arem
9、adechildren of b.(c) A recursive strategy is used. Assume that N = 2k. A binomial tree is built for the Nelements asin part (b). The largest subtreeof the root is then recursively converted into abinary heap of 2k- 1elements. The last element in the heap (which is the only one on anextra level) is t
10、hen inserted into the binomial queue consisting of the remaining binomialtrees,thus forming anotherbinomial tree of 2k- 1elements. At that point, theroot hasasub-tree that is a heap of 2k- 1- 1 elements and another subtree that is a binomial tree of 2k- 1elements. Recursively convert that subtreeint
11、o aheap; now the whole structure is abinaryheap. The running time for N = 2ksatis?es T(N) = 2T (N / 2) + log N. The base case isT(8) = 8.-30-名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页 - - - - - - - - - 6.8Let D1, D2, ., Dkbe random variables representin
12、gthe depth of the smallest,secondsmal-lest, and kthsmallest elements, respectively. We are interested in calculating E (Dk). Inwhat follows, we assumethat the heapsize N is one less than a power of two (that is, thebottom level is completely ?lled) but suf?ciently large so that terms bounded by O (1
13、 / N)are negligible.Without loss of generality, we may assumethat the kthsmallest element isin the left subheapof the root. Let pj,kbe the probability that this element is the jthsmal-lest elementin the subheap.Lemma: For k1, E (Dk) =j=1k- 1pj ,k(E(Dj) + 1).Proof: An element that is at depth d in th
14、e left subheap is at depth d + 1 in the entiresubheap. Since E (Dj+ 1) = E(Dj) + 1, thetheorem follows.Since by assumption, the bottom level of the heap is full, each of second,third, ., k- 1thsmallest elementsarein the left subheapwith probability of 0.5. (Technically, the probabil-ity should be ?1
15、2- 1/(N - 1) of being in the right subheapand ?12+ 1/(N - 1) of being in theleft, since we have already placed the kthsmallest in theright. Recall that we haveassumedthat terms of size O (1/N ) can be ignored.) Thuspj,k= pk- j,k=2k- 21_(j - 1k - 2)Theorem: E (Dk) log k .Proof: The proof is by induct
16、ion.The theorem clearly holds for k = 1 and k = 2. We thenshow that it holds for arbitrary k 2 on theassumption that it holds for all smaller k. Now,by the inductive hypothesis, for any 1 j k- 1,E (Dj) + E(Dk- j) log j + log k- jSince f (x) = log x is convex for x 0,log j + log k- j 2log (k / 2)Thus
17、E (Dj) + E(Dk - j) log (k/ 2) + log (k/ 2)Furthermore, sincepj ,k= pk - j ,k,pj,kE (Dj) + pk- j,kE (Dk- j) pj ,klog (k/ 2) + pk- j,klog (k/ 2)From the lemma,E (Dk) =j =1k- 1pj,k(E (Dj) + 1)= 1 +j =1k- 1pj ,kE (Dj)ThusE (Dk) 1 +j =1k- 1pj ,klog (k/ 2)-31-名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - -
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 2022年MarkAllenWeiss数据结构与算法分析课后习题答案 2022 MarkAllenWeiss 数据结构 算法 分析 课后 习题 答案
限制150内