软件学院数据结构与算法分析期末试题(2006级A)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
四川大学期末考试试题
(2007-2008学年第1学期)
课程号:课程名称:数据结构与算法分析(A卷)任课教师:适用专业年级:06级软件工程学号:姓名:
(1)An algorithm must be or do all of the following EXCEPT:
a) correct
b) composed of concrete steps
c) ambiguous
d) composed of a finite number of steps
(2)For set P, the notation |P| indicates
a) The number of elements in P. b) The inverse of P.
c) The powerset of P. d) None of the above.
(3)Pick the quadratic growth rate.
a) 5n b) 20 log n
c) 2n^2 d) 2^n
(4)
(5)Huffman coding provides the optimal coding when:
a) The messages are in English.
b) The messages are binary numbers.
c) The frequency of occurrence for a letter is independent of its context within the message.
d) Never.
(6)A sorting algorithm is stable if it:
a) Works for all inputs.
b) Does not change the relative ordering of records with identical key values.
c) Always sorts in the same amount of time (within a constant factor) for a given input size.
(7)Here is a series of C++ statements using the list ADT in the book.
L1.append(10);
L1.append(20);
L1.append(15);
If these statements are applied to an empty list, the result will look
like:
a) < 10 20 15 >
b) < | 10 20 15 >
c) < 10 20 15 | >
d) < 15 20 10 >
e) < | 15 20 10 >
f) < 15 20 10 | >
(8)An entry-sequenced file stores records sorted by:
a) Primary key value. b) Secondary key value.
c) Order of arrival. d) Frequency of access.
(9)Breadth-first search is best implemented using:
a) A stack or recursion. b) A queue.
c) A tree.
(10)A recurrence relation is often used to model programs with
a) for loops. b) branch control like "if" statements.
c) recursive calls. d) None of the above.
2.(10 scores)
Assume a list has the following configuration:
<| 6, 28, 16, 8, 9>
Write a series of C++ statements using the List ADT as follows to delete the element with value 16.
// List abstract class
template <class Elem> 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 successful, 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, 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 fence 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 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 partition is empty.
virtual bool getV alue(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 e i o u.
1 3 5 7 8
4.(15 scores)
Show the max-heap that results from running buildHeap on the following values stored in an array:
10 5 12 3 2 1 8 7 9 4
5.(15 scores)
When implementation Insertion Sort, a binary search could be used to locate the position within the first i – 1 elememts of the array into which element i should be inserted. How would this affect the number of comparisons required? How would using such a binary search affect the asymptotic running time for Insertion Sort?
6.(15 scores)
// Binary tree node abstract class
template <class Elem> class BinNode {
public:
// Return the node's element
virtual Elem& val() = 0;
// Set the node's element
virtual void setV al(const Elem&) = 0;
// Return the node's left child
virtual BinNode* left() const = 0;
// Set the node's left child
virtual void setLeft(BinNode*) = 0;
// Return the node's right child
virtual BinNode* right() const = 0;
// Set the node's right child
virtual void setRight(BinNode*) = 0;
// Return true iff the node is a leaf
virtual bool isLeaf() = 0;
};
Write a recursive function that returns the height of a binary true..
template <class Elem>
int height(BinNode<Elem>* subroot)
{
if (subroot == NULL) return 0; // Empty subtree
return 1 + max(height(subroot->left()),
height(subroot->right()));
}
7.(15 scores)
List the order in which the edges of the following graph are visited when running Prim’s MST algorithm starting at V ertex 3. Show the final MST.。