# abstract data types by luckboy

VIEWS: 72 PAGES: 6

• pg 1
```									CHAPTER 12

Abstract Data Types

Review Questions
1. An abstract data type is a data declaration packaged together with the operations that are meaningful for the data type with the implementation hidden from the user. 3. A linear list is a list in which each element has a unique successor. 5. Two common implementations of a general list are an array and a linked list. 7. A push operation adds an element to the top of the stack while a pop operation removes an element from the top of the stack. A push can put the stack in an overflow condition while a pop can put the stack in an underflow condition. 9. The enqueue operation adds an element to the rear of a queue. The dequeue operation removes the element at the front of the queue. The enqueue operation could put the queue in an overflow state while the dequeue could put the queue in an underflow state. 11. 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. 13. In a depth-first traversal, all of a vertex's descendents are processed before moving to an adjacent vertex. In a breadth-first traversal, all adjacent vertices of a vertex are processed before going to the next level. 15. A network is a graph with weighted lines.

Multiple-Choice Questions
17. 19. 21. 23. 25. 27. 29. d c a a b d a
3

4

CHAPTER 12

ABSTRACT DATA TYPES

31. 33. 35. 37. 39. 41. 43. 45. 47. 49. 51. 53.

c c d d c b a d a b b a

Exercises
55. (top) 6 5 (bottom) 57. moveStack Input: Source stack (s1) and destination stack (s2) 1. While s1 is not empty 1.1 push ( s2, pop(s1) ) End loop End 59. catStack Input: Source stack (s2) and destination stack (s1) 1. While s2 is not empty 1.1 push ( s1, pop(s2) ) End loop End 61. compareStack Input: The two stacks to compare (s1 and s2) 1. Allocate memory for two temporary stacks (Temp1 and Temp2) 2. copyStack ( s1, Temp1 ) (see #58) 3. copyStack ( s2, Temp2 ) (see #58) 4. While Temp1 is not empty AND Temp2 is not empty 4.1 TempValue1 = pop ( Temp1 ) 4.2 TempValue2 = pop ( Temp2 ) 4.3 If TempValue1 is not equal to TempValue2

SECTION

5

4.3.1 Return false End if End loop 5. If Temp1 is not empty OR Temp2 is not empty 5.1 Return false End if 6. Return true End 63. emptyQueue Input: Queue to empty (q3) 1. While q3 is not empty 1.1 dequeue( q3 ) End loop End 65. copyQueue Input: Source queue (q2) and destination queue (q3) 1. Allocate memory for a temporary queue (Temp) 2. While q2 is not empty 2.1 enqueue ( Temp, dequeue(q2) ) End loop 3. While Temp is not empty 3.1 TempValue = dequeue(Temp) 3.2 enqueue ( q2, TempValue ) 3.3 enqueue ( q3, TempValue ) End loop End 67. compareQueue Input: The two queues to compare (q1 and q2) 1. Allocate memory for two temporary queues (Temp1 and Temp2) 2. copyQueue ( q1, Temp1 ) (see #65) 3. copyQueue ( q2, Temp2 ) (see #65) 4. While Temp1 is not empty AND Temp2 is not empty 4.1 TempValue1 = dequeue ( Temp1 ) 4.2 TempValue2 = dequeue ( Temp2 ) 4.3 If TempValue1 is not equal to TempValue2 4.3.1 Return false End if

6

CHAPTER 12

ABSTRACT DATA TYPES

End loop 5. If Temp1 is not empty OR Temp2 is not empty 5.1 Return false End if 6. Return true End 69. See Figure 12.1 Figure 12.1 Exercise 69

J C B A E D F G I H

71. This tree cannot be drawn because it is not a valid binary tree. Node C must be the root because it is listed last in the postorder traversal. From the inorder traversal, we see that nodes A, B, and D must be in the left subtree (because they are listed to the left of the root) and that nodes E, F, and G are in the right subtree. Looking back at the postorder list, however, we see that nodes G and F are listed first, which is not possible. 73. Assuming that arcs are stored in sequence by their destination, the traversal is: A, G, F, H, D, E, C, B 75. See Figure 12.2. Figure 12.2 Exercise 75
A A B C D E F G H A B C D E F G H 0 4 3 0 0 0 1 0 B 4 0 0 0 3 0 0 0 C 3 0 0 8 0 5 0 0 D 0 0 8 0 0 0 0 5 E 0 3 0 0 0 0 6 0 F 0 0 5 0 0 0 2 7 G 1 0 0 0 6 2 0 0 H 0 0 0 5 0 7 0 0

Vertex Vector

Adjacency Matrix

SECTION

7

77. See Figure 12.3. Figure 12.3 Exercise 77

A
2 3 1 3 6 4 2 2 3 6

B

F

E
1

1 2 2 1

C

D

8

CHAPTER 12

ABSTRACT DATA TYPES

```
To top