Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures -3 rd exam- 授課教師 : 李錫智 教授. 1.[10] Please answer the following questions about queue: [ 2] What is the response of the statement: “(new.

Similar presentations


Presentation on theme: "Data Structures -3 rd exam- 授課教師 : 李錫智 教授. 1.[10] Please answer the following questions about queue: [ 2] What is the response of the statement: “(new."— Presentation transcript:

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

2 1.[10] Please answer the following questions about queue: [ 2] What is the response of the statement: “(new queue()).isEmpty()”? [2] What is the response of the statement: “((new queue()).enqueue(item)).peekFront()”? [2] What is the response of the following statement: “(((new queue()).enqueue(item)).peekFront()).dequeue()”? [2] What is the response of the following statement: “((((new queue()).enqueue(item)).peekFront()).dequeue()).enqueue(item)” ? [2] What is the response of the following statement: “((((new queue()).enqueue(item)).peekFront()).dequeue()).dequeue()”?

3 Ans: (a)true (b)item (c)true (d)true (e)false

4 2.[10] Please answer the following questions about queue implementations: Suppose we use a circular array to implement a queue. Let front and back are two variables storing the indices of the first and last objects entering into the queue, respectively.[3] What can a problem arise with this implementation? [2] How do you solve it? Suppose we use a circular array to implement a queue. Let front be a variable storing the index prior to that of the first object entering into the queue, and back be another variable storing the index of the last object entering into the queue. [3] What is the advantage of this implementation? [2] How are these variables set initially?

5 Ans: (a) problem: 當 circular queue 為空的或滿的, front 都等於 back+1 ,無法由判斷 circular queue 目前的情況 solve: 令一額外變數儲存目前 circular queue 裡面的個數 (b) Advantage: 不用再用額外變數儲存目前 circular queue 裡 面的個數,直接可藉由 front 和 back 判斷 circular queue 的 狀況 Initial set: front=back

6 3.[10] Please answer the following questions about trees: Suppose we have a binary tree with height h. [1] What is the minimal number of nodes in this tree? [2] What is the maximal number of nodes in this tree? Suppose we have a complete binary tree with height h. [2] What is the minimal number of nodes in this tree? [2] What are the maximal number of nodes in this tree? Suppose we have a binary tree with n nodes. [2] What is the minimal height of this tree? [1] What is the maximal height of this tree?

7 Ans: (a) h,2 h -1 (b) 2 h-1, 2 h -1 (c) Log 2 (n+1),n

8 4.[10] Suppose we have a binary tree as shown in Figure 1. Fig.1 [3] Please traverse the tree in inorder. [3] Please traverse the tree in preorder. [4] Please traverse the tree in postorder.

9 Ans: Inorder:60,70,40,30,20,80,50,90,10 Preorder:30,40,60,70,50,80,20,90,10 Postorder:70,60,40,20,80,10,90,50,30

10 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. [3] Continuing: Suppose we delete 90 from the tree. Please show the resulting array. Please add the newly released space at the end of the free chain. Please link the parent of 90 directly to the child of 90. [3] Continuing: Suppose we add 200 as the left child of 70. Please show the resulting array. Store it in the first available space.

11 Ans:(a)

12 (b)

13 (c)

14 6.[10] Suppose we have 15 integers: 35, 25, 55, 40, 70, 10, 65, 5, 20, 60, 30, 15, 50, 45, 75. [3] Please create a binary search tree for them by considering one by one. [4] How do you save this binary search tree in an array and reconstruct the same tree later? [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.

15 35 25 55 10 30 5 20 15 40 70 506575 4560 Ans: (a)

16 (b) 先用 preorder: 之後再建一次 binary search tree

17 35 25 50 10 30 5 20 15 40 70 456575 60 (c)

18 7.[10] Please answer the following questions with general trees: Fig. 2

19 Fig. 3 [5] Figure 2 is a general tree. Please draw a corresponding binary tree for it. [5] Figure 3 is a binary tree intended for storing a general tree. Please draw the general tree it represents.

20 30 70 5 80 15 25 1 90 35 55 2 45 Ans:(a)

21 a bfj ceg h i d k (b)

22 8.[10] Suppose we have 15 integers: 35, 25, 55, 40, 70, 10, 65, 5, 20, 60, 30, 15, 50, 45, 75. Please create a heap, starting with a complete binary tree with 15 nodes. Show the tree each time one level is completed during the adjustment process.

23 35 25 55 4070 5075 520 60 30 15104565 Ans: Level1

24 35 70 75 4060 5065 520 25 30 15104555 Level2

25 75 70 65 4060 5055 520 25 30 15104535 Level3

26 9.[10] Suppose we have a heap represented in an array shown below: 20, 17, 16, 13, 12, 15, 7, 2, 5 [3] Please add 19 into it. Show the resulting heap. [3] Continuing: Please add 25 into it. Show the resulting heap. [4] Continuing: Please remove the root. Show the resulting heap.

27 Ans:

28 10.[10] Suppose we have 7 integers: 35, 25, 55, 40, 70, 10, 65. Please sort them in ascending order using heap sort. Please using an array of size 7 to implement it. Please draw the array each time the root of the heap is removed.

29 Ans:


Download ppt "Data Structures -3 rd exam- 授課教師 : 李錫智 教授. 1.[10] Please answer the following questions about queue: [ 2] What is the response of the statement: “(new."

Similar presentations


Ads by Google