Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures -3 rd exam- 授課教授:李錫智. 1.[10] Suppose we want to implement a queue by using the following List operations: getLength(); remove(position);

Similar presentations


Presentation on theme: "Data Structures -3 rd exam- 授課教授:李錫智. 1.[10] Suppose we want to implement a queue by using the following List operations: getLength(); remove(position);"— Presentation transcript:

1 Data Structures -3 rd exam- 授課教授:李錫智

2 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 ); }

3 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 step312342 step42302 step52302 step623403 step73413 step8423 step94524 step1064520

4 [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

5 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

6 4.[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

7 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 05012 1603 27045 3806 41007 51108 690 740 830 9? 10 ?11 ?

8 [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 05012 1603 27048 3806 41007 51109 690 740 830 9? 10 ?11 ?

9 [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 05012 16035 27048 3806 41007 5200 690 740 830 9? 10 ?11 ?

10 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: 65 25 30 570 7535 15 10 20 55 4060 50 45

11 [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 25 30 570 7535 15 10 20 55 4060 50 45 65 25 30 570 7535 15 10 20 50 4060 45

12 7.[10] Please answer the following questions with general trees: Figure 2 Figure 3

13 [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: 45 85 20 105 50 95 70 16 17 60 40 30 a b c d e f g k j ih

14 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:

15 [5] Please create two different binary trees which have an inorder traversal as above. ANS:

16 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: 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

17 [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

18 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: 70103565255540 655510253570 35405510256570 25403510556570 10253540556570 10253540556570 10253540556570 10253540556570


Download ppt "Data Structures -3 rd exam- 授課教授:李錫智. 1.[10] Suppose we want to implement a queue by using the following List operations: getLength(); remove(position);"

Similar presentations


Ads by Google