《数据结构与算法分析》(C++第二版)【美】Clifford-A.Shaffer著-课后习题答案-二(共28页).doc
《《数据结构与算法分析》(C++第二版)【美】Clifford-A.Shaffer著-课后习题答案-二(共28页).doc》由会员分享,可在线阅读,更多相关《《数据结构与算法分析》(C++第二版)【美】Clifford-A.Shaffer著-课后习题答案-二(共28页).doc(28页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上数据结构与算法分析(C+第二版)【美】Clifford A.Shaffer著 课后习题答案 二5Binary Trees5.1 Consider a non-full binary tree. By definition, this tree must have some internalnode X with only one non-empty child. If we modify the tree to removeX, replacing it with its child, the modified tree will have a higher frac
2、tion ofnon-empty nodes since one non-empty node and one empty node have beenremoved.5.2 Use as the base case the tree of one leaf node. The number of degree-2 nodesis 0, and the number of leaves is 1. Thus, the theorem holds.For the induction hypothesis, assume the theorem is true for any tree withn
3、 1 nodes.For the induction step, consider a tree T with n nodes. Remove from the treeany leaf node, and call the resulting tree T. By the induction hypothesis, Thas one more leaf node than it has nodes of degree 2.Now, restore the leaf node that was removed to form T. There are twopossible cases.(1)
4、 If this leaf node is the only child of its parent in T, then the number ofnodes of degree 2 has not changed, nor has the number of leaf nodes. Thus,the theorem holds.(2) If this leaf node is the child of a node in T with degree 2, then that nodehas degree 1 in T. Thus, by restoring the leaf node we
5、 are adding one newleaf node and one new node of degree 2. Thus, the theorem holds.By mathematical induction, the theorem is correct.32335.3 Base Case: For the tree of one leaf node, I = 0, E = 0, n = 0, so thetheorem holds.Induction Hypothesis: The theorem holds for the full binary tree containingn
6、 internal nodes.Induction Step: Take an arbitrary tree (call it T) of n internal nodes. Selectsome internal node x from T that has two leaves, and remove those twoleaves. Call the resulting tree T. Tree T is full and has n1 internal nodes,so by the Induction Hypothesis E = I + 2(n 1).Call the depth
7、of node x as d. Restore the two children of x, each at leveld+1. We have nowadded d to I since x is now once again an internal node.We have now added 2(d + 1) d = d + 2 to E since we added the two leafnodes, but lost the contribution of x to E. Thus, if before the addition we hadE = I + 2(n 1) (by t
8、he induction hypothesis), then after the addition wehave E + d = I + d + 2 + 2(n 1) or E = I + 2n which is correct. Thus,by the principle of mathematical induction, the theorem is correct.5.4 (a) template void inorder(BinNode* subroot) if (subroot = NULL) return; / Empty, do nothingpreorder(subroot-
9、left();visit(subroot); / Perform desired actionpreorder(subroot-right();(b) template void postorder(BinNode* subroot) if (subroot = NULL) return; / Empty, do nothingpreorder(subroot-left();preorder(subroot-right();visit(subroot); / Perform desired action5.5 The key is to search both subtrees, as nec
10、essary.template bool search(BinNode* subroot, Key K);if (subroot = NULL) return false;if (subroot-value() = K) return true;if (search(subroot-right() return true;return search(subroot-left();34 Chap. 5 Binary Trees5.6 The key is to use a queue to store subtrees to be processed.template void level(Bi
11、nNode* subroot) AQueueBinNode* Q;Q.enqueue(subroot);while(!Q.isEmpty() BinNode* temp;Q.dequeue(temp);if(temp != NULL) Print(temp);Q.enqueue(temp-left();Q.enqueue(temp-right();5.7 template int height(BinNode* subroot) if (subroot = NULL) return 0; / Empty subtreereturn 1 + max(height(subroot-left(),h
12、eight(subroot-right();5.8 template int count(BinNode* subroot) if (subroot = NULL) return 0; / Empty subtreeif (subroot-isLeaf() return 1; / A leafreturn 1 + count(subroot-left() +count(subroot-right();5.9 (a) Since every node stores 4 bytes of data and 12 bytes of pointers, theoverhead fraction is
13、12/16 = 75%.(b) Since every node stores 16 bytes of data and 8 bytes of pointers, theoverhead fraction is 8/24 33%.(c) Leaf nodes store 8 bytes of data and 4 bytes of pointers; internal nodesstore 8 bytes of data and 12 bytes of pointers. Since the nodes havedifferent sizes, the total space needed f
14、or internal nodes is not the sameas for leaf nodes. Students must be careful to do the calculation correctly,taking the weighting into account. The correct formula looks asfollows, given that there are x internal nodes and x leaf nodes.4x + 12x12x + 20x= 16/32 = 50%.(d) Leaf nodes store 4 bytes of d
15、ata; internal nodes store 4 bytes of pointers.The formula looks as follows, given that there are x internal nodes and35x leaf nodes:4x4x + 4x= 4/8 = 50%.5.10 If equal valued nodes were allowed to appear in either subtree, then during asearch for all nodes of a given value, whenever we encounter a no
16、de of thatvalue the search would be required to search in both directions.5.11 This tree is identical to the tree of Figure 5.20(a), except that a node withvalue 5 will be added as the right child of the node with value 2.5.12 This tree is identical to the tree of Figure 5.20(b), except that the val
17、ue 24replaces the value 7, and the leaf node that originally contained 24 is removedfrom the tree.5.13 template int smallcount(BinNode* root, Key K);if (root = NULL) return 0;if (KEComp.gt(root-value(), K)return smallcount(root-leftchild(), K);elsereturn smallcount(root-leftchild(), K) +smallcount(r
18、oot-rightchild(), K) + 1;5.14 template void printRange(BinNode* root, int low,int high) if (root = NULL) return;if (KEComp.lt(high, root-val() / all to leftprintRange(root-left(), low, high);else if (KEComp.gt(low, root-val() / all to rightprintRange(root-right(), low, high);else / Must process both
19、 childrenprintRange(root-left(), low, high);PRINT(root-value();printRange(root-right(), low, high);5.15 The minimum number of elements is contained in the heap with a single nodeat depth h 1, for a total of 2h1 nodes.The maximum number of elements is contained in the heap that has completelyfilled u
20、p level h 1, for a total of 2h 1 nodes.5.16 The largest element could be at any leaf node.5.17 The corresponding array will be in the following order (equivalent to levelorder for the heap):12 9 10 5 4 1 8 7 3 236 Chap. 5 Binary Trees5.18 (a) The array will take on the following order:6 5 3 4 2 1The
21、 value 7 will be at the end of the array.(b) The array will take on the following order:7 4 6 3 2 1The value 5 will be at the end of the array.5.19 / Min-heap classtemplate class minheap private:Elem* Heap; / Pointer to the heap arrayint size; / Maximum size of the heapint n; / # of elements now in
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构与算法分析 数据结构 算法 分析 C+ 第二 Clifford Shaffer 课后 习题 答案 28
链接地址:https://www.taowenge.com/p-14076140.html
限制150内