数据结构与算法分析习题及参考答案
数据结构与算法复习题及参考答案

(B)10.最大容量为n的循环队列,队尾指针是rear,队头是front,则队空的条件是()。
A)(rear+1)%n==frontB)rear===frontC)rear+1==frontD)(rear-l)%n==front
(A)11.循环队列A[0..m-1]存放其元素值,用front和rear分别表示队头和队尾,则当前队列中的元素数是:
(√)11.对于不同的使用者,一个表结构既可以是栈,也可以是队列,也可以是线性表。
(√)12.栈是一种对所有插入、删除操作限于在表的一端进行的线性表,是一种后进先出型结构。
(√)13.两个栈共享一片连续存空间时,为提高存利用率,减少溢出机会,应把两个栈的栈底分别设在这片存空间的两端。
(×)14.二叉树的度为2。
(×)25.顺序表是一种有序的线性表。[任何数据结构才用顺序存储都叫顺序表]
(√)26.分配给顺序表的存单元地址必须是连续的。
(√)27.栈和队列具有相同的逻辑特性。[它们的逻辑结构都是线性表]
(√)28.树形结构中每个结点至多有一个前驱。
(×)29.在树形结构中,处于同一层上的各结点之间都存在兄弟关系。
复习题集
一判断题
(√)1.在决定选取何种存储结构时,一般不考虑各结点的值如何。
(√)2.抽象数据类型与计算机部表示和实现无关。
(×)3.线性表采用链式存储结构时,结点和结点部的存储空间可以是不连续的。
(×)4.链表的每个结点中都恰好包含一个指针。
(×)5.链表的删除算法很简单,因为当删除链中某个结点后,计算机会自动地将后续的各个单元向前移动。
(A)(rear-front+m)%m(B)rear-front+1(C)rear-front-1(D)rear-front
《数据结构与算法》课后习题答案

2.3 课后习题解答2.3.2 判断题1.线性表的逻辑顺序与存储顺序总是一致的。
〔×〕2.顺序存储的线性表可以按序号随机存取。
〔√〕3.顺序表的插入和删除操作不需要付出很大的时间代价,因为每次操作平均只有近一半的元素需要移动。
〔×〕4.线性表中的元素可以是各种各样的,但同一线性表中的数据元素具有一样的特性,因此属于同一数据对象。
〔√〕5.在线性表的顺序存储构造中,逻辑上相邻的两个元素在物理位置上并不一定相邻。
〔×〕6.在线性表的链式存储构造中,逻辑上相邻的元素在物理位置上不一定相邻。
〔√〕7.线性表的链式存储构造优于顺序存储构造。
〔×〕8.在线性表的顺序存储构造中,插入和删除时移动元素的个数与该元素的位置有关。
〔√〕9.线性表的链式存储构造是用一组任意的存储单元来存储线性表中数据元素的。
〔√〕10.在单链表中,要取得某个元素,只要知道该元素的指针即可,因此,单链表是随机存取的存储构造。
〔×〕11.静态链表既有顺序存储的优点,又有动态链表的优点。
所以它存取表中第i 个元素的时间与i 无关。
〔×〕12.线性表的特点是每个元素都有一个前驱和一个后继。
〔×〕2.3.3 算法设计题1.设线性表存放在向量A[arrsize] 的前 elenum 个分量中,且递增有序。
试写一算法,将x 插入到线性表的适当位置上,以保持线性表的有序性,并且分析算法的时间复杂度。
【提示】直接用题目中所给定的数据构造〔顺序存储的思想是用物理上的相邻表示逻辑上的相邻,不一定将向量和表示线性表长度的变量封装成一个构造体〕,因为是顺序存储,分配的存储空间是固定大小的,所以首先确定是否还有存储空间,假设有,那么根据原线性表中元素的有序性,来确定插入元素的插入位置,后面的元素为它让出位置,〔也可以从高低标端开始一边比拟,一边移位〕然后插入x ,最后修改表示表长的变量。
int insert (datatype A[],int *elenum,datatype x)/* 设 elenum 为表的最大下标*/ {if (*elenum==arrsize-1)return 0;/* 表已满,无法插入*/else {i=*elenum;while (i>=0 && A[i]>x)/* 边找位置边移动*/{A[i+1]=A[i];i--;}/* 插入成功 */A[i+1]=x;(*elenum)++;return 1;}}时间复杂度为O(n) 。
数据结构与算法分析习题与参考答案

大学《数据结构与算法分析》课程习题及参考答案模拟试卷一一、单选题(每题 2 分,共20分)1.以下数据结构中哪一个是线性结构?( )A. 有向图B. 队列C. 线索二叉树D. B树2.在一个单链表HL中,若要在当前由指针p指向的结点后面插入一个由q指向的结点,则执行如下( )语句序列。
A. p=q; p->next=q;B. p->next=q; q->next=p;C. p->next=q->next; p=q;D. q->next=p->next; p->next=q;3.以下哪一个不是队列的基本运算?()A. 在队列第i个元素之后插入一个元素B. 从队头删除一个元素C. 判断一个队列是否为空D.读取队头元素的值4.字符A、B、C依次进入一个栈,按出栈的先后顺序组成不同的字符串,至多可以组成( )个不同的字符串?A.14B.5C.6D.85.由权值分别为3,8,6,2的叶子生成一棵哈夫曼树,它的带权路径长度为( )。
以下6-8题基于图1。
6.该二叉树结点的前序遍历的序列为( )。
A.E、G、F、A、C、D、BB.E、A、G、C、F、B、DC.E、A、C、B、D、G、FD.E、G、A、C、D、F、B7.该二叉树结点的中序遍历的序列为( )。
A. A、B、C、D、E、G、FB. E、A、G、C、F、B、DC. E、A、C、B、D、G、FE.B、D、C、A、F、G、E8.该二叉树的按层遍历的序列为( )。
A.E、G、F、A、C、D、B B. E、A、C、B、D、G、FC. E、A、G、C、F、B、DD. E、G、A、C、D、F、B9.下面关于图的存储的叙述中正确的是( )。
A.用邻接表法存储图,占用的存储空间大小只与图中边数有关,而与结点个数无关B.用邻接表法存储图,占用的存储空间大小与图中边数和结点个数都有关C. 用邻接矩阵法存储图,占用的存储空间大小与图中结点个数和边数都有关D.用邻接矩阵法存储图,占用的存储空间大小只与图中边数有关,而与结点个数无关10.设有关键码序列(q,g,m,z,a,n,p,x,h),下面哪一个序列是从上述序列出发建堆的结果?( )A. a,g,h,m,n,p,q,x,zB. a,g,m,h,q,n,p,x,zC. g,m,q,a,n,p,x,h,zD. h,g,m,p,a,n,q,x,z二、填空题(每空1分,共26分)1.数据的物理结构被分为_________、________、__________和___________四种。
数据结构与算法作业答案

《数据结构与算法》作业参考答案说明:1、题号形式: 每题都以【sn,cha,sec】开头,sn表明本题的题目序号,每道题都有唯一的序号;cha表示内容所在的章;sec表示内容所在的节。
如:【17,2,1】表示序号17的题来自第2章第1节。
2、题型:1) 选择题:序号1-180题2) 是非题:序号181-220题3) 分析计算作图题:序号221-250题(选自《数据结构题集》—严蔚敏等编)3、内容取舍:根据本学期上课课件中的内容,未上课章节的练习可舍弃。
4、必做题或选做题:是非题和选择题(序号1-220)只要在上过课的章节中都是必做题,分析计算作图题(序号221-250)在每题后标出是必做题还是选做题,其中16个必做题14个选做题。
1) 选择题:序号1-180题序号参考答案1 BD2 D3 C4 A5 C6 D7 C8 D9 B10 D11 B12 D13 C14 C15 D16 A17 A18 A19 A20 A21 A22 D24 A25 B26 C27 C28 C29 B30 B31 B32 B33 B34 A35 A36 C37 D38 D39 B40 C41 B42 A43 D44 B45 D46 B47 C48 C49 B50 B51 A52 D53 D54 C55 A56 A57 C58 C59 C60 B61 A62 D64 B65 C66 B67 D68 D69 B70 A71 C72 B73 A74 D75 D76 B77 C78 C79 D80 B81 D82 C83 B84 A85 B86 C87 D88 B89 B90 B91 D92 C93 D94 A95 C96 C97 B98 A99 C 100 C 101 A 102 D 103 C105 C 106 D 107 C 108 B 109 B 110 D111 A 112 B 113 A 114 B 115 C 116 D 117 B 118 C 119 A 120 B121 A 122 A 123 C 124 C 125 B 126 B 127 B 128 C 129 C 130 B131 A 132 D 133 AB 134 D 135 C 136 C 137 B 138 A 139 B 140 B141 D 142 A 143 B145 A146 A147 C148 B149 C150 A151 B152 B153 C154 B155 A156 C157 C158 C159 A160 B161 C162 A163 A164 D165 B166 C167 D168 C169 C170 B171 B172 C173 B174 D175 C176 D177 B178 A179 A180 B2) 是非题:序号181-220题181 T182 T183 F184 T185 T186 F187 T188 T189 T190 T191 F192 T193 T194 F195 F196 T197 F198 T199 F200 T201 T202 T203 F204 T205 T206 F207 T208 T209 F210 T211 F212 T213 T214 F215 T216 T217 F218 T219 T220 F3) 分析计算作图题:序号221-250(选自《数据结构题集》—严蔚敏等编)【221,1,4】(选自《数据结构题集》1.8,选做题)设n为正整数,试确定下列各段程序中前置以记号@的语句的频度(语句的频度指的是该语句重复执行的次数)。
《数据结构与算法分析》(C++第二版)【美】Clifford A.Shaffer著 课后习题答案 二

《数据结构与算法分析》(C++第二版)【美】Clifford A.Shaffer著课后习题答案二5Binary Trees5.1 Consider a non-full binary tree. By definition, this tree must have some internalnode X with only one non-empty child. If we modify the tree to removeX, replacing it with its child, the modified tree will have a higher fraction ofnon-empty nodes since one non-empty node and one empty node have been removed.5.2 Use as the base case the tree of one leaf node. The number of degree-2 nodesis 0, and the number of leaves is 1. Thus, the theorem holds.For the induction hypothesis, assume the theorem is true for any tree withn − 1 nodes.For the induction step, consider a tree T with n nodes. Remove from the treeany leaf node, and call the resulting tree T. By the induction hypothesis, Thas one more leaf node than it has nodes of degree 2.Now, restore the leaf node that was removed to form T. There are twopossible cases.(1) If this leaf node is the only child of its parent in T, then the number ofnodes of degree 2 has not changed, nor has the number of leaf nodes. Thus,the theorem holds.(2) If this leaf node is the child of a node in T with degree 2, then that nodehas degree 1 in T. Thus, by restoring the leaf node we are adding one newleaf node and one new node of degree 2. Thus, the theorem holds.By mathematical induction, the theorem is correct.32335.3 Base Case: For the tree of one leaf node, I = 0, E = 0, n = 0, so thetheorem holds.Induction Hypothesis: The theorem holds for the full binary tree containingn internal nodes.Induction Step: Take an arbitrary tree (call it T) of n internal nodes. Selectsome internal node x from T that has two leaves, and remove those twoleaves. Call the resulting tree T’. Tree T’ is full and has n−1 internal nodes,so by the Induction Hypothesis E = I + 2(n − 1).Call the depth of node x as d. Restore the two children of x, each at leveld+1. We have nowadded d to I since x is now once again an internal node.We have now added 2(d + 1) − d = d + 2 to E since we added the two leafnodes, but lost the contribution of x to E. Thus, if before the addition we had E = I + 2(n − 1) (by the induction hypothesis), then after the addition we have E + d = I + d + 2 + 2(n − 1) or E = I + 2n which is correct. Thus,by the principle of mathematical induction, the theorem is correct.5.4 (a) template <class Elem>void inorder(BinNode<Elem>* subroot) {if (subroot == NULL) return; // Empty, do nothingpreorder(subroot->left());visit(subroot); // Perform desired actionpreorder(subroot->right());}(b) template <class Elem>void postorder(BinNode<Elem>* subroot) {if (subroot == NULL) return; // Empty, do nothingpreorder(subroot->left());preorder(subroot->right());visit(subroot); // Perform desired action}5.5 The key is to search both subtrees, as necessary.template <class Key, class Elem, class KEComp>bool search(BinNode<Elem>* subroot, Key K);if (subroot == NULL) return false;if (subroot->value() == K) return true;if (search(subroot->right())) return true;return search(subroot->left());}34 Chap. 5 Binary Trees5.6 The key is to use a queue to store subtrees to be processed.template <class Elem>void level(BinNode<Elem>* subroot) {AQueue<BinNode<Elem>*> Q;Q.enqueue(subroot);while(!Q.isEmpty()) {BinNode<Elem>* temp;Q.dequeue(temp);if(temp != NULL) {Print(temp);Q.enqueue(temp->left());Q.enqueue(temp->right());}}}5.7 template <class Elem>int height(BinNode<Elem>* subroot) {if (subroot == NULL) return 0; // Empty subtreereturn 1 + max(height(subroot->left()),height(subroot->right()));}5.8 template <class Elem>int count(BinNode<Elem>* subroot) {if (subroot == NULL) return 0; // Empty subtreeif (subroot->isLeaf()) return 1; // A leafreturn 1 + count(subroot->left()) +count(subroot->right());}5.9 (a) Since every node stores 4 bytes of data and 12 bytes of pointers, the overhead fraction is 12/16 = 75%.(b) Since every node stores 16 bytes of data and 8 bytes of pointers, the overhead fraction is 8/24 ≈ 33%.(c) Leaf nodes store 8 bytes of data and 4 bytes of pointers; internal nodesstore 8 bytes of data and 12 bytes of pointers. Since the nodes havedifferent sizes, the total space needed for internal nodes is not the sameas for leaf nodes. Students must be careful to do the calculation correctly,taking the weighting into account. The correct formula looks asfollows, given that there are x internal nodes and x leaf nodes.4x + 12x12x + 20x= 16/32 = 50%.(d) Leaf nodes store 4 bytes of data; internal nodes store 4 bytes of pointers. The formula looks as follows, given that there are x internal nodes and35x leaf nodes:4x4x + 4x= 4/8 = 50%.5.10 If equal valued nodes were allowed to appear in either subtree, then during a search for all nodes of a given value, whenever we encounter a node of that value the search would be required to search in both directions.5.11 This tree is identical to the tree of Figure 5.20(a), except that a node with value 5 will be added as the right child of the node with value 2.5.12 This tree is identical to the tree of Figure 5.20(b), except that the value 24 replaces the value 7, and the leaf node that originally contained 24 is removed from the tree.5.13 template <class Key, class Elem, class KEComp>int smallcount(BinNode<Elem>* root, Key K);if (root == NULL) return 0;if (KEComp.gt(root->value(), K))return smallcount(root->leftchild(), K);elsereturn smallcount(root->leftchild(), K) +smallcount(root->rightchild(), K) + 1;5.14 template <class Key, class Elem, class KEComp>void printRange(BinNode<Elem>* root, int low,int high) {if (root == NULL) return;if (KEComp.lt(high, root->val()) // all to leftprintRange(root->left(), low, high);else if (KEComp.gt(low, root->val())) // all to rightprintRange(root->right(), low, high);else { // Must process both childrenprintRange(root->left(), low, high);PRINT(root->value());printRange(root->right(), low, high);}}5.15 The minimum number of elements is contained in the heap with a single node at depth h − 1, for a total of 2h−1 nodes.The maximum number of elements is contained in the heap that has completely filled up level h − 1, for a total of 2h − 1 nodes.5.16 The largest element could be at any leaf node.5.17 The corresponding array will be in the following order (equivalent to level order for the heap):12 9 10 5 4 1 8 7 3 236 Chap. 5 Binary Trees5.18 (a) The array will take on the following order:6 5 3 4 2 1The value 7 will be at the end of the array.(b) The array will take on the following order:7 4 6 3 2 1The value 5 will be at the end of the array.5.19 // Min-heap classtemplate <class Elem, class Comp> class minheap {private:Elem* Heap; // Pointer to the heap arrayint size; // Maximum size of the heapint n; // # of elements now in the heapvoid siftdown(int); // Put element in correct placepublic:minheap(Elem* h, int num, int max) // Constructor{ Heap = h; n = num; size = max; buildHeap(); }int heapsize() const // Return current size{ return n; }bool isLeaf(int pos) const // TRUE if pos a leaf{ return (pos >= n/2) && (pos < n); }int leftchild(int pos) const{ return 2*pos + 1; } // Return leftchild posint rightchild(int pos) const{ return 2*pos + 2; } // Return rightchild posint parent(int pos) const // Return parent position { return (pos-1)/2; }bool insert(const Elem&); // Insert value into heap bool removemin(Elem&); // Remove maximum value bool remove(int, Elem&); // Remove from given pos void buildHeap() // Heapify contents{ for (int i=n/2-1; i>=0; i--) siftdown(i); }};template <class Elem, class Comp>void minheap<Elem, Comp>::siftdown(int pos) { while (!isLeaf(pos)) { // Stop if pos is a leafint j = leftchild(pos); int rc = rightchild(pos);if ((rc < n) && Comp::gt(Heap[j], Heap[rc]))j = rc; // Set j to lesser child’s valueif (!Comp::gt(Heap[pos], Heap[j])) return; // Done37swap(Heap, pos, j);pos = j; // Move down}}template <class Elem, class Comp>bool minheap<Elem, Comp>::insert(const Elem& val) { if (n >= size) return false; // Heap is fullint curr = n++;Heap[curr] = val; // Start at end of heap// Now sift up until curr’s parent < currwhile ((curr!=0) &&(Comp::lt(Heap[curr], Heap[parent(curr)]))) {swap(Heap, curr, parent(curr));curr = parent(curr);}return true;}template <class Elem, class Comp>bool minheap<Elem, Comp>::removemin(Elem& it) { if (n == 0) return false; // Heap is emptyswap(Heap, 0, --n); // Swap max with last valueif (n != 0) siftdown(0); // Siftdown new root valit = Heap[n]; // Return deleted valuereturn true;}38 Chap. 5 Binary Trees// Remove value at specified positiontemplate <class Elem, class Comp>bool minheap<Elem, Comp>::remove(int pos, Elem& it) {if ((pos < 0) || (pos >= n)) return false; // Bad posswap(Heap, pos, --n); // Swap with last valuewhile ((pos != 0) &&(Comp::lt(Heap[pos], Heap[parent(pos)])))swap(Heap, pos, parent(pos)); // Push up if largesiftdown(pos); // Push down if small keyit = Heap[n];return true;}5.20 Note that this summation is similar to Equation 2.5. To solve the summation requires the shifting technique from Chapter 14, so this problem may be too advanced for many students at this time. Note that 2f(n) − f(n) = f(n),but also that:2f(n) − f(n) = n(24+48+616+ ··· +2(log n − 1)n) −n(14+28+316+ ··· +log n − 1n)logn−1i=112i− log n − 1n)= n(1 − 1n− log n − 1n)= n − log n.5.21 Here are the final codes, rather than a picture.l 00h 010i 011e 1000f 1001j 101d 11000a 1100100b 1100101c 110011g 1101k 11139The average code length is 3.234455.22 The set of sixteen characters with equal weight will create a Huffman coding tree that is complete with 16 leaf nodes all at depth 4. Thus, the average code length will be 4 bits. This is identical to the fixed length code. Thus, in this situation, the Huffman coding tree saves no space (and costs no space).5.23 (a) By the prefix property, there can be no character with codes 0, 00, or 001x where “x” stands for any binary string.(b) There must be at least one code with each form 1x, 01x, 000x where“x” could be any binary string (including the empty string).5.24 (a) Q and Z are at level 5, so any string of length n containing only Q’s and Z’s requires 5n bits.(b) O and E are at level 2, so any string of length n containing only O’s and E’s requires 2n bits.(c) The weighted average is5 ∗ 5 + 10 ∗ 4 + 35 ∗ 3 + 50 ∗ 2100bits per character5.25 This is a straightforward modification.// Build a Huffman tree from minheap h1template <class Elem>HuffTree<Elem>*buildHuff(minheap<HuffTree<Elem>*,HHCompare<Elem> >* hl) {HuffTree<Elem> *temp1, *temp2, *temp3;while(h1->heapsize() > 1) { // While at least 2 itemshl->removemin(temp1); // Pull first two treeshl->removemin(temp2); // off the heaptemp3 = new HuffTree<Elem>(temp1, temp2);hl->insert(temp3); // Put the new tree back on listdelete temp1; // Must delete the remnantsdelete temp2; // of the trees we created}return temp3;}6General Trees6.1 The following algorithm is linear on the size of the two trees. // Return TRUE iff t1 and t2 are roots of identical// general treestemplate <class Elem>bool Compare(GTNode<Elem>* t1, GTNode<Elem>* t2) { GTNode<Elem> *c1, *c2;if (((t1 == NULL) && (t2 != NULL)) ||((t2 == NULL) && (t1 != NULL)))return false;if ((t1 == NULL) && (t2 == NULL)) return true;if (t1->val() != t2->val()) return false;c1 = t1->leftmost_child();c2 = t2->leftmost_child();while(!((c1 == NULL) && (c2 == NULL))) {if (!Compare(c1, c2)) return false;if (c1 != NULL) c1 = c1->right_sibling();if (c2 != NULL) c2 = c2->right_sibling();}}6.2 The following algorithm is Θ(n2).// Return true iff t1 and t2 are roots of identical// binary treestemplate <class Elem>bool Compare2(BinNode<Elem>* t1, BinNode<Elem* t2) { BinNode<Elem> *c1, *c2;if (((t1 == NULL) && (t2 != NULL)) ||((t2 == NULL) && (t1 != NULL)))return false;if ((t1 == NULL) && (t2 == NULL)) return true;4041if (t1->val() != t2->val()) return false;if (Compare2(t1->leftchild(), t2->leftchild())if (Compare2(t1->rightchild(), t2->rightchild())return true;if (Compare2(t1->leftchild(), t2->rightchild())if (Compare2(t1->rightchild(), t2->leftchild))return true;return false;}6.3 template <class Elem> // Print, postorder traversalvoid postprint(GTNode<Elem>* subroot) {for (GTNode<Elem>* temp = subroot->leftmost_child();temp != NULL; temp = temp->right_sibling())postprint(temp);if (subroot->isLeaf()) cout << "Leaf: ";else cout << "Internal: ";cout << subroot->value() << "\n";}6.4 template <class Elem> // Count the number of nodesint gencount(GTNode<Elem>* subroot) {if (subroot == NULL) return 0int count = 1;GTNode<Elem>* temp = rt->leftmost_child();while (temp != NULL) {count += gencount(temp);temp = temp->right_sibling();}return count;}6.5 The Weighted Union Rule requires that when two parent-pointer trees are merged, the smaller one’s root becomes a child of the larger one’s root. Thus, we need to keep track of the number of nodes in a tree. To do so, modify the node array to store an integer value with each node. Initially, each node isin its own tree, so the weights for each node begin as 1. Whenever we wishto merge two trees, check the weights of the roots to determine which has more nodes. Then, add to the weight of the final root the weight of the new subtree.6.60 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15-1 0 0 0 0 0 0 6 0 0 0 9 0 0 12 06.7 The resulting tree should have the following structure:42 Chap. 6 General TreesNode 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15Parent 4 4 4 4 -1 4 4 0 0 4 9 9 9 12 9 -16.8 For eight nodes labeled 0 through 7, use the following series of equivalences: (0, 1) (2, 3) (4, 5) (6, 7) (4 6) (0, 2) (4 0)This requires checking fourteen parent pointers (two for each equivalence),but none are actually followed since these are all roots. It is possible todouble the number of parent pointers checked by choosing direct children ofroots in each case.6.9 For the “lists of Children” representation, every node stores a data value and a pointer to its list of children. Further, every child (every node except the root)has a record associated with it containing an index and a pointer. Indicatingthe size of the data value as D, the size of a pointer as P and the size of anindex as I, the overhead fraction is3P + ID + 3P + I.For the “Left Child/Right Sibling” representation, every node stores three pointers and a data value, for an overhead fraction of3PD + 3P.The first linked representation of Section 6.3.3 stores with each node a datavalue and a size field (denoted by S). Each child (every node except the root)also has a pointer pointing to it. The overhead fraction is thusS + PD + S + Pmaking it quite efficient.The second linked representation of Section 6.3.3 stores with each node adata value and a pointer to the list of children. Each child (every node exceptthe root) has two additional pointers associated with it to indicate its placeon the parent’s linked list. Thus, the overhead fraction is3PD + 3P.6.10 template <class Elem>BinNode<Elem>* convert(GTNode<Elem>* genroot) {if (genroot == NULL) return NULL;43GTNode<Elem>* gtemp = genroot->leftmost_child();btemp = new BinNode(genroot->val(), convert(gtemp),convert(genroot->right_sibling()));}6.11 • Parent(r) = (r − 1)/k if 0 < r < n.• Ith child(r) = kr + I if kr +I < n.• Left sibling(r) = r − 1 if r mod k = 1 0 < r < n.• Right sibling(r) = r + 1 if r mod k = 0 and r + 1 < n.6.12 (a) The overhead fraction is4(k + 1)4 + 4(k + 1).(b) The overhead fraction is4k16 + 4k.(c) The overhead fraction is4(k + 2)16 + 4(k + 2).(d) The overhead fraction is2k2k + 4.6.13 Base Case: The number of leaves in a non-empty tree of 0 internal nodes is (K − 1)0 + 1 = 1. Thus, the theorem is correct in the base case.Induction Hypothesis: Assume that the theorem is correct for any full Karytree containing n internal nodes.Induction Step: Add K children to an arbitrary leaf node of the tree withn internal nodes. This new tree now has 1 more internal node, and K − 1more leaf nodes, so theorem still holds. Thus, the theorem is correct, by the principle of Mathematical Induction.6.14 (a) CA/BG///FEDD///H/I//(b) CA/BG/FED/H/I6.15 X|P-----| | |C Q R---| |V M44 Chap. 6 General Trees6.16 (a) // Use a helper function with a pass-by-reference// variable to indicate current position in the// node list.template <class Elem>BinNode<Elem>* convert(char* inlist) {int curr = 0;return converthelp(inlist, curr);}// As converthelp processes the node list, curr is// incremented appropriately.template <class Elem>BinNode<Elem>* converthelp(char* inlist,int& curr) {if (inlist[curr] == ’/’) {curr++;return NULL;}BinNode<Elem>* temp = new BinNode(inlist[curr++], NULL, NULL);temp->left = converthelp(inlist, curr);temp->right = converthelp(inlist, curr);return temp;}(b) // Use a helper function with a pass-by-reference // variable to indicate current position in the// node list.template <class Elem>BinNode<Elem>* convert(char* inlist) {int curr = 0;return converthelp(inlist, curr);}// As converthelp processes the node list, curr is// incremented appropriately.template <class Elem>BinNode<Elem>* converthelp(char* inlist,int& curr) {if (inlist[curr] == ’/’) {curr++;return NULL;}BinNode<Elem>* temp =new BinNode<Elem>(inlist[curr++], NULL, NULL);if (inlist[curr] == ’\’’) return temp;45curr++ // Eat the internal node mark.temp->left = converthelp(inlist, curr);temp->right = converthelp(inlist, curr);return temp;}(c) // Use a helper function with a pass-by-reference// variable to indicate current position in the// node list.template <class Elem>GTNode<Elem>* convert(char* inlist) {int curr = 0;return converthelp(inlist, curr);}// As converthelp processes the node list, curr is// incremented appropriately.template <class Elem>GTNode<Elem>* converthelp(char* inlist,int& curr) {if (inlist[curr] == ’)’) {curr++;return NULL;}GTNode<Elem>* temp =new GTNode<Elem>(inlist[curr++]);if (curr == ’)’) {temp->insert_first(NULL);return temp;}temp->insert_first(converthelp(inlist, curr));while (curr != ’)’)temp->insert_next(converthelp(inlist, curr));curr++;return temp;}6.17 The Huffman tree is a full binary tree. To decode, we do not need to know the weights of nodes, only the letter values stored in the leaf nodes. Thus, we can use a coding much like that of Equation 6.2, storing only a bit mark for internal nodes, and a bit mark and letter value for leaf nodes.7Internal Sorting7.1 Base Case: For the list of one element, the double loop is not executed and the list is not processed. Thus, the list of one element remains unaltered and is sorted.Induction Hypothesis: Assume that the list of n elements is sorted correctlyby Insertion Sort.Induction Step: The list of n + 1 elements is processed by first sorting thetop n elements. By the induction hypothesis, this is done correctly. The final pass of the outer for loop will process the last element (call it X). This isdone by the inner for loop, which moves X up the list until a value smallerthan that of X is encountered. At this point, X has been properly insertedinto the sorted list, leaving the entire collection of n + 1 elements correctly sorted. Thus, by the principle of Mathematical Induction, the theorem is correct.7.2 void StackSort(AStack<int>& IN) {AStack<int> Temp1, Temp2;while (!IN.isEmpty()) // Transfer to another stackTemp1.push(IN.pop());IN.push(Temp1.pop()); // Put back one elementwhile (!Temp1.isEmpty()) { // Process rest of elemswhile (IN.top() > Temp1.top()) // Find elem’s placeTemp2.push(IN.pop());IN.push(Temp1.pop()); // Put the element inwhile (!Temp2.isEmpty()) // Put the rest backIN.push(Temp2.pop());}}46477.3 The revised algorithm will work correctly, and its asymptotic complexity will remain Θ(n2). However, it will do about twice as many comparisons, since it will compare adjacent elements within the portion of the list already knownto be sorted. These additional comparisons are unproductive.7.4 While binary search will find the proper place to locate the next element, it will still be necessary to move the intervening elements down one position in the array. This requires the same number of operations as a sequential search. However, it does reduce the number of element/element comparisons, and may be somewhat faster by a constant factor since shifting several elements may be more efficient than an equal number of swap operations.7.5 (a) template <class Elem, class Comp>void selsort(Elem A[], int n) { // Selection Sortfor (int i=0; i<n-1; i++) { // Select i’th recordint lowindex = i; // Remember its indexfor (int j=n-1; j>i; j--) // Find least valueif (Comp::lt(A[j], A[lowindex]))lowindex = j; // Put it in placeif (i != lowindex) // Add check for exerciseswap(A, i, lowindex);}}(b) There is unlikely to be much improvement; more likely the algorithmwill slow down. This is because the time spent checking (n times) isunlikely to save enough swaps to make up.(c) Try it and see!7.6 • Insertion Sort is stable. A swap is done only if the lower element’svalue is LESS.• Bubble Sort is stable. A swap is done only if the lower element’s valueis LESS.• Selection Sort is NOT stable. The new low value is set only if it isactually less than the previous one, but the direction of the search isfrom the bottom of the array. The algorithm will be stable if “less than”in the check becomes “less than or equal to” for selecting the low key position.• Shell Sort is NOT stable. The sublist sorts are done independently, andit is quite possible to swap an element in one sublist ahead of its equalvalue in another sublist. Once they are in the same sublist, they willretain this (incorrect) relationship.• Quick-sort is NOT stable. After selecting the pivot, it is swapped withthe last element. This action can easily put equal records out of place.48 Chap. 7 Internal Sorting• Conceptually (in particular, the linked list version) Mergesort is stable.The array implementations are NOT stable, since, given that the sublistsare stable, the merge operation will pick the element from the lower listbefore the upper list if they are equal. This is easily modified to replace“less than” with “less than or equal to.”• Heapsort is NOT stable. Elements in separate sides of the heap are processed independently, and could easily become out of relative order.• Binsort is stable. Equal values that come later are appended to the list.• Radix Sort is stable. While the processing is from bottom to top, thebins are also filled from bottom to top, preserving relative order.7.7 In the worst case, the stack can store n records. This can be cut to log n in the worst case by putting the larger partition on FIRST, followed by the smaller. Thus, the smaller will be processed first, cutting the size of the next stacked partition by at least half.7.8 Here is how I derived a permutation that will give the desired (worst-case) behavior:a b c 0 d e f g First, put 0 in pivot index (0+7/2),assign labels to the other positionsa b c g d e f 0 First swap0 b c g d e f a End of first partition pass0 b c g 1 e f a Set d = 1, it is in pivot index (1+7/2)0 b c g a e f 1 First swap0 1 c g a e f b End of partition pass0 1 c g 2 e f b Set a = 2, it is in pivot index (2+7/2)0 1 c g b e f 2 First swap0 1 2 g b e f c End of partition pass0 1 2 g b 3 f c Set e = 3, it is in pivot index (3+7/2)0 1 2 g b c f 3 First swap0 1 2 3 b c f g End of partition pass0 1 2 3 b 4 f g Set c = 4, it is in pivot index (4+7/2)0 1 2 3 b g f 4 First swap0 1 2 3 4 g f b End of partition pass0 1 2 3 4 g 5 b Set f = 5, it is in pivot index (5+7/2)0 1 2 3 4 g b 5 First swap0 1 2 3 4 5 b g End of partition pass0 1 2 3 4 5 6 g Set b = 6, it is in pivot index (6+7/2)0 1 2 3 4 5 g 6 First swap0 1 2 3 4 5 6 g End of parition pass0 1 2 3 4 5 6 7 Set g = 7.Plugging the variable assignments into the original permutation yields:492 6 4 0 13 5 77.9 (a) Each call to qsort costs Θ(i log i). Thus, the total cost isni=1i log i = Θ(n2 log n).(b) Each call to qsort costs Θ(n log n) for length(L) = n, so the totalcost is Θ(n2 log n).7.10 All that we need to do is redefine the comparison test to use strcmp. The quicksort algorithm itself need not change. This is the advantage of paramerizing the comparator.7.11 For n = 1000, n2 = 1, 000, 000, n1.5 = 1000 ∗√1000 ≈ 32, 000, andn log n ≈ 10, 000. So, the constant factor for Shellsort can be anything less than about 32 times that of Insertion Sort for Shellsort to be faster. The constant factor for Shellsort can be anything less than about 100 times thatof Insertion Sort for Quicksort to be faster.7.12 (a) The worst case occurs when all of the sublists are of size 1, except for one list of size i − k + 1. If this happens on each call to SPLITk, thenthe total cost of the algorithm will be Θ(n2).(b) In the average case, the lists are split into k sublists of roughly equal length. Thus, the total cost is Θ(n logk n).7.13 (This question comes from Rawlins.) Assume that all nuts and all bolts havea partner. We use two arrays N[1..n] and B[1..n] to represent nuts and bolts. Algorithm 1Using merge-sort to solve this problem.First, split the input into n/2 sub-lists such that each sub-list contains twonuts and two bolts. Then sort each sub-lists. We could well come up with apair of nuts that are both smaller than either of a pair of bolts. In that case,all you can know is something like:N1, N2。
数据结构与算法设计课后习题及答案详解

第一章1.数据结构研究的主要内容包括逻辑结构、存储结构和算法。
2.数据元素是数据的基本单位,数据项是数据的最小标示单位。
3.根据数据元素之间关系的不同,数据的逻辑结构可以分为集合、树形、线性、图状。
4.常见的数据存储结构有四种类型:顺序、链式、索引、散列。
5.可以从正确性、可读性、健壮性、高效性四方面评价算法的质量。
6.在一般情况下,一个算法的时间复杂度是问题规模的函数。
7.常见时间复杂度有:常数阶O(1)、线性阶O(n)、对数阶O(log2 n)、平方阶O(n²)和指数阶O(2ⁿ)。
通常认为,具有常数阶量级的算法是好算法,而具有指数阶量级的算法是差算法。
8.时间复杂度排序由大到小(n+2)!>2ⁿ+²>(n+2)4次方>nlog2 n>100000.问答题:1.什么叫数据元素?数据元素是数据的基本单位,是数据这个集合的个体,也称为元素、结点、顶点、记录。
2.什么叫数据逻辑结构?什么叫数据存储结构?数据逻辑结构:指数据元素之间存在的固有的逻辑结构。
数据存储结构:数据元素及其关系在计算机内的表示。
3.什么叫抽象数据类型?抽象数据类型是指数据元素集合以及定义在该集合上的一组操作。
4.数据元素之间的关系在计算机中有几种表示方法?顺序、链式、索引、散列。
5.数据的逻辑结构与数据的存储结构之间存在着怎样的关系?相辅相成,不可分割。
6.什么叫算法?算法的性质有哪些?算法:求解问题的一系列步骤的集合。
可行性、有容性、确定性、有输入、有输出。
7.评价一个算法的好坏应该从哪几方面入手?正确性、可读性、健壮性、高效性。
第二章1.线性表中,第一个元素没有直接前驱,最后一个元素没有直接后继。
2.线性表常用的两种存储结构分别是顺序存储结构和链式存储结构。
3.在长度为n的顺序表中,插入一个新元素平均需要移动表中的n/2个元素,删除一个元素平均需要移动(n-1)/2个元素。
4.在长度为n的顺序表的表头插入一个新元素的时间复杂度为O(n),在表尾插入一个新元素的时间复杂度为O(1)。
《数据结构与算法》习题与答案

《数据结构与算法》习题与答案(解答仅供参考)一、名词解释:1. 数据结构:数据结构是计算机存储、组织数据的方式,它不仅包括数据的逻辑结构(如线性结构、树形结构、图状结构等),还包括物理结构(如顺序存储、链式存储等)。
它是算法设计与分析的基础,对程序的效率和功能实现有直接影响。
2. 栈:栈是一种特殊的线性表,其操作遵循“后进先出”(Last In First Out, LIFO)原则。
在栈中,允许进行的操作主要有两种:压栈(Push),将元素添加到栈顶;弹栈(Pop),将栈顶元素移除。
3. 队列:队列是一种先进先出(First In First Out, FIFO)的数据结构,允许在其一端插入元素(称为入队),而在另一端删除元素(称为出队)。
常见的实现方式有顺序队列和循环队列。
4. 二叉排序树(又称二叉查找树):二叉排序树是一种二叉树,其每个节点的左子树中的所有节点的值都小于该节点的值,而右子树中的所有节点的值都大于该节点的值。
这种特性使得能在O(log n)的时间复杂度内完成搜索、插入和删除操作。
5. 图:图是一种非线性数据结构,由顶点(Vertex)和边(Edge)组成,用于表示对象之间的多种关系。
根据边是否有方向,可分为有向图和无向图;根据是否存在环路,又可分为有环图和无环图。
二、填空题:1. 在一个长度为n的顺序表中,插入一个新元素平均需要移动______个元素。
答案:(n/2)2. 哈希表利用______函数来确定元素的存储位置,通过解决哈希冲突以达到快速查找的目的。
答案:哈希(Hash)3. ______是最小生成树的一种算法,采用贪心策略,每次都选择当前未加入生成树且连接两个未连通集合的最小权重边。
答案:Prim算法4. 在深度优先搜索(DFS)过程中,使用______数据结构来记录已经被访问过的顶点,防止重复访问。
答案:栈或标记数组5. 快速排序算法在最坏情况下的时间复杂度为______。
《数据结构与算法》课后答案

2.3 课后习题解答2.3.2 判断题1.线性表的逻辑顺序与存储顺序总是一致的。
(×)2.顺序存储的线性表可以按序号随机存取。
(√)3.顺序表的插入和删除操作不需要付出很大的时间代价,因为每次操作平均只有近一半的元素需要移动。
(×)4.线性表中的元素可以是各种各样的,但同一线性表中的数据元素具有相同的特性,因此属于同一数据对象。
(√)5.在线性表的顺序存储结构中,逻辑上相邻的两个元素在物理位置上并不一定相邻。
(×)6.在线性表的链式存储结构中,逻辑上相邻的元素在物理位置上不一定相邻。
(√)7.线性表的链式存储结构优于顺序存储结构。
(×)8.在线性表的顺序存储结构中,插入和删除时移动元素的个数与该元素的位置有关。
(√)9.线性表的链式存储结构是用一组任意的存储单元来存储线性表中数据元素的。
(√)10.在单链表中,要取得某个元素,只要知道该元素的指针即可,因此,单链表是随机存取的存储结构。
(×)11.静态链表既有顺序存储的优点,又有动态链表的优点。
所以它存取表中第i个元素的时间与i无关。
(×)12.线性表的特点是每个元素都有一个前驱和一个后继。
(×)2.3.3 算法设计题1.设线性表存放在向量A[arrsize]的前elenum个分量中,且递增有序。
试写一算法,将x 插入到线性表的适当位置上,以保持线性表的有序性,并且分析算法的时间复杂度。
【提示】直接用题目中所给定的数据结构(顺序存储的思想是用物理上的相邻表示逻辑上的相邻,不一定将向量和表示线性表长度的变量封装成一个结构体),因为是顺序存储,分配的存储空间是固定大小的,所以首先确定是否还有存储空间,若有,则根据原线性表中元素的有序性,来确定插入元素的插入位置,后面的元素为它让出位置,(也可以从高下标端开始一边比较,一边移位)然后插入x ,最后修改表示表长的变量。
int insert (datatype A[],int *elenum,datatype x) /*设elenum为表的最大下标*/{if (*elenum==arrsize-1) return 0; /*表已满,无法插入*/else {i=*elenum;while (i>=0 && A[i]>x) /*边找位置边移动*/{A[i+1]=A[i];i--;}A[i+1]=x; /*找到的位置是插入位的下一位*/(*elenum)++;return 1; /*插入成功*/}}时间复杂度为O(n)。
数据结构与算法分析课后习题答案

数据结构与算法分析课后习题答案【篇一:《数据结构与算法》课后习题答案】>2.3.2 判断题2.顺序存储的线性表可以按序号随机存取。
(√)4.线性表中的元素可以是各种各样的,但同一线性表中的数据元素具有相同的特性,因此属于同一数据对象。
(√)6.在线性表的链式存储结构中,逻辑上相邻的元素在物理位置上不一定相邻。
(√)8.在线性表的顺序存储结构中,插入和删除时移动元素的个数与该元素的位置有关。
(√)9.线性表的链式存储结构是用一组任意的存储单元来存储线性表中数据元素的。
(√)2.3.3 算法设计题1.设线性表存放在向量a[arrsize]的前elenum个分量中,且递增有序。
试写一算法,将x 插入到线性表的适当位置上,以保持线性表的有序性,并且分析算法的时间复杂度。
【提示】直接用题目中所给定的数据结构(顺序存储的思想是用物理上的相邻表示逻辑上的相邻,不一定将向量和表示线性表长度的变量封装成一个结构体),因为是顺序存储,分配的存储空间是固定大小的,所以首先确定是否还有存储空间,若有,则根据原线性表中元素的有序性,来确定插入元素的插入位置,后面的元素为它让出位置,(也可以从高下标端开始一边比较,一边移位)然后插入x ,最后修改表示表长的变量。
int insert (datatype a[],int *elenum,datatype x) /*设elenum为表的最大下标*/ {if (*elenum==arrsize-1) return 0; /*表已满,无法插入*/else {i=*elenum;while (i=0 a[i]x)/*边找位置边移动*/{a[i+1]=a[i];i--;}a[i+1]=x;/*找到的位置是插入位的下一位*/ (*elenum)++;return 1;/*插入成功*/}}时间复杂度为o(n)。
2.已知一顺序表a,其元素值非递减有序排列,编写一个算法删除顺序表中多余的值相同的元素。
数据结构与算法设计与分析考核试卷

8.在冒泡排序中,每一趟排序都能确定一个元素的最终位置。()
答案:______
9. Prim算法和Kruskal算法都可以用来求解最小生成树问题,但Prim算法总是从某一顶点开始,而Kruskal算法总是从某一权值最小的边开始。()
答案:______
10.在一个递归算法中,如果递归调用不是算法的最后一个操作,那么这种递归称为尾递归。()
B.邻接表适合表示稀疏图
C.邻接多重表适合表示无向图
D.邻接表和邻接多重表适合表示有向图
14.以下哪些算法属于分治算法?()
A.快速排序
B.归并排序
C.二分查找
D.动态规划
15.以下哪些情况下,动态规划比贪心算法更适合解决问题?()
A.存在重叠子问题
B.问题具有最优子结构
C.需要考虑所有可能的选择
D.问题可以通过局部最优达到全局最优
C.插入一个节点
D.查找某个节点
5.以下哪些算法可以用于解决最小生成树问题?()
A. Kruskal算法
B. Prim算法
C. Dijkstra算法
D. Bellman-Ford算法
6.以下哪些数据结构可以用来实现堆?()
A.数组
B.链表
C.栈
D.队列
7.关于图的深度优先遍历和广度优先遍历,以下哪些说法是正确的?()
________________________________
2.动态规划算法通常用于解决最优化问题,请阐述动态规划算法的三个基本要素,并给出一个动态规划问题的实例。
________________________________
________________________________
数据结构与算法测试题+参考答案

数据结构与算法测试题+参考答案一、单选题(共80题,每题1分,共80分)1、某线性表中最常用的操作是在最后一个元素之后插入一个元素和删除第一个元素,则采用什么存储方式最节省运算时间?A、仅有头指针的单循环链表B、双链表C、仅有尾指针的单循环链表D、单链表正确答案:C2、数据结构研究的内容是()。
A、数据的逻辑结构B、数据的存储结构C、建立在相应逻辑结构和存储结构上的算法D、包括以上三个方面正确答案:D3、下列关于无向连通图特征的叙述中,正确的是:所有顶点的度之和为偶数边数大于顶点个数减1至少有一个顶点的度为1A、只有1B、1和2C、1和3D、只有2正确答案:A4、下面的程序段违反了算法的()原则。
void sam(){ int n=2;while (n%2==0) n+=2;printf(“%d”,n);}A、确定性B、可行性C、有穷性D、健壮性正确答案:C5、对任意给定的含 n (n>2) 个字符的有限集 S,用二叉树表示 S 的哈夫曼编码集和定长编码集,分别得到二叉树 T1 和 T2。
下列叙述中,正确的是:A、出现频次不同的字符在 T2 中处于相同的层B、出现频次不同的字符在 T1 中处于不同的层C、T1 的高度大于 T2 的高度D、T1 与 T2 的结点数相同正确答案:A6、数据序列{ 3,2,4,9,8,11,6,20 }只能是下列哪种排序算法的两趟排序结果?A、快速排序B、选择排序C、插入排序D、冒泡排序正确答案:A7、设散列表的地址区间为[0,16],散列函数为H(Key)=Key%17。
采用线性探测法处理冲突,并将关键字序列{ 26,25,72,38,8,18,59 }依次存储到散列表中。
元素59存放在散列表中的地址是:A、11B、9C、10D、8正确答案:A8、采用递归方式对顺序表进行快速排序,下列关于递归次数的叙述中,正确的是:A、每次划分后,先处理较短的分区可以减少递归次数B、递归次数与每次划分后得到的分区处理顺序无关C、递归次数与初始数据的排列次序无关D、每次划分后,先处理较长的分区可以减少递归次数正确答案:B9、以下数据结构中,()是非线性数据结构。
数据结构与算法题库(含参考答案)

数据结构与算法题库(含参考答案)一、单选题(共100题,每题1分,共100分)1、在一次校园活动中拍摄了很多数码照片,现需将这些照片整理到一个PowerPoint 演示文稿中,快速制作的最优操作方法是:A、创建一个 PowerPoint 相册文件。
B、创建一个 PowerPoint 演示文稿,然后批量插入图片。
C、创建一个 PowerPoint 演示文稿,然后在每页幻灯片中插入图片。
D、在文件夹中选中所有照片,然后单击鼠标右键直接发送到PowerPoint 演示文稿中。
正确答案:A2、下面对“对象”概念描述错误的是A、对象不具有封装性B、对象是属性和方法的封装体C、对象间的通信是靠消息传递D、一个对象是其对应类的实例正确答案:A3、设栈与队列初始状态为空。
首先A,B,C,D,E依次入栈,再F,G,H,I,J 依次入队;然后依次出队至队空,再依次出栈至栈空。
则输出序列为A、F,G,H,I,J,E,D,C,B,AB、E,D,C,B,A,J,I,H,G,FC、F,G,H,I,J,A,B,C,D,E,D、E,D,C,B,A,F,G,H,I,J正确答案:A4、设表的长度为 20。
则在最坏情况下,冒泡排序的比较次数为A、20B、19C、90D、190正确答案:D5、设二叉树的前序序列为 ABDEGHCFIJ,中序序列为 DBGEHACIFJ。
则后序序列为A、DGHEBIJFCAB、JIHGFEDCBAC、GHIJDEFBCAD、ABCDEFGHIJ正确答案:A6、Excel工作表B列保存了11位手机号码信息,为了保护个人隐私,需将手机号码的后 4 位均用“*”表示,以 B2 单元格为例,最优的操作方法是:A、=REPLACE(B2,7,4,"****")B、=REPLACE(B2,8,4,"****")C、=MID(B2,7,4,"****")D、=MID(B2,8,4,"****")第 10 组正确答案:B7、小金从网站上查到了最近一次全国人口普查的数据表格,他准备将这份表格中的数据引用到 Excel 中以便进一步分析,最优的操作方法是:A、通过 Excel 中的“自网站获取外部数据”功能,直接将网页上的表格导入到 Excel 工作表中。
数据结构与算法练习题库(含答案)

数据结构与算法练习题库(含答案)一、单选题(共80题,每题1分,共80分)1、对一棵二叉树的结点从 1 开始顺序编号。
要求每个结点的编号大于其左子树所有结点的编号、但小于右子树中所有结点的编号。
可采用▁▁▁▁▁ 实现编号。
A、中序遍历B、先序遍历C、层次遍历D、后序遍历正确答案:A2、设一段文本中包含4个对象{a,b,c,d},其出现次数相应为{4,2,5,1},则该段文本的哈夫曼编码比采用等长方式的编码节省了多少位数?A、5B、4C、2D、0正确答案:C3、两个有相同键值的元素具有不同的散列地址A、一定不会B、一定会C、可能会D、有万分之一的可能会正确答案:C4、将元素序列{18,23,11,20,2,7,27,33,42,15}按顺序插入一个初始为空的、大小为11的散列表中。
散列函数为:H(Key)=Key%11,采用线性探测法处理冲突。
问:当第一次发现有冲突时,散列表的装填因子大约是多少?A、0.73B、0.27C、0.64D、0.45正确答案:D5、对N个记录进行归并排序,归并趟数的数量级是:A、O(NlogN)B、O(logN)C、O(N)D、O(N2)正确答案:B6、下列说法不正确的是:A、图的遍历是从给定的源点出发每一个顶点仅被访问一次B、图的深度遍历不适用于有向图C、遍历的基本算法有两种:深度遍历和广度遍历D、图的深度遍历是一个递归过程正确答案:B7、二叉树的中序遍历也可以循环地完成。
给定循环中堆栈的操作序列如下(其中push为入栈,pop为出栈): push(1), push(2), push(3), pop(), push(4), pop(), pop(), push(5), pop(), pop(), push(6), pop()A、6是根结点B、2是4的父结点C、2和6是兄弟结点D、以上全不对正确答案:C8、设最小堆(小根堆)的层序遍历结果为{1, 3, 2, 5, 4, 7, 6}。
数据结构与算法练习题附答案

1、下面关于算法的说法错误的是()A、算法最终必须由计算机程序实现B、为解决某问题的算法同为该问题编写的程序含义是相同的C、算法的可行性是指指令不能有二义性D、以上几个都是错误的参考答案:D2、数据在计算机存储器内表示时,物理地址与逻辑地址不相同的,称为()A、存储结构B、逻辑结构C、链式存储结构D、顺序存储结构参考答案:C3、以下说法正确的是()(2分)A、数据元素是数据的最小单位B、数据项是数据的基本单位C、数据结构是带有结构的各数据项的集合D、数据结构是带有结构的数据元素的集合参考答案:D4、通常从正确性、易读性、健壮性、高效性等四个方面评价算法(包括程序)的质量。
以下解释错误的是()A、正确性算法应能正确地实现预定的功能(即处理要求)B、易读性算法应易于理解和阅读,以便于调试、修改和扩充C、健壮性当环境发生变化时,算法能适当地做出反应或进行处理,不会产生不需要的运行结果D、高效性即达到所需要的时间性能参考答案:C5、树形结构是数据元素之间存在一种()A、一对一关系B、多对多关系C、多对一关系D、一对多关系参考答案:D6、数据结构是指()A、数据元素的组织形式B、数据类型C、数据存储结构D、数据定义参考答案:A7、算法分析的目的是()A、找出数据结构的合理性3、研究算法中的输入和输出关系C、分析算法的效率以求改进D、分析算法的易懂性和文档性参考答案:C8、数据在计算机内有链式和顺序两种存储方式,在存储空间使用的灵活性上,链式存储比顺序存储要()A、低B、高C、相同D、以上都不正确参考答案:B9、算法的空间复杂度是指()A、执行算法程序所占的存储空间B、算法程序中的指令条数C、算法程序的长度D、算法执行过程中所需要的存储空间参考答案:D10、数据的存储结构是指()A、数据所占的存储空间量B、数据的逻辑结构在计算机中的表示C、数据在计算机中的顺序存储方式D、存数在外存中的数据参考答案:B11、线性表是()A、一个有限序列,可以为空B、一个有限序列,不能为空C、一个无限序列,可以为空D、一个无限序列,不能为空参考答案:A12、下列叙述正确的是()A、线性表是线性结构B、栈和队列是非线性结构C、线性链表是非线性结构D、二叉树是线性结构参考答案:A13、计算机内部数据处理的基本单位是()A、数据B、数据元素C、数据项D、数据库参考答案:B14、从逻辑上可以把数据结构分为()两大类A、动态结构、静态结构B、顺序结构、链式结构C、线性结构、非线性结构D、初等结构、构造型结构参考答案:C15、算法的时间复杂度取决于()A、问题的规模B、待处理数据的初态C、A 和B参考答案:C16、以下属于逻辑结构的是()(2分)A、顺序表B、哈希表C、有序表D、单链表参考答案:C17、下列数据结构中,()是非线性数据结构A、树B、字符串C、队D、栈参考答案:A18、设语句x++的时间是单位时间,则以下语句的时间复杂度为()for(i=1;i<=n;i++)for(j=|;j<=n;j++)x++;(2分)A、O(1)B、O(n2)C、O(n)D、O(n3)参考答案:B19、算法的计算量大小称为计算的()(2分)A、效率B、复杂性C、现实性D、难度参考答案:B20、数据结构只是研究数据的逻辑结构和物理结构,这种观点()A、正确B、错误C、前半句正确,后半句错误D、前半句错误,后半句正确参考答案:B21、计算机算法指的是(),它具有输入、输出、可行性、确定性和有穷性等五个特性。
数据结构与算法分析—期末复习题及答案

单选题(每题 2 分,共20分)1. 对一个算法的评价,不包括如下(B )方面的内容。
A.健壮性和可读性 B.并行性 C.正确性 D.时空复杂度2. 在带有头结点的单链表HL中,要向表头插入一个由指针p指向的结点,则执行( A )。
A. p->next=HL->next; HL->next=p;B. p->next=HL; HL=p;C. p->next=HL; p=HL;D. HL=p; p->next=HL;3. 对线性表,在下列哪种情况下应当采用链表表示?( B )A.经常需要随机地存取元素B.经常需要进行插入和删除操作C.表中元素需要占据一片连续的存储空间D.表中元素的个数不变4. 一个栈的输入序列为1 2 3,则下列序列中不可能是栈的输出序列的是( C )A. 2 3 1B. 3 2 1C. 3 1 2D. 1 2 36. 若需要利用形参直接访问实参时,应将形参变量说明为(D )参数。
A.值 B.函数 C.指针 D.引用8. 在稀疏矩阵的带行指针向量的链接存储中,每个单链表中的结点都具有相同的( A )。
A.行号 B.列号 C.元素值 D.非零元素个数10. 从二叉搜索树中查找一个元素时,其时间复杂度大致为(C )。
A. O(n)B. O(1)C. O(log2n)D. O(n2)二、运算题(每题 6 分,共24分)1. 数据结构是指数据及其相互之间的_联系。
当结点之间存在M对N(M:N)的联系时,称这种结构为__图__。
2. 队列的插入操作是在队列的___尾_进行,删除操作是在队列的_首_进行。
3. 当用长度为N的数组顺序存储一个栈时,假定用top==N表示栈空,则表示栈满的条件是___top==0___(要超出才为满)_______________。
4. 对于一个长度为n的单链存储的线性表,在表头插入元素的时间复杂度为___ O(1)__,在表尾插入元素的时间复杂度为___ O(n)___。
(完整版)数据结构与算法第1章参考答案08

for(j=i+1;j<=n;j++)
if(R[j]>R[j+1])k=j;
t=R[k];R[k]=R[i];R[i]=t;
}
[解答]O(n2)
7.计算一元n次多项式P( x,n)=a0+alx+a2x2+..+anxn“的值,输人x,n,a0,al,…,an,输出多项式P(x,n)的值。设计算法求解,请选择合适的输人、输出格式,要求算法具有较好的时间性能。
习题参考答案
一.选择题
1.从逻辑上可以把数据结构分为(C)两大类。
A.动态结构、静态结构B.顺序结构、链式结构
C.线性结构、非线性结构D.初等结构、构造型结构
2.在下面的程序段中,对x的斌值语句的频度为(C)。
for( t=1;k<=n;k++)
for(j=1;j<=n; j++)
x=x十1;
A. O(2n)B.O(n)C.O(n2).D.O(1og2n)
D.同一个算法,实现语言的级别越高,执行效率就越低
5.在发生非法操作时,算法能够作出适当处理的特性称为(B)。
A.正确性B.健壮性C.可读性D.可移植性
二、判断题
1.数据的逻辑结构是指数据的各数据项之间的逻辑关系。(√)
2.顺序存储方式的优点是存储密度大,且插人、删除运算效率高。(×)
3.数据的逻辑结构说明数据元素之间的次序关系,它依赖于数据的存储结构。(×)
3.采用链式存储结构表示数据时,相邻的数据元素的存储地址(C)。
A.一定连续B.一定不连续
C.不一定连续D.部分连续,部分不连续
4.下面关于算法说法正确的是(D)。
A.算法的时间复杂度一般与算法的空间复杂度成正比
B.解决某问题的算法可能有多种,但肯定采用相同的数据结构
数据结构与算法习题含参考答案

数据结构与算法习题含参考答案一、单选题(共100题,每题1分,共100分)1、要为 Word 2010 格式的论文添加索引,如果索引项已经以表格形式保存在另一个 Word文档中,最快捷的操作方法是:A、在 Word 格式论文中,逐一标记索引项,然后插入索引B、直接将以表格形式保存在另一个 Word 文档中的索引项复制到 Word 格式论文中C、在 Word 格式论文中,使用自动插入索引功能,从另外保存 Word 索引项的文件中插D、在 Word 格式论文中,使用自动标记功能批量标记索引项,然后插入索引正确答案:D2、下面不属于计算机软件构成要素的是A、文档B、程序C、数据D、开发方法正确答案:D3、JAVA 属于:A、操作系统B、办公软件C、数据库系统D、计算机语言正确答案:D4、在 PowerPoint 演示文稿中,不可以使用的对象是:A、图片B、超链接C、视频D、书签第 6 组正确答案:D5、下列叙述中正确的是A、软件过程是软件开发过程和软件维护过程B、软件过程是软件开发过程C、软件过程是把输入转化为输出的一组彼此相关的资源和活动D、软件过程是软件维护过程正确答案:C6、在 Word 中,不能作为文本转换为表格的分隔符的是:A、@B、制表符C、段落标记D、##正确答案:D7、某企业为了建设一个可供客户在互联网上浏览的网站,需要申请一个:A、密码B、门牌号C、域名D、邮编正确答案:C8、面向对象方法中,将数据和操作置于对象的统一体中的实现方式是A、隐藏第 42 组B、抽象C、结合D、封装正确答案:D9、下面属于整数类 I 实例的是A、-919B、0.919C、919E+3D、919D-2正确答案:A10、定义课程的关系模式如下:Course (C#, Cn, Cr,prC1#, prC2#)(其属性分别为课程号、课程名、学分、先修课程号 1和先修课程号 2),并且不同课程可以同名,则该关系最高是A、BCNFB、2NFC、1NFD、3NF正确答案:A11、循环队列的存储空间为 Q(1:100),初始状态为 front=rear=100。
数据结构与算法单元测试题1及答案

一、单选题1、在数据结构中,从逻辑上可以将之分为()结构。
A.线性和非线性结构B.动态和静态结构C.紧凑和非紧凑结构D.内部和非内部结构正确答案:A2、算法的时间复杂度取决于()。
A.问题的规模B.待处理数据的初态C.没有正确答案D.问题的规模以及待处理数据的初态正确答案:D3、某算法的时间复杂度是O(n2),表明该算法的()。
A.执行时间与n2成正比B.问题规模与n2成正比C.执行时间等于n2D.问题规模是n2正确答案:A4、衡量算法效率优劣的不包括()。
A.正确性和可读性B.现实性C.健壮性/鲁棒性D.高效率与低存储正确答案:B5、算法效率分析的两个主要方面是()。
A.空间复杂度和时间复杂度B.数据复杂性和程序复杂性C.正确性和简明性D.可读性和文档性正确答案:A6、下面程序段的时间复杂度为()。
for(i=0;i<n;i++)for(j=0;j<m;j++)A[i][j]=0;A.O(n*m)B.O(1)C.O(n2)D.O(m2)正确答案:A7、下面程序段的时间复杂度为()。
void sum(int n) //n为正整数{int p=1,sum=0,i;for(i=1;i<=n;i++){p*=i;sum+=p;}}A.O(n2)B.O(n)C.O(1)D.O(√n)正确答案:B8、下述哪一条是顺序存储结构的优点()。
A.插入运算方便B.随机存取C.可方便地用于各种逻辑结构的存储表示D.删除运算方便正确答案:B9、静态链表中指针表示的是()。
A.下一元素在数组中的下标B.左、右孩子地址C.数组下标D.内存地址正确答案:A10、若长度为n的线性表采用顺序存储结构,在其第i个位置插入一个新元素的算法的时间复杂度为()。
A.O(n2)B.O(0)C.O(n)D.O(1)正确答案:C11、对于顺序表,访问结点和删除结点的时间复杂度分别为()。
A.O(n) O(n)B.O(1) O(n)C.O(1) O(1)D.O(n) O(1)正确答案:B12、对于一个带头结点的单链表,其头指针为head,判定该表为空表的条件是()。
数据结构与算法分析习题与参考答案

大学《数据结构与算法分析》课程习题及参考答案模拟试卷一一、单选题(每题 2 分,共20分)1.以下数据结构中哪一个是线性结构?( )A. 有向图B. 队列C. 线索二叉树D. B树2.在一个单链表HL中,若要在当前由指针p指向的结点后面插入一个由q指向的结点,则执行如下( )语句序列。
A. p=q; p->next=q;B. p->next=q; q->next=p;C. p->next=q->next; p=q;D. q->next=p->next; p->next=q;3.以下哪一个不是队列的基本运算?()A. 在队列第i个元素之后插入一个元素B. 从队头删除一个元素C. 判断一个队列是否为空D.读取队头元素的值4.字符A、B、C依次进入一个栈,按出栈的先后顺序组成不同的字符串,至多可以组成( )个不同的字符串?A.14B.5C.6D.85.由权值分别为3,8,6,2的叶子生成一棵哈夫曼树,它的带权路径长度为( )。
以下6-8题基于图1。
6.该二叉树结点的前序遍历的序列为( )。
A.E、G、F、A、C、D、BB.E、A、G、C、F、B、DC.E、A、C、B、D、G、FD.E、G、A、C、D、F、B7.该二叉树结点的中序遍历的序列为( )。
A. A、B、C、D、E、G、FB. E、A、G、C、F、B、DC. E、A、C、B、D、G、FE.B、D、C、A、F、G、E8.该二叉树的按层遍历的序列为( )。
A.E、G、F、A、C、D、B B. E、A、C、B、D、G、FC. E、A、G、C、F、B、DD. E、G、A、C、D、F、B9.下面关于图的存储的叙述中正确的是( )。
A.用邻接表法存储图,占用的存储空间大小只与图中边数有关,而与结点个数无关B.用邻接表法存储图,占用的存储空间大小与图中边数和结点个数都有关C. 用邻接矩阵法存储图,占用的存储空间大小与图中结点个数和边数都有关D.用邻接矩阵法存储图,占用的存储空间大小只与图中边数有关,而与结点个数无关10.设有关键码序列(q,g,m,z,a,n,p,x,h),下面哪一个序列是从上述序列出发建堆的结果?( )A. a,g,h,m,n,p,q,x,zB. a,g,m,h,q,n,p,x,zC. g,m,q,a,n,p,x,h,zD. h,g,m,p,a,n,q,x,z二、填空题(每空1分,共26分)1.数据的物理结构被分为_________、________、__________和___________四种。