1 / 21

数据结构基础 期中考试解答

数据结构基础 期中考试解答. 2011-10-8. 选择 1. Which of the following is true? a. n ! grows faster than x n for any x b. ( log n) 2 grows faster than n 1/2 c. n grows slower than n 1/2 (log n) 2 d. log(n 2 ) grows faster than (log n) 2 解答:选 a 增长速度: N ! > 指数型 > 多项式型 > 对数型. 选择 2.

kaiya
Download Presentation

数据结构基础 期中考试解答

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 数据结构基础期中考试解答 2011-10-8

  2. 选择1 • Which of the following is true? • a. n! grows faster than xnfor any x • b. (log n)2 grows faster than n1/2 • c. n grows slower than n1/2(log n)2 • d. log(n2) grows faster than (log n)2 • 解答:选a • 增长速度: • N!>指数型>多项式型>对数型

  3. 选择2 • Two stacks are implemented in an array of size m. Initially, the “top” positions of the two stacks are set as: top1= -1, top2=m. In what condition that both stacks are full? • a.top2-top1=0 b.top1+1=top2 b.top1+top2=m d.top1=top2+1 • 解答:选b 0 1 …….. m-1 top1 top2

  4. 选择3 • Suppose that 5 numbers are pushed onto a stack in the order of 12345. If the output order is 35421, what is the minimum capacity of the stack should have? • a. 2 b. 3 c. 4 d. 5 • 解答:选c • 元素5是第5个push进的,是第2个pop出来的,所以深度至少为4

  5. 选择4 • When conversing the infix expression 3 *2 + 8 / 4 into its postfix , which order of stack operations will be taked? Here let P stand for “push” and O for “pop”. • a. PPPOOO b.POPOPOc.POPPOOd.PPOOPO • 解答:选c • 32*84/+

  6. 选择5 • A queue is implemented in a circular array of size 6. Now, rear and front positions are 0 and 4 respectively, afterenqueue 2 elements and dequeue 2 elements, what are the positions of rear and front respectively? • a.0 and 4 b.2 and 6 c.2 and 0 d.2 and 2 • 解答:选c • Rear:0 -> 2 front: 4 -> 0(6)

  7. 选择6 • Given a quadtree(四叉树) with 4 nodes of degree 2, 2 nodes of degree 3, 1 nodes of degree 4. The number of leaf nodes in this tree is ______. • a. 8 b. 12 c. 18 d. 20 • 解答:选b • 设叶子个数为n0,度为1的结点数为n1,度为2结点数为n2,度为4结点数为n4度为4结点数为n4 • 结点总数(记为n): n=n0+n1+n2+n3+n4 另一方面,1度,2度,3度,4度结点分别为有1-4个孩子,结点总数又可以表示为:n=nl+2n2+3n3+4n4+1故有n0+n1+n2+n3+n4=nl+2n2+3n3+4n4+1,即得n0=n2+2n3+3n4+1

  8. 选择7 • How many leaf node does a complete binary tree with 2435 nodes have? • a. 1218 b. 1217 c. 812 d. cannot be determined • 解答:选a • 深度为11的完全二叉树节点为2^11-1=2047,所以第12层的叶节点是2435-2047=388个。第11层一共有节点数2^10=1024,则该层叶节点个数是1024-388/2=830。所以叶节点一共有388+830=1218个

  9. 选择8 • In a ________, keys along each of the paths from any node to the root are ordered. • a. heap b. binary search tree c. complete tree d. none of the above • 解答:选a • B的特点是左儿子<根<右儿子,并不能保证path上节点的大小顺序

  10. 选择9 • In a min-heap with n elements, at what position may the maximum key exist? • a. n/2b. n/2 - 1c. 1d. n/2 + 2 • 解答:选d • Min-heap的特点是:1.父节点小,子节点大;2.是一个完全二叉树。所以最大值一定在最底层。 • n/2是节点n的parent,必定在倒数第二层。 • 只有d选项会出现在最底层。

  11. 选择10 • Among the following sorting algorithms, ____ has the average run time O(NlogN) with O(N) extra spaces. • a. Quick sort b. Heap sort • c. Merge sort d. Insertion sort • 解答:选c • Quicksort- O(NlogN),O(LogN)extraspaces • Heapsort-O(NlogN),noextraspaces • Mergesort-O(NlogN), O(N) extraspaces • Insertionsort-O(N2), noextraspaces

  12. 选择11 • In the following integer sequences, which is(are) NOT a heap. • a. (100,85,98,77,80,60,82,40,20,10,66) • b. (100,98,85,82,80,77,66,60,40,20,10) • c. (10,20,40,60,66,77,80,82,85,98,100) • d. (100,85,40,77,80,60,66,98,82,10,20) • 解答:选择d • D不是maxheap也不是minheap

  13. 选择12 • Among the following sorting algorithms, which has the average run time O(NlogN) with small extra space. • a. Quick sortb. Heap sortc. Merge sortd. Insertion sort • 解答:选b • Quicksort- O(NlogN),O(LogN)extraspaces • Heapsort-O(NlogN),noextraspaces • Mergesort-O(NlogN), O(N) extraspaces • Insertionsort-O(N2), noextraspaces

  14. 选择13 • Followings are the middle stages during sorting (2,12,16,88,5,10), which sorting algorithm could the most possible used? • First Stage:2,12,16,5,10,88 • Second Stage:2,12,5,10,16,88 • Third Stage:2,5,10,12,16,88 • a.Bubbles Sort b.Selection Sort c.Heap Sort d.Insertion Sort • 解答:选a

  15. 填空 maxheap的deletemax函数 ElementTypeDeleteMax( PriorityQueue H ) { inti, Child; ElementTypeMaxElement, LastElement; MaxElement = ; H->Elements[1]//此处保存这个最大的值 LastElement = H->Elements[ H->Size-- ]; for( i = 1; i * 2 <= H->Size; i = Child ) { Child = i * 2; //找大儿子 if( Child!=H->size && H->Elements[child]<H->Elements[child+1] ) Child++; //如果右儿子大,就指向右儿子 if( LastElement < H->Elements[ Child ] ) H->Elements[i] = H->Elements[child] ;//大的向上移 else break; } H->Elements[ i ] = LastElement; return MaxElement; }

  16. 第三大题第一题 • The followings are the partial results of a binary tree’s traversals in pre-order, in-order and post-order. Please draw the corresponding tree. (13 points) • Pre-order : _B_F_ICEH_G • In-order: D_KFIA_EJC_ • Post-order: _K_FBHJ_G_A

  17. Pre-order : 根-左-右 • In-order: 左-根-右 • Post-order: 左-右-根 • 共11个元素 A G B D F J I C H E K A B D F K I C E H J G D B K F I A H E J C G D K I F B H J E G C A

  18. 第三大题第二题 • A binary search treesix numbers 2 4 6 8 10 12 are the keys in this tree. Please: • (a)show the pre-order result of this tree. (7 points) • (b)show the resulting binary search tree after 7 is inserted.(6 points) • 解答: • Binarysearchtree 特点是左<根<右 • 所以pre-order是8,2,6,4,10,12 • 插入7 10 12 2 6 4 7 8

  19. 第四大题 • Please write a non-recursive function FirstNode() that return the first node of a binary tree in post travel order. (20 points) • structTreeNode { • ElementType Element; • TreeNode *left; • TreeNode *right; • } • TreeNode *FirstNode (TreeNode *tree) • 解答: • Post-order的遍历顺序是左-右-根 • 寻找第一个node

  20. TreeNode *FirstNode (TreeNode *p) { while (p && (p->left || p->right)) { if (p->left) p=p->left; else p=p->right; } return p }

  21. 一些注意事项 • Project提交格式 • 打包提交. • Pdf命名规则:hqm_G18_P2.pdf • 源代码命名规则:hqm_G18_P2.c或者.cpp • 不按规则的writer会扣分 • Homework • 每次周三上机课交给我,并领取上次作业 • 至今还有同学没有交过homework • 总分计算规则 • 期末考试50分+期中考试10分+homework15分+project25分 =100分

More Related