已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1 CHAPTER 12 Abstract Data Types (Solutions to Practice Set) Review Questions 1. An abstract data type (ADT) is a data declaration packaged together with the oper- ations that are meaningful for the data type. In an ADT, the operations used to access the data are known, but the implementation of the operations are hidden. 2. A stack is a restricted linear list in which all additions and deletions are made at one end, called the top. If we insert a series of data into a stack and then remove it, the order of the data will be reversed. This reversing attribute is why stacks are known as a last in, first out (LIFO) data structure. Four basic stack operations defined in this chapter are stack, push, pop, and empty. 3. A queue is a linear list in which data can only be inserted at one end, called the rear, and deleted from the other end, called the front. These restrictions ensure that the data are processed through the queue in the order in which they are received. In other words, a queue is a first in, first out (FIFO) structure. Four basic queue oper- ations defined in this chapter are queue, enqueue, dequeue, and empty. 4. A general linear list is a list in which operations, such as insertion, can be done anywhere in the list, that is, at the beginning, in the middle, or at the end of the list. Six common general linear list operations defined in this chapter are list, insert, delete, retrieve, traverse, and empty. 5. A tree consists of a finite set of elements, called nodes (or vertices), and a finite set of directed lines, called arcs, that connect pairs of the nodes. If the tree is not empty, one of the nodes, called the root, has no incoming arcs. The other nodes in a tree can be reached from the root following a unique path, which is a sequence of consecutive arcs. A binary tree is a tree in which no node can have more than two subtrees. A binary search tree (BST) is a binary tree with one extra property: the key value of each node is greater than the key values of all nodes in each left sub- tree and smaller than the value of all nodes in each right subtree. 6. A depth first traversal processes all of the nodes in one subtree before processing all of the nodes in the other subtree. In a breadth first traversal, all the nodes at one level are processed before moving on to the next level. 2 7. A graph is an ADT made of a set of nodes, called vertices, and set of lines connect- ing the vertices, called edges or arcs. Graphs may be either directed or undirected. In a directed graph, or digraph, each edge, which connects two vertices, has a direction (arrowhead) from one vertex to the other. In an undirected graph, there is no direction. 8. Four stack applications are: reversing data, pairing data, postponing data usage, and backtracking steps. 9. General linear lists are used in situations where the elements are accessed ran- domly or sequentially. For example, in a college, a linear list can be used to store information about the students who are enrolled in each semester. 10. Binary trees have many applications in computer sciences, two of which are Huff- man coding and expression trees. Binary search trees are used when we want to have a list that can be searched using an efficient search algorithm such as binary search. Multiple-Choice Questions Exercises 26. 27. 11. b12. d13. b14. b15. d16. a 17. a18. c19. c20. c21. b 22. c 23. a24. d25. b while (NOT empty (S2) pop (S2, x)/ x will be discarded stack(Temp) while (NOT empty (S1) pop (S1, x) push (Temp, x)/ Temp is a temporary stack while (NOT empty (Temp) pop (Temp, x) push (S2, x) 3 28. 29. 30. Figure S11.30 shows the contents of the stack and the value of the variables. 31. Algorithm S12.31 shows the pseudocode. stack(Temp) while (not empty (S1) pop (S1, x) push (Temp, x)/ Temp is a temporary stack while (not empty (Temp) pop (Temp, x) push (S1, x) push (S2, x) stack(Temp) while (NOT empty (S2) pop (S2, x) push (Temp, x)/ Temp is a temporary stack while (NOT empty (Temp) pop (Temp, x) push (S2, x) Figure S11.30Exercise 30 Algorithm S12.31Exercise 47 Algorithm: Palindrome(String1 n) Purpose: It checks if a string is a palindrome Pre: Given: a string Post: Return: true (the string is a palindrome) or false (the string is not a palindrome) stack (S) i 1 while i n S1S1S1S1S1S1S1 x2y3 5 3 5 2 3 5 3 5 6 55 4 32. Algorithm S12.32 shows the pseudocode. Algorithm S12.31Exercise 31 (Continued) C stringi push (S, C) i i + 1 i 1 while i n pop (S, x) if (x stingi)return false return true Algorithm S12.32Exercise 32 Algorithm: CompareStack(S1, S2) Purpose: Check if two stacks are the same Pre: Given: S1 and S2 Post: Return: true (S1 = S2) or false (S1 S2) flag true Stack (Temp1) Stack (Temp2) while (NOT empty (S1) and NOT empty (S2) pop (S1, x) push (Temp1, x) pop (S2, y) push (Temp2, y) if (x y) flag false if (NOT empty (S1) or NOT empty (S2) flag false while (NOT empty (Temp1) and NOT empty (Temp2) pop (Temp1, x) push (S1, x) pop (Temp2, y) push (S2, y) return flag 5 33. 34. 35. 36. while (NOT empty (Q) dequeue (Q, x)/ x will be discarded while (NOT empty (Q1) dequeue (Q1, x) enqueue (Q2, x) while (NOT empty (Q2)/ First we empty Q2. dequeue (Q2, x) while (NOT empty (Q1) dequeue (Q1, x) enqueue (Temp, x) while (NOT empty (Temp) dequeue (Temp, x) enqueue (Q1, x) enqueue (Q2, x) while (NOT empty (Q2) dequeue (Q2, x) enqueue (Q1, x) 6 37. Algorithm S12.37 shows the pseudocode. 38. a. Since traversal is postorder, the root comes at the end: G b. Since the traversal is preorder, the root comes at the beginning: I c. Since traversal is postorder, the root comes at the end: E 39. The preorder traversal JCBADEFIGH tells us that node J is the root. The inorder traversal ABCEDFJGIH implies that nodes ABCEDF (in the left of J) are in the Algorithm S12.37Exercise 37 Algorithm: CompareQueue(Q1, Q2) Purpose: Check if two queues are the same Pre: Given: Q1 and Q2 Post: Return: true (Q1 = S2) or false (Q1 S2) flag true Queue(Temp1) Queue(Temp2) while (NOT empty (Q1) OR NOT empty (Q2) if (NOT empty (Q1) dequeue (Q1, x) enqueue (Temp1, x) if (NOT empty (Q2) dequeue (Q2, y) enqueue (Temp2, y) if (x y) flag false if (NOT empty (Q1) XOR NOT empty (Q2) flag false while (NOT empty (Temp1) OR NOT empty (Temp2) if (NOT empty (Temp1) dequeue (Temp1, x) enqueue (Q1, x) if (NOT empty (Temp2) dequeue (Temp2, y) enqueue (Q2, y) return flag 7 left subtree and nodes GIH (in the right of J) are in the right subtree. Following the same logic for each subtree we build the binary tree as shown in Figure S11.39. 40. The postorder traversal FECHGDBA tells us that node A is the root. The Inorder traversal FECABHDG implies that nodes FEC in the left of A are in the left sub- tree and nodes BHDG in the right of A are in the right subtree. Following the same logic for each subtree we build the binary tree as shown Figure S11.40 41. The postorder traversal GFDABEC tells us that node C is the root. The inorder tra- versal ABDCEFG tell us that nodes ABD (in the left of C) are in the left subtree and nodes EFG (in the right of A) are in the right subtree (Figure S11.41). We can Figure S11.39Exercise 39 Figure S11.40Exercise 40 GD A B C EF J I H J CI GH ABEDF J ABCEDFGIH a. Step 1b. Step 2 c. Final tree G D A BC E FH C B FEHDG A A FECBHDG a. Step 1b. Step 2 c. Final tree 8 decompose the left subtree into two nodes, but the right subtree cannot be decom- posed because nodes EFG are not contiguous in the postorder traversal. We cannot find the root of this subtree. There are some errors in the postorder traversal listing. 42. Algorithm S12.42 shows the pseudocode. Figure S11.41 Algorithm S12.42Exercise 42 Algorithm: StackADTArrayImplementation Purpose: Implementing stack operations with an array Allocation: An array of size n is allocated stack (Stack S)/ Stack operation allocate record S of two fields S.top 0 S.count 0 push (Stack S, DataRecord x)/ Push operation S.top S.top +1 S.count S.count + 1 AS.top x pop (Stack S, DataRecord x)/ Pop operation x AS.top S.top S.top 1 S.count S.count 1 empty (Stack S)/ Empty operation if (S.count = 0) return true else return false B AD C A ABDEFG EFG a. Step 1b. Step 2 9 43. Algorithm S12.43 shows the pseudocode. Algorithm S12.43Exercise 43 Algorithm: StackADTLinkedListImplementation Purpose: Implementing stack operations with linked list stack (Stack S)/ Stack operation allocate record S of two fields S.top null S.count 0 push (Stack S, DataRecord x)/ Push operation Allocate a node and a new pointer new address of the allocated node (*new).data x (*new).link null if (S.top = null) S.top new else (*new).link S.top S.top new S.count S.count + 1 pop (Stack S, DataRecord x)/ Pop operation x *(S.top).data S.top *(S.top).link S.count S.count 1 empty (Stack S)/ Empty operation if (S.count = 0) return true else return false 10 44. Algorithm S12.44 shows the pseudocode. Algorithm S12.44Exercise 44 Algorithm: QueueADTArrayImplementation Purpose: Implementing queue operations with an array Allocation: An array of size n is allocated queue (Queue Q)/ Queue operation allocate record Q of three fields Q.count 0 Q.rear 0 Q.front 0 enqueue (Queue Q, DataRecord x)/ Enqueue operation if (Q.front = 0) Q.front 1 Q.count Q.count +1 Q.rear Q.rear + 1 AQ.rear x dequeue (Queue Q, DataRecord x)/ Dequeue operation x AQ.front Q.front Q.front + 1 Q.count Q.count 1 empty (Queue Q)/ Empty operation if (Q.count = 0) return true else return false 11 45. Algorithm S12.45 shows the pseudocode. Algorithm S12.45Exercise 45 Algorithm: QueueADTLinkedListImplementation Purpose: Implementing queue operations with linked list queue (Queue Q)/ Queue operation allocate record Q of three fields Q.count 0 Q.front null Q.rear null enqueue (Queue Q, DataRecord x)/ enqueue operation Allocate a node and a new pointer new address of the allocated node (*new).data x (*new).link null if (Q.count = 0) Q.front new Q.rear new else if (Q.count = 1) (*front).link new rear (*front).link else (*rear).link new Q.count Q.count + 1 dequeue (Queue Q, DataRecord x)/ Dequeue operation x AQ.front if (Q.count = 1) Q.front null Q.rear null else if (Q.count = 1) (*front).link new rear (*front).link else front (*front).link Q.count Q.count 1 empty (Queue Q)/ Empty operation if (Q.count = 0) return true else return false 12 46. Algorithm S12.46 shows the pseudocode. Algorithm S12.46Exercise 46 Algorithm: ListADTArrayImplementation Purpose: Implementing list operations with an array Allocation: An array of size n is allocated Include BinarySearchArray algorithm from chapter 11, exercise 25 here Include ShiftDown algorithm from chapter 11, exercise 26 here list (List L)/ List operation allocate record L of two fields L.count 0 L.first 0 insert (List L, DataRecord x)/ Insert operation BinarySearchArray (A, n, x.key, flag, i) ShiftDown (A, n, i) Ai x if (empty (L) L.first 1 L.count L.count + 1 delete (List L, DataRecord x)/ Delete operation BinarySearchArray (A, n, x.key, flag, i) x Ai ShiftUp (A, n, i) L.count L.count 1 if (empty (L) L.first 0 retrieve (List L, DataRecord x)/ Retrieve operation BinarySearchArray (A, n, x.key, flag, i) x Ai traverse (List L, Process)/ Traverse operation walker 1 while (walker L.count) Process (Awalker walker walker + 1 empty (L)/ Empty operation if (L.count = 0) return true else return false 13 47. Algorithm S12.47 shows the pseudocode. Algorithm S12.47Exercise 47 Algorithm: ListADTLinkedListImplementation Purpose: Implementing list operations with a linked list Include SearchLinkedList algorithm from chapter 11 list (List L)/ List operation allocate record L of two fields
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年江西省水利投资集团有限公司中层管理人员招聘备考题库含答案详解
- 2025年高职会计(财务分析)试题及答案
- 2025年中职第三学年(房地产市场调研)市场分析阶段测试题及答案
- 2025年中职(环境监测技术)环境检测阶段测试题及答案
- 2025年大学二年级(税收学)税务筹划综合测试题及答案
- 2025年大学服装效果图(电脑绘图技巧)试题及答案
- 2025年中职烹饪工艺与营养(蒸菜制作工艺)试题及答案
- 2025年中职城市水利(城市水利工程)试题及答案
- 2025年高职数字媒体艺术设计(展示设计)试题及答案
- 2026年电脑维修(病毒查杀方法)试题及答案
- 思想政治教育研究课题申报书
- 开发区再生水资源化利用建设项目可行性研究报告
- 知识产权法考试重点复习资料
- 区域创新一体化机制-洞察及研究
- 2025年人卫基础护理学第七版试题及答案
- 2025至2030聚氯乙烯(PVC)土工膜行业产业运行态势及投资规划深度研究报告
- 航天信息股份有限公司笔试题
- 网上家居商城项目设计汇报
- 2025吉林检验专升本试题及答案
- 普外科科室主任工作汇报
- 新疆概算管理办法
评论
0/150
提交评论