数据结构与算法习题答案
1. Fill the blank with correct C++ codes:
(1) Given an array storing integers ordered by distinct value without duplicate, modify the binary
search routines to return the position of the integer with the smallest value greater than K when K itself does not appear in the array. Return ERROR if the greatest value in the array is less than K: (12 scores)
// Return position of smallest element >= K int newbinary(int array[], int n, int K) { int l = -1;
int r = n; // l and r beyond array bounds while (l+1 != r) { // Stop when l and r meet
___ int i=(l+r)/2_____; // Look at middle of subarray if (K < array[i]) __ r=i ___; // In left half if (K == array[i]) __ return i ___; // Found it if (K > array[i]) ___ l=i ___ // In right half }
// K is not in array or the greatest value is less than K
if K<= array[n-1] (or r!=n) // the greatest value in the array is not less than K with r updated
return r ; // when K itself does not appear in the array
else return ERROR; // the integer with the greatest value less than K }
(2) The height of a complete binary tree with k nodes is 「log2(k+1)︱(1 node tree has hight 1)
(3) The number of different shapes of binary trees with 5nodes is _42_.
2. A certain binary tree has the preorder enumeration as ABECDFGHIJ and the inorder enumeration as EBDCAFIHGJ. Try to draw the binary tree and give the postorder enumeration. (The process of your solution is required!!!) A A A A
B F B F B F EBDC FIHGJ
E IHGJ E C G E C G CD
J IH J H D D Postorder enumeration: EDCBIHJGFA
I
3. Determine Θ for the following code fragments in the average case. Assume that all variables are of type int. (1) sum=0;
for (i=0; i<5; i++) for (j=0; j sum++; solution : Θ___(n)_______ (2) sum = 0; for(i=1;i<=n;i++) for(j=n;j>=i;j--) sum++; solution : Θ__(n2)________ (3) sum=0; if (EVEN(n)) for (i=0; i sum=sum+n; solution : Θ___(n)_____ 4. Trace by hand the execution of creation a binary search tree with the input sequence as : {46,25,78,62,12,37,70,29} which is empty tree initially. Solution: BST obtained with data inserted one by one 46 25 78 12 37 62 29 70 5. Design an algorithm to transfer the score report from 100-point to 5-point, the level E corresponding score<60, 60~69 being D, 70~79 being C, 80~89 as B,score>=90 as A. The distribution table is as following. Please describe your algorithm using a decision tree and give the total path length. Score in 100-point Distribution rate 0-59 5% 60-69 10% 70-79 45% 80-89 25% 90-100 15% solution: the design logic is to build a Huffman tree 100% 0 1 55% 45% 100 1 0 C 30% 25% 0 1 B 15% 15% A 0 1 D 5% 10% D E Total length: 4 + 4 + 3 + 2 + 1= 14, Average length: 4 * 5% +10% * 4 + 15 %* 3 + 25% * 2 + 45% = 2.00, the 0-false,1-true as the logic branches. 6. Assume a disk drive is configured as follows. The total storage is approximately 1.35G divided among 15 surfaces. Each surface has 612 tracks; there are 144 sectors/track, 1024 byte/sector, and 16 sectors/cluster. The interleaving factor is four. The disk turns at 7200rmp (8.33 ms/r). The track-to-track seek time is 20 ms, and the average seek time is 80 ms. Now how long does it take to read all of the data in a 360 KB file on the disk? Assume that the file’s clusters are spread randomly across the disk. A seek must be performed each time the I/O reader moves to a new track. Show your calculations. (The process of your solution is required!!!) Solution: The first question is how many clusters the file requires? A cluster holds 16*1K = 16K. Thus, the file requires 360/16=22.5 clusters=22complete cluster and 8k(8 sectors) The time to read a cluster is seek time to the cluster+ latency time + (interleaf factor × rotation time). Average seek time is defined to be 80 ms. Latency time is 0.5 * 8.33 ms (60/7200≈8.33ms), and cluster rotation time is 4* (16/144)*8.33. Seek time for the total file read time is 22* (80 + 0.5 * 8.33+ 4 * (16/144)*8.33 ) +(80 + 0.5 * 8.33+ 4 * (8 /144)*8.33 )≈2019.095ms 7. Using closed hashing, with double hashing to resolve collisions, insert the following keys into a hash table of eleven slots (the slots are numbered 0 through 10). The hash functions to be used are H1 and H2, defined below. You should show the hash table after all eight keys have been inserted. Be sure to indicate how you are using H1 and H2 to do the hashing. ( The process of your solution is required!!!) H1(k) = 3k mod 11 H2(k) = 7k mod 10+1 Keys: 22, 41, 53, 46, 30, 13, 1, 67. Solution: H1(22)=0, H1(41)=2, H1(53)=5, H1(46)=6, no conflict When H1(30)=2, H2(30)=1 (2+1*1)=3,so 30 enters the 3rd slot; H1(13)=6, H2(13)=2 (6+1*2)=8, so 13 enters the 8th slot; H1(1)=3, H2(1)=8 (3+5*8)= 10 so 1 enters 10 (pass by 0, 8, 5, 2 ); H1(67)=3, H2(67)=10 (3+2*10)= 1 so 67 enters 1(pass by 2) 22 67 41 30 53 46 13 1 0 1 2 3 4 5 6 7 8 9 10 8. You are given a series of records whose keys are integers. The records arrive in the following order: C, S, D, T, A, M, P, I, B, W, N, G, U, R. Show the 2-3 tree that results from inserting these records. (the process of your solution is required!!!) Solution: MS
相关推荐:
- [建筑文档]2018年公需课:专业技术人员创新能力与
- [建筑文档]2013年福建教师招考小学数学历年真题
- [建筑文档]高中信息技术课flash知识点总结 - 图文
- [建筑文档]电工实训 - 图文
- [建筑文档]最高院公告案例分析100篇(民商篇)
- [建筑文档]南开中学高2017级14-15学年(上)期末
- [建筑文档]五粮液集团战略分析
- [建筑文档]鲁教版(2012秋季版)九年级化学 酸碱
- [建筑文档]超星尔雅2017中国哲学概论自整理题库答
- [建筑文档]关于成为海口金盘饮料公司材料独家供货
- [建筑文档]LNG学习资料第一册 基础知识 - 图文
- [建筑文档]四年级品社下册《好大一个家》复习资料
- [建筑文档]现阶段领导权力腐败的特点及发展趋势
- [建筑文档]魏晋南北朝诗歌鉴赏—嵇康
- [建筑文档]坚持追求真爱是理智的行为 正方一辩稿
- [建筑文档]湘西州刑释解教人员帮教安置工作存在的
- [建筑文档]园林工程试题库及答案
- [建筑文档]计算机长期没有向WSUS报告状态
- [建筑文档]日语最新流行语
- [建筑文档]B62-016 景观进场交底专题会议
- 2018年中考语文课内外古诗词鉴赏专题复
- 高考试题研究心得体会
- C语言基础题及答案
- 电气控制及PLC习题及答案
- 都昌小学家长学校汇报材料
- GMAT作文模板正确使用方法
- 俄军办坦克大赛:中国99式有望与豹2A6
- 成本会计练习题
- 酒店餐饮业最流行的5S管理方法
- 2014-2015学年山东省菏泽市高二(下)
- 《黄鹤楼送孟浩然之广陵》教案、说课、
- 2013年结构化学自测题 有答案版
- 2011西安世界园艺博览会游览解说词(附
- 窗口文明单位示范单位创建活动总结
- 2018满分超星尔雅就业课后练习期末答案
- 韶山市城市总体规划-基础资料
- 苏教版第三单元知识点归纳
- 第4章 曲轴模态分析
- 加大查办案件力度的思考
- 武汉CPC导轨介绍




