Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array...

30
Data Structures -3 rd test- 2015/06/01 授授授授 授授授

Transcript of Data Structures -3 rd test- 2015/06/01 授課教授:李錫智. Question 1 Suppose we use an array...

Page 1: 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.

Data Structures-3rd test-

2015/06/01

授課教授:李錫智

Page 2: 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.

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.

Page 3: 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.

Solution of Q.1

1. 嘗試搜尋並且比較陣列裡的值,一樣時則傳回2. 當新增一個 item 時,在要新增的位置,後面

的項目皆往後移一格3. 當刪掉一個 item 時,在此 item 後面的項目皆

往前移一格。4. O(n)

5. O(n) [ 若第一小題搜尋使用 binary search 答案為 O(log n)]

Page 4: 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.

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.

Page 5: 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.

Solution of Q.2-1

• 沿著連結搜尋每一個值,找到值時則傳回。 若找到最後沒搜尋到,則回傳空值。

Page 6: 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.

Solution of Q.2-2

Page 7: 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.

Solution of Q.2-3

Page 8: 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.

Solution of Q.2-4 & Q.2-5

4. O(n)

5. O(n)

Page 9: 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.

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?

Page 10: 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.

Solution of Q.3-1

• 當 queue 為 full 或 empty 時, Front 的位置,皆為 back之下一個

Page 11: 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.

Solution of Q.3-2

1. 能區別 full 與 empty 狀態2. 初始值, Front = back 即可

Page 12: 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.

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?

Page 13: 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.

Solution of Q.4-1

A. h (Fig. A)

B. 2h -1(Fig. B)

C. 2h-1 (Fig. C)

D. 2h -1(Fig. C)

Fig. A Fig. B

Fig. C (Complete Binary Tree)

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

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?

Page 15: 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.

Solution of Q.4-2

1. +1 (Fig. B)

2. n (Fig. A)

3. +1 (Fig. C)

4. +1 (Fig. C)

※ 註解 n , h 為整數 2h

-1 2h-1 -1

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

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

Page 17: 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.

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

Page 18: 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.

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.

Page 19: 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.

Solution of Q.6-1

index value L R

0 30 1 2

1 40 3 -1

2 50 4 5

3 60 -1 6

4 80 7 -1

5 90 -1 8

6 70 -1 -1

7 20 -1 -1

8 10 -1 -1

9 ? -1 10

10 ? -1 11

11 ? -1 -1

root : 0free chain start: 9

Page 20: 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.

Solution of Q.6-2

index value L R

0 30 1 2

1 40 3 -1

2 50 4 8

3 60 -1 6

4 80 7 -1

5 90 -1 -1

6 70 -1 -1

7 20 -1 -1

8 10 -1 -1

9 ? -1 10

10 ? -1 11

11 ? -1 5

root : 0free chain start: 9

Page 21: 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.

Solution of Q.6-3

index value L R

0 30 1 2

1 40 3 -1

2 50 4 8

3 60 -1 6

4 80 7 -1

5 90 -1 -1

6 70 9 -1

7 20 -1 -1

8 10 -1 -1

9 200 -1 -1

10 ? -1 11

11 ? -1 5

root : 0free chain start: 10

Page 22: 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.

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.

Page 23: 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.

Solution of Q.7-135

25 55

10 30

5 20

15

40 70

50

45 60

65 75

Page 24: 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.

Solution of Q.7-230

25 55

10

5 20

15

40 70

50

45 60

65 75

Page 25: 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.

Solution of Q.7-330

25 50

10

5 20

15

40 70

45

60

65 75

Page 26: 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.

Question 8

• 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.

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

Page 27: 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.

Solution of Q.830

70

805

15 9025

551 35

2 45

a

b f

c

d

hg k

j

e i

a. b.

Page 28: 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.

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.

Page 29: 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.

Solution of Q.935

25 55

10 30

5 20

15

40 70

50

45 60

65 75

Traverse binary search tree in inorder 5,10,15,20,25,30,35,40,45,50,55,60,65,70,75

Page 30: 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.

-End-