Data Structures -3 rd exam- 授課教授:李錫智
1.[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 ); }
2.[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: array frontback index01234 initial44 step1140 step21241 step step42302 step52302 step step73413 step8423 step94524 step
[5] We adopt the linked-based implementation for the queue. Please show the content of the list, frontPtr, backPtr after each operation. ANS: Fptr1Bptr Fptr12Bptr Fptr123Bptr Fptr23Bptr Fptr23Bptr Fptr234Bptr Fptr34Bptr Fptr4Bptr Fptr45Bptr Fptr456Bptr
3.[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
4.[10] Suppose we have a binary tree as shown in Figure 1. Figure 1 [3] Please traverse the tree in preorder. ANS: [3] Please traverse the tree in inorder. ANS: [4] Please traverse the tree in postorder. ANS:
5.[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 indexleftright ? 10 ?11 ?
[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 indexleftright ? 10 ?11 ?
[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 indexleftright ? 10 ?11 ?
6.[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:
[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:
7.[10] Please answer the following questions with general trees: Figure 2 Figure 3
[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: a b c d e f g k j ih
8.[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:
9.[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: [4] Continuing: Please remove the root. Show the resulting heap. ANS:
[3] Continuing: Please add 25 into it. Show the resulting heap. ANS:
10.[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: