数据结构与算法分析 第2章

合集下载

数据结构课后习题及解析第二章

数据结构课后习题及解析第二章
利用单向循环链表作为存储结构模拟此过程,按照出列顺序打印出各人的编号。
例如m的初值为20;n=7,7个人的密码依次是:3,1,7,2,4,8,4,出列的顺序为6,1,4,7,2,3,5。
第二章答案
约瑟夫环问题
约瑟夫问题的一种描述为:编号1,2,…,n的n个人按顺时针方向围坐一圈,每个人持有一个密码(正整数)。一开始任选一个报数上限值m,从第一个人开始顺时针自1开始顺序报数,报到m时停止报数。报m的人出列,将他的密码作为新的m值,从他在顺时针方向上的下一个人开始重新从1报数,如此下去,直至所有的人全部出列为止。试设计一个程序,求出出列顺序。利用单向循环链表作为存储结构模拟此过程,按照出列顺序打印出各人的编号。
9.假设有一个循环链表的长度大于1,且表中既无头结点也无头指针。已知s为指向链表某个结点的指针,试编写算法在链表中删除指针s所指结点的前趋结点。
10.已知有单链表表示的线性表中含有三类字符的数据元素(如字母字符、数字字符和其它字符),试编写算法来构造三个以循环链表表示的线性表,使每个表中只含同一类的字符,且利用原表中的结点空间作为这三个表的结点空间,头结点可另辟空间。
r=p;
}
}
r->next=L->next;
printf("请输入第一个报数上限值m(m>0):");
scanf("%d",&m);
printf("*****************************************\n");
printf("出列的顺序为:\n");
q=L;
p=L->next;
7.试分别以不同的存储结构实现线性表的就地逆置算法,即在原表的存储空间将线性表(a1, a2..., an)逆置为(an, an-1,..., a1)。

数据结构第二章习题(1)

数据结构第二章习题(1)

B. 单链表
C. 双链表
D. 单循环链表
3.具有线性结构的数据结构是( )。
A. 图
B. 树
C. 广义表
D. 栈
4.在一个长度为 n 的顺序表中,在第 i 个元素之前插入一个新元素时,需向后移动( )
个元素。
A. n-i
B. n-i+1
C. n-i-1
5.非空的循环单链表 head 的尾结点 p 满足( )。
A. n-i
B. n-i+1
C. n-i-1
D. i+1
10.线性表是n个( )的有限序列。
A. 表元素
B. 字符 C. 数据元素 D. 数据项
11.从表中任一结点出发,都能扫描整个表的是( )。
A. 单链表
B. 顺序表
C. 循环链表
D. 静态链表
12.在具有n个结点的单链表上查找值为x的元素时,其时间复杂度为( )。
A. q->next=s->next;s->next=p; B. s->next=p;q->next=s->next;
C. p->next=s->next;s->next=q; D. s->next=q;p->next=s->next; 24.在以下的叙述中,正确的是( )。
A. 线性表的顺序存储结构优于链表存储结构 B. 线性表的顺序存储结构适用于频繁插入/删除数据元素的情况
A. p->next=p->next->next;
B. p=p->next;p->next=p->next->next;
C. p =p->next;

数据结构的课程设计目的

数据结构的课程设计目的

数据结构的课程设计目的一、课程目标知识目标:1. 掌握数据结构的基本概念,包括线性结构(如数组、链表、栈、队列)和非线性结构(如树、图等)的特点与应用。

2. 学会分析不同数据结构在存储和处理数据时的效率,理解时间复杂度和空间复杂度的概念。

3. 掌握常见数据结构的具体实现方法,并能够运用到实际编程中。

技能目标:1. 培养学生运用数据结构解决实际问题的能力,能够根据问题的特点选择合适的数据结构进行优化。

2. 提高学生的编程实践能力,使其能够熟练编写与数据结构相关的程序代码,并进行调试与优化。

3. 培养学生独立思考和团队协作的能力,通过项目实践和课堂讨论,提高问题分析、解决方案的设计与实现能力。

情感态度价值观目标:1. 培养学生对数据结构学科的兴趣,激发学生的学习热情和主动探究精神。

2. 培养学生严谨、细致、踏实的学术态度,使其认识到数据结构在计算机科学与软件开发领域的重要性。

3. 培养学生具备良好的团队合作精神,学会倾听、沟通、表达与协作,提高人际交往能力。

课程性质:本课程为计算机科学与技术及相关专业的基础课程,旨在培养学生的数据结构知识和编程技能,提高学生解决实际问题的能力。

学生特点:学生已具备一定的编程基础,具有一定的逻辑思维能力和问题解决能力,但可能对数据结构的应用和实现方法了解不足。

教学要求:结合学生特点,课程设计应注重理论与实践相结合,以案例驱动、项目导向的教学方法,引导学生掌握数据结构知识,提高编程实践能力。

同时,注重培养学生的情感态度价值观,使其在学习过程中形成积极的学习态度和良好的团队协作精神。

通过具体的学习成果评估,确保课程目标的达成。

二、教学内容1. 线性结构:- 数组:数组的概念、存储方式、应用场景。

- 链表:单链表、双向链表、循环链表的概念及实现。

- 栈与队列:栈的概念、应用场景、实现方法;队列的概念、应用场景、实现方法。

2. 非线性结构:- 树:树的概念、二叉树、二叉查找树、平衡树(如AVL树)、堆的概念及其应用。

《数据结构与算法(C++语言版)》第2章 线性表

《数据结构与算法(C++语言版)》第2章 线性表
• 以下是一个使用类LinearList的C++程序,它假定之前的程 序均存储在LinearList.h之中,且异常类定义位于文件 exception.h之中。该示例完成以下操作:创建一个大小为5 的整数线性表L;输出该表的长度(为0);在第0个元素之 后插入2;在第一个元素之后插入6和8(至此,线性表为2, 6,8);寻找并输出第一个元素(为2);输出当前表的长 度(为3);删除并输出第一个元素。
数据结构与算法 (C++语言版)
第2章 线性表
线性表的类型定义
• 基本概念 • 线性表是由n(n≥0)个类型相同的数据元素组成的有限序 列,通常表示为L=(a1, …, ai–1, ai, ai+1, …, an)。其中,L为线 性表名称,ai为组成该线性表的数据元素,ai–1领先于ai,ai 领先于ai+1,称ai–1是ai的直接前驱元素,ai+1是ai的直接后继 元素。当i=1, 2, …, n–1时,ai有且仅有一个直接后继;当 i=2, 3, …, n时,ai有且仅有一个直接前驱。 • 线性表的长度就是线性表中元素的个数n(n≥0)。当n=0时, 称为空表。在非空表中的每个数据元素都有一个确定的位 置,如a1是第一个数据元素,an是最后一个数据元素,ai是 第i个数据元素。称i为数据元素ai在线性表中的位序。
线性表的类型定义
Prev_Elem(L, cur_e, &pre_e) //返回当前元素的前一个元素值 输入:线性表L。 输出:若cur_e是线性表L的数据元素,且不是第一个,则用 pre_e返回它的直接前驱元 素;否则操作失败,pre_e无定义。 Next_Elem(L, cur_e, &next_e) //返回当前元素的后一个元素值 输入:线性表L。 输出:若cur_e是线性表L的数据元素,且不是最后一个,则用 next_e返回它的直接后继元素;否则操作失败,next_e无定 义。

《数据结构与算法分析》(C++第二版)【美】Clifford A.Shaffer著 课后习题答案 二

《数据结构与算法分析》(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。

数据结构与算法分析课后习题答案

数据结构与算法分析课后习题答案

数据结构与算法分析课后习题答案【篇一:《数据结构与算法》课后习题答案】>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,其元素值非递减有序排列,编写一个算法删除顺序表中多余的值相同的元素。

数据结构课后习题与解析第二章

数据结构课后习题与解析第二章

第二章习题1. 描述以下三个概念的区别:头指针,头结点,首元素结点。

2. 填空:(1)在顺序表中插入或删除一个元素,需要平均移动元素,具体移动的元素个数与有关。

(2)在顺序表中,逻辑上相邻的元素,其物理位置相邻。

在单链表中,逻辑上相邻的元素,其物理位置相邻。

(3)在带头结点的非空单链表中,头结点的存储位置由指示,首元素结点的存储位置由指示,除首元素结点外,其它任一元素结点的存储位置由指示。

3.已知L是无表头结点的单链表,且P结点既不是首元素结点,也不是尾元素结点。

按要求从下列语句中选择合适的语句序列。

a. 在P结点后插入S结点的语句序列是:。

b. 在P结点前插入S结点的语句序列是:。

c. 在表首插入S结点的语句序列是:。

d. 在表尾插入S结点的语句序列是:。

供选择的语句有:(1)P->next=S;(2)P->next= P->next->next;(3)P->next= S->next;(4)S->next= P->next;(5)S->next= L;(6)S->next= NULL;(7)Q= P;(8)while(P->next!=Q) P=P->next;(9)while(P->next!=NULL) P=P->next;(10)P= Q;(11)P= L;(12)L= S;(13)L= P;4. 设线性表存于a(1:arrsize)的前elenum个分量中且递增有序。

试写一算法,将X插入到线性表的适当位置上,以保持线性表的有序性。

5. 写一算法,从顺序表中删除自第i个元素开始的k个元素。

6. 已知线性表中的元素(整数)以值递增有序排列,并以单链表作存储结构。

试写一高效算法,删除表中所有大于mink且小于maxk的元素(若表中存在这样的元素),分析你的算法的时间复杂度(注意:mink和maxk是给定的两个参变量,它们的值为任意的整数)。

数据结构第2章典型例题解析

数据结构第2章典型例题解析

第2章线性表典型例题解析一、选择题1.线性表是具有n个(n≥0)的有限序列。

A.表元素B.字符C.数据元素D.数据项【分析】线性表是具有相同数据类型的n(n≥0)个数据元素的有限序列,通常记为(a1,a2,…,a n),其中n为表长,n=0时称为空表。

【答案】C2.顺序存储结构的优点是。

A.存储密度大B.插入运算方便C.删除运算方便D.可方便地用于各种逻辑结构的存储表示【分析】顺序存储结构是采用一组地址连续的存储单元来依次存放数据元素,数据元素的逻辑顺序和物理次序一致。

因此,其存储密度大。

【答案】A3.带头结点的单链表head为空的判断条件是。

A.head==NULL B.head->next==NULLC.head->next==head D.head!=NULL【分析】链表为空时,头结点的指针域为空。

【答案】B4.若某线性表中最常用的操作是在最后一个元素之后插入一个元素和删除第一个元素,则采用存储方式最节省运算时间。

A.单链表B.仅有头指针的单循环链表C.双链表D.仅有尾指针的单循环链表【分析】根据题意要求,该线性表的存储应能够很方便地找到线性表的第一个元素和最后一个元素,A和B都能很方便地通过头指针找到线性表的第一个元素,却要经过所有元素才能找到最后一个元素;选项C双链表若存为双向循环链表,则能很方便地找到线性表的第一个元素和最后一个元素,但存储效率要低些,插入和删除操作也略微复杂;选项D可通过尾指针直接找到线性表的最后一个元素,通过线性表的最后一个元素的循环指针就能很方便地找到第一个元素。

【答案】D5.若某线性表最常用的操作是存取任一指定序号的元素和在最后进行插入和删除运算,则利用存储方式最节省时间。

A.顺序表B.双链表C.带头结点的双循环链表D.单循环链表【分析】某线性表最常用的操作是存取任一指定序号的元素和在最后进行插入和删除运算。

因此不需要移动线性表种元素的位置。

根据题意要求,该线性表的存储应能够很方便地找到线性表的任一指定序号的元素和最后一个元素,顺序表是由地址连续的向量实现的,因此具有按序号随机访问的特点。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

2.3 线性表的链式存储结构
• 2.3.1 单向链表
– 对于链式分配线性表,整个链表的存取必须是 从头指针开始,头指针指示链表中第一个结点 的存储位置。同时由于最后一个数据元素,没 有直接后继,则线性链表中最后一个结点的指 针为“空”(null)。 – 在使用单链表结点时,必须完成三步:
• 首先创建一个新结点; • 为该结点赋一个新值;新结点的next域赋值个当前 元素; • 当前结点的前驱的next域要指向新插入的结点。
2.1 线性表类型的定义
• 线性表的离散定义是:B=<A,R>,其中A包含n个结点 (a1,a2……an), R只包含一个关系。 R={(ai-1,ai) | I=1,2,……n},线性表中包含的数据元素个数为线性 表的长度。 • 一个数据元素通常包含多个数据项,此时每个数据元素 称为记录,含有大量的记录的线性表称为文件。 • 在稍微复杂的线性表中,一个数据元素可以由若干个数 据项组成。 • 线性表是一个比较灵活的数据结构,它的长度根据需要 增长或缩短,也可以对线性表的数据元素进行不同的操 作(如访问数据元素、插入、删除数据元素等)。
第2章 线性表
2.1 线性表类型的定义 2.2 线性表的顺序表示和实现 2.3 线性表的链式存储结构 2.3.1 单向链表 2.3.2 单链表的基本运算 2.3.3 循环链表 2.3.4 双链表 2.4 链表应用举例 2.5 顺序表和链表的比较
2.1 线性表类型的定义
• 线性表是n个数据元素的有限序列。其一般描述 为: • A=(a1,a2,……an) • 其中A称为线性表的名称, 每个ai(n≥i≥1)称为 线性表的数据元素,具体n的值含义则称为线性 表中包含有数据元素的个数,也称为线性表的长 度;当n的值等于0时,表示该线性表是空表。每 个数据元素的含义在不同情况下各不相同,它们 可能是一个字母、一个数字、也可以是一条记录 等。一般情况下,在线性表中每个ai的描述的是 一组相同属性的数据。
2.3 线性表的链式存储结构
• 2.3.3 循环链表
– 循环线性链表中已知链表中任何结点,可以找到链表中的所有结 点,我们一般还是习惯把头结点作为已知条件,但是如果已知条 件是头结点,将在以下的插入或删除结点时造成不方便: – 删除末尾结点 – 第一个结点前插入新结点 – 在第一种情况下,虽然能够完成删除,但是,需要我们从头结点 开始逐个查找结点直到找到最后一个结点的直接前驱结点,然后 才能够删除,整个算法的时间复杂度是O(n)。 – 在第二种情况下,虽然能够完成插入,但是,需要我们从头结点 开始逐个查找结点直到找到最后一个结点,然后才能够插入,因 为我们需要修改最后一个结点的指针域,整个算法的时间复杂度 是O(n)。•Βιβλιοθήκη 2.3 线性表的链式存储结构
• • 2.3.2 单链表的基本运算
头插入法
– – 如果我们在链表的开始结点之前附加一个结点,并 称它为头结点,那么会带来以下两个优点: 第一,由于开始结点的位置被存放在头结点的指针 域中,所以在链表的第一个位置上的操作就和在表 的其他位置上操作一致,无须进行特殊处理; 第二,无论链表是否为空,其头指针是指向头结点 的非空指针(空表中头结点的指针域空),因此空 表和非空表的处理也就统一了。
2.3 线性表的链式存储结构
• 2.3.1 单向链表
– 任意存储单元存储线性表的数据元素,对于链式存储线 性表时,其特点形式如图所示:
DATA
NEXT
•其中data 是数据域,存放数据元素的值;next是指针 域,存放相邻的下一个结点的地址,单向链表是指结 点中的指针域只有一个的沿着同一个方向表示的链式 存储结构。 •因为结点是一个独立的对象,所以能够实现独立的结 点类。
2.2 线性表的顺序表示和实现
• 线性表的存储结构分为顺序存储和非顺序存储。 其中顺序存储也称为向量存储或一维数组存储。 • (1)顺序表
– 线性表的顺序存储,也称为向量存储,又可以说是一 维数组存储。线性表中结点存放的物理顺序与逻辑顺 序完全一致,它叫向量存储(一般指一维数组存储), 与此同时对应A=(a1,a2,...an )线性表而言。 – 实际上,数据的存储逻辑位置由数组的下标决定。所 以相邻的元素之间地址的计算公式为(假设每个数据 元素占有c个存储单元): – LOC(ai+1)=LOC(ai)+ c
2.3 线性表的链式存储结构
• 在单链表中,因为指针是单一方向,结点 的查找只能从前向后查找,不能反向查找, 所以在插入、删除结点时,特别是在某个 结点之前插入,或者删除某个结点时,需 要利用结点的前趋结点的指针,所以在查 找结点时,需要保留结点的直接前趋结点 位置。也因为在单链表中,结点的查找只 能从前向后查找,不能反向查找,所以在 单向链表中,头结点的非常重要,不能丢 失。
2.3 线性表的链式存储结构
• 2.3.3 循环链表
– 以上的两种情况造成无谓的时间开销,为解决这个问 题,我们通常在循环链表以末尾结点指针为已知条件, 这样以上的两种情况,都可以直接完成,因为已知末 尾结点可以直接找到头结点,此时的时间复杂度为O (1),这样在不增加任何开销的情况下,减少了时 间的开销。 – 空的循环线性链表根据定义可以与单向链表相同,也 可以不相同。判断循环链表的末尾结点条件也就不同 于单向链表,不同之处在于是单向链表是判别最后结 点的指针域是否为空,而循环线性链表末尾结点的判 定条件是其指针域的值指向头结点。
2.2 线性表的顺序表示和实现
• (1)顺序表
– 顺序分配的线性表的可以直接使用一维数组描述为: – type arraylist[];//type的类型根据实际需要确定// – 通常用在数组的元素个数不是很多且可以对数组元素“枚举”的 情况下。也可以使用符合类型数组的动态进行动态定义。 – type arrayname[]; – 该代码只是对应用数组的声明,还没有对该数组分配空间,因此 不能访问数组。只有对数组进行初始化并申请内存资源后,才能 够对数组中元素进行使用和访问。 – arrayname= new type[arraysize]; – 其作用是给名称为arrayname的数组分配arraysize个类型为type 大小的空间;其中arraysize表示数组的长度,它可以是整型的常 量和变量;如果arraysize是常量,则分配固定大小的空间,如果 是变量,则表示根据参数动态分配数组的空间。

2.3 线性表的链式存储结构
• ② 查找运算 • 按序号查找: 按序号查找:
– 在链表中,即使知道被访问结点的序号i,也不能像顺 序表中那样直接按序号i访问结点,而只能从链表的头 指针出发,顺着链域next逐个结点往下搜索,直至搜 索到第i个结点为止(一般采用计数器的方式)。链表 不是随机存取结构,只能顺序存取。 – 查找之前首先要做到从头(head)开始,然后再逐个 向后查找,查找过程中,每向后移动依次,计数器增 加1,直到找到第i个结点(查找成功)或找完整个链 表,没有第i个结点(查找失败)。
2.2 线性表的顺序表示和实现
• (2)顺序表基本运算的实现 • 线性表的顺序存储的结构,容易实现线性表的某 些操作,如随机存取第i个数据元素等,但是在插 入或删除数据元素时则是比较烦琐,所以顺序存 储结构比较适合存取数据元素。应该注意java的 数组下标从0开始。下面考虑线性表顺序存储的 插入、删除和排序的实现方法。 • 顺序表的“求表长”和取第i个数据元素的时间复 杂度均为O(1),因为可以直接求出线性表的 长度,顺序存储下可可以实现随机存取,可以直 接取得数据元素,而不需要移动元素。
2.3 线性表的链式存储结构
• ③求链表长度
– 求链表长度基本同按序号查找,从头结点开始顺着链 域扫描,用指针curr指向当前扫描到的结点(原因是 头结点指针不能变),用len作计数器,累计当前扫描 的结点数,直至curr=null,返回长度len就可以了。
• ④ 删除结点
– 删除结点是将表中的某个结点从表中删除,实际上还 是利用查找算法,找到满足条件的将要删除的结点后, 注意删除过程中,使用的指针是被删除结点的直接前 驱结点的指针,直接删除即可。
2.3 线性表的链式存储结构
• 2.3.3 循环链表
– 循环链表又称为循环线性链表,其存储结构基本同单向链表,是 在单向链表的基础上加以改进形成的,它可以解决单向链表中单 方向查找的缺点。因为单向链表只能沿着一个方向,不能反向查 找,并且最后一个结点的指针域的值是null,为解决单向链表的 缺点,可以利用末尾结点的空指针完成前向查找。将单链表的末 尾结点的指针域的null变为指向第一个结点,逻辑上形成一个环 型,该存储结构称之为单向循环链表。 – 相对单链表而言,有以下的优点: – 在不增加任何空间的情况下,能够已知任意结点的地址,可以找 到链表中的所有结点(环向查找)。 – 当然在查找某个结点的前驱结点时,需要增加时间开销完成,查 找的时间复杂度是O(n)。
2.2 线性表的顺序表示和实现
• (1)顺序表
– 对线性表的所有数据元素,假设已知第一个数据元素 a1的地址为d1,每个结点占有c个存储单元, 则第i个 数据元素ai的地址为: – di=d1+(i-1)*c – 线性表的第一个数据元素的位置通常称做起始位置或 基地址。 – 线性表的这种机内表示称做线性表的顺序存储结构或 顺序映象(Sequential mapping),使用这种存储结 构存储的线性表又称做顺序表。其特点是,表中相邻 的元素之间具有相邻的存储位置。 – 在使用一维数组时,数组的下标起始位置根据给定的 问题确定,或者根据实际的高级语言的规定确定。
2.3 线性表的链式存储结构
• 线性表的顺序存储结构的特点是逻辑关系上相邻 的两个元素在物理位置上也相邻,因此随机存取 元素时比较简单,但是这个特点也使得在插入和 删除元素时,造成大量的数据元素移动,同时如 果使用静态分配存储单元,还要预先占用连续的 存储空间,可能造成空间的浪费或空间的溢出。 • 如果采用链式存储,就不要求逻辑上相邻的数据 元素在物理位置上也相邻,因此它没有顺序存储 结构所具有的弱点,但同时也失去了可随机存取 的优点。
相关文档
最新文档