Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array to implement a sorted list. Please answer the following questions.

Similar presentations


Presentation on theme: "Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array to implement a sorted list. Please answer the following questions."— Presentation transcript:

1 Data Structures -3 rd test- 2015/06/01 授課教授:李錫智

2 Question 1 Suppose we use an array to implement a sorted list. Please answer the following questions about queue: [2] How do you search for an item in the sorted list? [2] How do you insert an item into the sorted list? [2] How do you delete an item from the sorted list? [2] What is the efficiency of the insertion? Please express it in big O. [2] What is the efficiency of the search? Please express it in big O.

3 Solution of Q.1 1. 嘗試搜尋並且比較陣列裡的值,一樣時則傳回 2. 當新增一個 item 時,在要新增的位置,後面的 項目皆往後移一格 3. 當刪掉一個 item 時,在此 item 後面的項目皆往 前移一格。 4. O(n) 5. O(n) [ 若第一小題搜尋使用 binary search 答案為 O(log n)]

4 Question 2 Suppose we use a link to implement a sorted list. Please answer the following questions about queue: [2] How do you search for an item in the sorted list? [2] How do you insert an item into the sorted list? [2] How do you delete an item from the sorted list? [2] What is the efficiency of the insertion? Please express it in big O. [2] What is the efficiency of the search? Please express it in big O.

5 Solution of Q.2-1 沿著連結搜尋每一個值,找到值時則傳回。 若找到最後沒搜尋到,則回傳空值。

6 Solution of Q.2-2

7 Solution of Q.2-3

8 Solution of Q.2-4 & Q.2-5 4. O(n) 5. O(n)

9 Question 3 Please answer the following questions about queue implementations: [5] 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 items entering into the queue, respectively. What can a problem arise with this implementation? 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?

10 Solution of Q.3-1 當 queue 為 full 或 empty 時, Front 的位置,皆為 back 之下 一個

11 Solution of Q.3-2 1. 能區別 full 與 empty 狀態 2. 初始值, Front = back 即可

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

13 Solution of Q.4-1 A.h (Fig. A) B.2 h -1(Fig. B) C.2 h-1 (Fig. C) D.2 h -1(Fig. C) Fig. A Fig. B Fig. C (Complete Binary Tree)

14 Question 4-2 Please answer the following questions about trees: [3] Suppose we have a binary tree with n nodes. What is the minimal height of this tree? [2] Suppose we have a binary tree with n nodes. What is the maximal height of this tree? [2] Suppose we have a complete binary tree with n nodes. What is the minimal height of this tree? [2] Suppose we have a complete binary tree with n nodes. What is the maximal height of this tree?

15 Solution of Q.4-2

16 Question 5 Suppose we have a binary tree as shown in Figure 1. [3] Please traverse the tree in inorder. [3] Please traverse the tree in preorder. [4] Please traverse the tree in postorder. Figure 1

17 Solution of Q.5 1.60, 70, 40, 30, 20, 80, 50, 90, 10 2.30, 40, 60, 70, 50, 80, 20, 90, 10 3.70, 60, 40, 20, 80, 10, 90, 50, 30

18 Question 6 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.

19 Solution of Q.6-1 indexvalueLR 03012 1403 25045 3606 4807 5908 670 720 810 9? 10 ?11 ? root : 0 free chain start: 9

20 Solution of Q.6-2 indexvalueLR 03012 1403 25048 3606 4807 590 670 720 810 9? 10 ?11 ?5 root : 0 free chain start: 9

21 Solution of Q.6-3 indexvalueLR 03012 1403 25048 3606 4807 590 6709 720 810 9200 10?11 ?5 root : 0 free chain start: 10

22 Question 7 Suppose we have 15 integers: 35, 25, 55, 40, 70, 10, 65, 5, 20, 60, 30, 15, 50, 45, 75. [5] Please create a binary search tree for them by considering one by one. [3] Please delete the root from the resulting binary 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. [2] Continuing: 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.

23 Solution of Q.7-1 35 2555 1030 520 15 4070 50 4560 6575

24 Solution of Q.7-2 30 2555 10 520 15 4070 50 4560 6575

25 Solution of Q.7-3 30 2550 10 520 15 4070 45 60 6575

26 Question 8 Please answer the following questions with general trees: Figure 2Figure 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.

27 Solution of Q.8 30 70 805 15 9025 55 135 2 45 a b f c d h g k j e i a. b.

28 Question 9 [10] Suppose we have 15 integers: 35, 25, 55, 40, 70, 10, 65, 5, 20, 60, 30, 15, 50, 45, 75. Please sort them in ascending order by tree sort.

29 Solution of Q.9 35 2555 1030 520 15 4070 50 4560 6575 Traverse binary search tree in inorder 5,10,15,20,25,30,35,40,45,50,55,60,65,70,75

30 -End-


Download ppt "Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array to implement a sorted list. Please answer the following questions."

Similar presentations


Ads by Google