NTU-Exam 板


LINE

课程名称︰资料结构与演算法 课程性质︰必修 课程教师:林轩田 开课学院:电资学院 开课系所︰资工系 考试日期(年月日)︰2013.04.18 考试时限(分钟):120分钟 试题 : Midterm Examination Problem Sheet TIME: 04/18/2013, 10:20-12:20 This is an open-book exam. You can use any printed materials as your reference during the exam. Any electronic devices are not allowed. Any form of cheating, lying or plagiarism will not be tolerated. Students can get zero scores and/or get negative scores and/or fail the class and/or be kicked out of school and/or receive other punishments for those kinds of misconducts. Both English and Chinese (if suited) are allowed for answering the questions. We do not accept any other languages. ┌─────────────────────────────────────┐ │There are 8 problems in the exam, each worth 25 points ─ the full credit │ │is 200 points. Some problems come with a few sub-problems to help you get │ │partial credits. For the 8 problems, 3 of them are marked with * and are │ │supposedly simple; 3 of them are marked with ** and are supposedly │ │regular; 2 of them are marked with *** and are supposedly difficult. There│ │is one bonus problem, numbered 1126, with 10 points. The problems are │ │roughly ordered by difficulty. │ └─────────────────────────────────────┘ (1) (25%, *) To count the number of boys/girls in some department, student C wrote the following program: 1 #include <iostream> 2 using namespace std; 3 void increase(int count){ 4 count++; 5 } 6 7 int main(){ 8 char ID[1126][11]; 9 int num_student; 10 int countBoy = 0; 11 int countGirl = 0; 12 13 //some code that initializes the ID array 14 //with something like "A123456789" per row 15 //also, num_student will be initialized 16 17 for(int i = 0; i < num_student; i++){ 18 if (ID[i][1] == '1') 19 increase(countBoy); 20 else 21 increase(countGirl); 22 } 23 cout << countBoy << ' ' << countGirl << endl; 24 return 0 ; 25 } (a) (10%) Assume that num_student is 1126 with 460 boys and 666 girls, what is the output of the program above? (b) (15%) If your answer above is not 460 666, explain how you can modify the program above with only one line to output 460 666. If your answer above is 460 666, explain to the TAs how the memory slots of the local variable countBoy in main and the local variable count in increase are changed during the first call to increase(countBoy). (2) (25%, *) Consider a complete binary tree with 11 nodes where each nodes are numbered from 1, 2, …, 11 that corresponds to its position in the array representation, with root being node number 1. That is, node i has a left child (if any) at 2i and a right child (if any) at 2i + 1. (a) (10%) Draw the binary tree with clear illustrations on the node numbers. (b) (5%) Do a post-order traversal in the binary tree above and print out the node numbers visited. (c) (5%) Do a in-order traversal in the binary tree above and print out the node numbers visited. (d) (5%) Do a pre-order traversal in the binary tree above and print out the node numbers visited. (3) (25%, *) You have been using a singly-linked list for your application, where each node is of the form struct Node{ int value; Node* next; }; But now there is a new need in your application that requires you to find the previous nodes quickly. Therefore, you need to write a program to convert your singly-linked list to a doubly-linked one. Therefore, you define the following DNode structure to represent a node in your doubly-linked list: struct DNode{ int value; DNode* next; DNode* prev; }; (a) (15%) Use any pseudo-code to describe an algorithm that takes a singly-linked list (i.e. a Node* head entry) and returns the new doubly-linked list (i.e. some DNode* entry) while deleting the memory allocated by the original singly-linked list. Your algorithm needs to be "on the fly". That is, it visits each node of the singly-linked list once and creates the corresponding node in the doubly-linked list immediately. (b) (10%) Modify your pseudo-code above to describe an algorithm that takes the same input but returns the new doubly-linked list in reverse order. Your algorithm still needs to be "on the fly". (Hint: You only need to change very few lines.) (4) (25%, **) Suppose you have two nonempty stacks S and T and a deque D. Use any pseudo-code to describe an algorithm that passes through the elements within S and T once, and uses D so that S contains all the elements of T below all of its original elements, with both sets of elements still in their original order. (5) (25%, **) The financial team in your company finds an old calculator implemented with the LISP language, which is based on prefix expressions of binary operators of the form ( - ( * 7 ( + 3 5 ) ) 1 ) But the team wants to evaluate the expression quickly, and therefore they want to transform the expression to its post-fix form. For simplicity of this problem, let's assume that there are parentheses in the expressions (which is true for LISP but not necessary in general). Use any pseudo-code to describe an algorithm transforms a valid prefix expression to a corresponding post-fix one (without parentheses). For instance, the post-fix expression that corresponds to the one above is 7 3 5 + * 1 - Your algorithm is only allowed to use one stack with size larger than the length of the prefix expression, and at most O(1) of additional memory. (Hint: Think about parentheses matching.) ┌─────────────────────────────────────┐ │For the following two problems, you can only use this definition: │ │Let f, g be functions from non-negative real numbers to non-negative real │ │numbers. We say f(n) = O(g(n)) iff there is a real constant c > 0 and an │ │real constant n_0 ≧ 1 such that f(n) ≦ cg(n) for n ≧ n_0. │ │Note that we use real numbers here for the simplicity of your proof below │ │(so you don't need to have floors and ceilings running in your proof). │ └─────────────────────────────────────┘ (6) (25%, **) Using the fact that log_e(n) ≦ n - 1 for all n ≧ 1, prove the following statement: "For any k > 0, log_2(m) = O(m^k)." (7) (25%, ***) Generalize your proof above to prove the following statement (or if you think the statement is wrong, disprove it): "Assume that f(n) = O(g(n)) and h(n) is a function from non-negative real numbers to non-negative real numbers. In addition, assume that ● h(n) is monotonically increasing: h(n') ≧ h(n) for all n' ≧ n ● The inverse function h^(-1)(m) of h exists for any m > 0 Then, f(h(n)) = O(g(h(n)))." (Hint: If this is true, let f(n) = log_2(n), g(n) = n, and h(n) = n^k and you can "easily" prove the statement in the previous problem with a few more lines.) (8) (25%, ***) For the binary max-heap with integer keys that is represented with an array and satisfies the heap properties: ● The nodes form a complete binary tree ● The node with the largest key in each sub-tree is at the root of the sub-tree (hence, the node with the overall largest key is at the root of the full tree.) (a) (5%) Student C thought that this property is true: "The node with the (2^k)-th largest key of the max-heap is within the first 2^(k+1) - 1 elements of the array." Show a heap without this property and thus prove student C wrong. (b) (10%) For a set of distinct integers, define the rank of an integer i be the total number of integers that are in the set and are not smaller than i. For instance, the largest number within the set is of rank 1, and the 2nd-largest number is of rank 2, and so on. Now, consider a set of all the elements within a binary max-heap that contains distinct integers. When the heap is of size 2^k - 1, prove that for a node at the third position of the array (the right child of the root), it is at least of rank 2 and at most of rank 2^(k-1) + 1. (c) (10%) Now, consider a binary max-heap that contains distinct integers and is of an arbitrary size ≧ 3, use any pseudo code to describe an algorithm that computes the rank of the node at the third position of the array. Your algorithm should run in O(r) time, where r is the rank of the node. (1126) (Bonus 10%, ???) We discussed about the difference between selection sort and heap sort, where it appears that the only change comes from replacing the linear search of the unordered batch (that can be either an array, a linked list, or almost anything) to a heap represented with an array. Given that the time complexity of heap sort, O(N logN) for N elements, is supposedly better than the time complexity of selection sort, O(N^2), why would anyone wants to use selection sort? Make some wild guesses and convince the TAs of some scenarios that is worth considering selection sort instead of heap sort. (Hint: Imagine that your boss, who is not of CS major, asks you this question. The TAs will grade qualitatively on whether you can convince your boss in a reasonable manner.) --



※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 61.228.165.134
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/NTU-Exam/M.1481355280.A.EF1.html
1F:→ rod24574575 : 已收资讯系! 12/10 15:37







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Tech_Job站内搜寻

TOP