180 likes | 279 Views
Data Structures -3 rd exam-. 授課教授:李錫智. [10] Suppose we want to implement a queue by using the following List operations: getLength (); remove(position); getEntry (position); setEntry ( position,newEntry ), insert( newPosition , newEntry ).
E N D
Data Structures-3rd exam- 授課教授:李錫智
[10] Suppose we want to implement a queue by using the following List operations: getLength(); remove(position); getEntry(position); setEntry(position,newEntry), insert(newPosition, newEntry). Please describe clearly how to realize the following operations: • [5] enqueue(item) • [5] dequeue(). ANS: 1. void enqueue(item){ insert( getLength() + 1 , item ); } 2. void dequeue(){ if ( getLength() > 0 ){ remove( 1 ); } }
[10] Suppose we want to do the following operations in sequence on an initially empty queue: enqueue(1), enqueue(2), enqueue(3), dequeue(), peekFront(), enqueue(4), dequeue(), dequeue(), enqueue(5), enqueue(6),. Please answer the following questions: • [5] We use a circular array A of five elements to implement the queue. Please show the content of array A, front, and back after each operation. Please note that your answer should be a correct implementation. ANS:
[5] We adopt the linked-based implementation for the queue. Please show the content of the list, frontPtr, backPtr after each operation. ANS:
[10] Please answer the following questions about trees: • [2] Suppose we have a binary tree with height 5. What is the minimal number of nodes in this tree? ANS: 5 • [2] Suppose we have a binary tree with height 5. What is the maximal number of nodes in this tree? ANS: 31 • [2] Suppose we have a complete binary tree with height 5. What is the minimal number of nodes in this tree? ANS: 16 • [2] Suppose we have a binary tree with 30 nodes. What is the minimal height of this tree? ANS: 5 • [2] Suppose we have a binary tree with 30 nodes. What is the maximal height of this tree? ANS: 30
[10] Suppose we have a binary tree as shown in Figure 1. Figure 1 • [3] Please traverse the tree in preorder. ANS: 50 60 80 90 70 100 40 110 30 • [3] Please traverse the tree in inorder. ANS: 80 90 60 50 40 100 70 110 30 • [4] Please traverse the tree in postorder. ANS: 90 80 60 40 100 30 110 70 50
[10] Please answer the following questions about trees: • [4] Store the tree of Figure 1 in an array of size 12. Please show the resulting array. Note that the nodes are stored level by level and from left to right. ANS: root = 0 free chain start = 9
[3] Continuing: Suppose we delete 110 from the tree. Please show the resulting array. Please add the newly released space as the first place of the free chain. Please link the parent of 110 directly to the child of 110. ANS: root = 0 free chain start = 5
[3] Continuing: Suppose we add 200 as the right child of 60. Please show the resulting array. Store it in the first available space. ANS: root = 0 free chain start = 9
[10] Suppose we have 15 integers: 65, 5, 35, 25, 55, 20, 40, 50, 45, 70, 10, 60, 30, 15, 75. • [3] Please create a binary search tree for them by adding the integers one by one to the tree, starting with an empty tree. ANS: 65 5 70 35 75 25 55 20 30 40 60 10 50 15 45
[4] How do you save this binary search tree in an array and reconstruct the same tree later? ANS: save the tree by traversing the tree in preorder. 65, 5, 35, 25, 20, 10, 15, 30, 55, 40, 50, 45, 60, 70, 75 and reconstruct it as a binary search tree • [3] Please delete 55 from the tree and show the resulting binary search tree. Note that a node is replaced by one that is the largest of those smaller than it. ANS: 65 65 5 5 70 70 35 35 75 75 25 25 50 55 20 20 30 30 40 40 60 60 10 10 50 45 15 15 45
Figure 2 Figure 3 • [10] Please answer the following questions with general trees:
60 17 16 70 50 40 30 105 20 95 j g b k f e h c a i d 85 45 • [5] Figure 2 is a general tree. Please draw a corresponding binary tree for it. ANS: • [5] Figure 3 is a binary tree intended for storing a general tree. Please draw the general tree it represents. ANS:
[10] Suppose we have 8 integers: 35, 25, 55, 40, 70, 10, 65, 5. • [5] Please create two different binary trees which have a preorder traversal as above. ANS:
[5] Please create two different binary trees which have an inorder traversal as above. ANS:
[10] Suppose we have a heap represented in an array shown below: 20, 16, 17, 12, 13, 7, 15, 5, 2 • [3] Please add 19 into it. Show the resulting heap. ANS: 20 16 17 12 13 7 15 5 2 19 20 16 17 12 19 7 15 5 2 13 20 19 17 12 16 7 15 5 2 13 • [4] Continuing: Please remove the root. Show the resulting heap. ANS: 13 19 17 12 16 7 15 5 2 19 13 17 12 16 7 15 5 2 19 16 17 12 13 7 15 5 2
[3] Continuing: Please add 25 into it. Show the resulting heap. ANS: 19 16 17 12 13 7 15 5 2 25 19 16 17 12 25 7 15 5 2 13 19 25 17 12 16 7 15 5 2 13 25 19 17 12 16 7 15 5 2 13
[10] Suppose we have 7 integers: 70, 10, 35, 65, 25, 55, 40. They are stored initially in an array of size 7. Please sort them in ascending order using heap sort. Please draw the array each time and as soon as the root of the heap has been switched to the appropriate position. ANS: