1、软件学院数据结构与算法分析期末试题B四川大学期末考试试题(2007-2008学年第1学期)课程号: 课程名称: 数据结构与算法分析(B卷) 任课教师: 适用专业年级: 06级软件工程 学号: 姓名: 考试须知四川大学学生参加由学校组织或由学校承办的各级各类考试,必须严格执行四川大学考试工作管理办法和四川大学考场规则。有考试违纪作弊行为的,一律按照四川大学学生考试违纪作弊处罚条例进行处理。四川大学各级各类考试的监考人员,必须严格执行四川大学考试工作管理办法、四川大学考场规则和四川大学监考人员职责。有违反学校有关规定的,严格按照四川大学教学事故认定及处理办法进行处理。题 号1234567卷面成绩得
2、 分20101015151515阅卷教师阅卷时间1. Single-Choice questions(each question 2 scores, total 20 scores)(1)The primary purpose of most computer programs is a) to perform a mathematical calculation. b) to store and retrieve information. c) to sort a collection of records. d) all of the above.(2)Assume that P cont
3、ains n elements. The number of sets in the powerset of P isa) n b) n2c) 2n d) 2n - 1e) 2n + 1(3)Pick the growth rate that corresponds to the most efficientalgorithm as n gets large:a) 5n b) 20 log nc) 2n2 d) 2n(4)A sequence has the following properties:a) May have duplicates, element have a position
4、.b) May have duplicates, elements do not have a position.c) May not have duplicates, elements have a position.d) May not have duplicates, elements do not have a position.(5)The correct traversal to use on a BST to visit the nodes in sorted order is:a) Preorder traversal. b) Inorder traversal.c) Post
5、order traversal.(6)When sorting n records, Insertion sort has best-case cost:a) O(log n). b) O(n).c) O(n log n). d) O(n2)e) O(n!) f) None of the above.(7)For a list of length n, the linked-list implementations prev function requires worst-case time:a) O(1).b) O(log n).c) O(n).d) O(n2).(8)The easiest
6、 way to represent a general tree is to:a) convert to a list. b) convert to a binary tree.c) convert to a graph.(9)Depth-first search is best implemented using:a) A stack or recursion. b) A queue.c) A tree.(10)For set P, the notation |P| indicatesa) The number of elements in P. b) The inverse of P.c)
7、 The powerset of P. d) None of the above.2(10 scores)Write a series of C+ statements that uses the List ADT as follows to create a list capable of holding twenty elements and which actually stores the list with following configuration:solution:list L1(20);L1.append(6);L1.append(28);L1.append(18);L1.
8、append(8);L1.append(9);L1.next();L1.next();/ List abstract classtemplate class List public: / Reinitialize the list. The client is responsible for / reclaiming the storage used by the list elements. virtual void clear() = 0; / Insert an element at the front of the right partition. / Return true if s
9、uccessful, false if the list is full. virtual bool insert(const Elem&) = 0; / Append an element at the end of the right partition. / Return true if successful, false if the list is full. virtual bool append(const Elem&) = 0; / Remove the first element of right partition. Return / true if successful,
10、 false if right partition is empty. / The element removed is returned in the parameter. virtual bool remove(Elem&) = 0; / Place fence at list start, making left partition empty virtual void setStart() = 0; / Place fence at list end, making right partition empty virtual void setEnd() = 0; / Move fenc
11、e one step left; no change if already at start virtual void prev() = 0; / Move fence one step right; no change if already at end virtual void next() = 0; / Return length of left partition virtual int leftLength() const = 0; / Return length of right partition virtual int rightLength() const = 0; / If
12、 pos or more elements are in the list, set the size / of left partition to pos and return true. Otherwise, / do nothing and return false. virtual bool setPos(int pos) = 0; / Return in first parameter the first element of the / right partition. Return true if successful, false / if the right partitio
13、n is empty. virtual bool getValue(Elem&) const = 0; / Print the contents of the list virtual void print() const = 0;3(10 scores)Build the Huffman coding tree and determine the codes for the following set of letters and weights:a b c d e.1 3 5 7 11solution:Huffman coding tree as follows:Here are the
14、final codes.a 11111b 1110c 110d 10e 04(15 scores)What are the minimum and maximum number of elememts in a heap of height h ?solution:The minimum number of elements is contained in the heap with a single nodeat depth h - 1, for a total of 2h-1 nodes.The maximum number of elements is contained in the
15、heap that has completely filled up level h - 1, for a total of 2h - 1 nodes.5(15 scores)The Bubble Sort implentation has the following inner for loop:for (int j = n 1; j i; j-)Consider the effect of replacing this with the following staement:for (int j = n 1; j 0; j-)Would the new implementation wor
16、k correctly? Would the change affect the asymptotic complexity of the algorithm? How would the change affect the running time of the algorithm?solution:The revised algorithm will work correctly, and its asymptotic complexity willremain(n2). However, it will do about twice as many comparisons, since
17、it will compare adjacent elements within the portion of the list already known to be sorted. These additional comparisons are unproductive.6(15 scores)/ Binary tree node abstract classtemplate class BinNode public: / Return the nodes element virtual Elem& val() = 0; / Set the nodes element virtual v
18、oid setVal(const Elem&) = 0; / Return the nodes left child virtual BinNode* left() const = 0; / Set the nodes left child virtual void setLeft(BinNode*) = 0; / Return the nodes right child virtual BinNode* right() const = 0; / Set the nodes right child virtual void setRight(BinNode*) = 0; / Return tr
19、ue iff the node is a leaf virtual bool isLeaf() = 0;Write a recursive function that returns a count of the number of leaf nodes in a binary true.solution: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();7(15 scores)List the order in which the edges of the following graph are visited when running Kruskals MST algorithm. Show the final MST.solution:(1, 6) (6, 5) (2, 3) (2, 4) (4, 6).Alternatively, (1, 6) (6, 5) (2, 3) (2, 4) (1, 2).etc
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1