Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have...

28
Data Structures -Final exam- 2015/6/22 授授授授 授授授

Transcript of Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have...

Page 1: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Data Structures-Final exam-

2015/6/22

授課教授:李錫智

Page 2: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Question 1.

• Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101, 24, 34, 45, 16.

• Let the hash function be h(x) = x%13 and the linear probing be used for resolving collisions.

• (a) Insert these keys into the hash table in order. Show the hash table after the insertions.

• (b) Please describe the process of finding 45 from the hash table.

• (c) Please describe the process of inserting 46 into the hash table.

Page 3: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q1.

• (a)

• (b) 先檢查 index: 45%13=6, 若 index 6 存放的integer keys 不為 45 ,則檢查 index: (6+i)%13, i為 1~12 ,直至找到 45 或空的 index 為止

• (c) 46%13=7 ,檢查 index 7 是否有空位,若沒有空位則檢查 index 8 ,直至找到空位或找完為止

80 68 16 58 72 34 100 101 24 45

Page 4: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Question 2.

• Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101, 24, 34, 45, 16.

• Let the hash function be h(x) = x%13 and the quadratic probing be used for resolving collisions.

• (a) Insert these keys into the hash table in order. Show the hash table after the insertions.

• (b) Please describe the process of finding 45 from the hash table.

• (c) Please describe the process of inserting 46 into the hash table.

Page 5: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q2.

• (a)

• (b) 45%13=6 ,比對 hash table index 為 (6+n2 )%13 的位址內是否為 45 ,其中 n=0, 1, 2, …. ,若是 45 則回傳 (6+n2 ) % 13 就是 45 的 hash table 位址

• (c) 46%13=7 ,尋找 hash table index 為 (7+n2 ) % 13 的位址,其中 n=0, 1, 2, …. ,直到找到第一個空的位址或找完為止

80 68 16 45 58 72 34 100 101 24

Page 6: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Question 3.

• Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101, 24, 34, 45, 16.

• Let double hashing be used for resolving collisions and the hash functions be h1(x) = x%13 and h2(x) = 7 – (x%7).

• (a) Insert these keys into the hash table in order. Show the hash table after the insertions.

• (b) Please describe the process of finding 45 from the hash table.

• (c) Please describe the process of inserting 46 into the hash table.

Page 7: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q3.

• (a)

• (b) 先檢查 index: h1(45)= 45%13=6 ,若沒找到則檢查 h1(45)+i*h2(45)=6+i*4, 其中 i=1, 2, … 直到找到 45 或空位為止

• (c) 先檢查 index: h1(46)= 46%13=7 是否有空位,若沒有則檢查 h1(46)+i*h2(46)=6+i*3, 其中 i=1, 2, … 直到找到空位或找完為止

16 45 80 68 58 72 34 100 101 24

Page 8: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Question 4.

• Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101, 24, 34, 45, 16.

• Let the hash function be h(x) = x%13 and the separate chaining be used for resolving collisions. Note that a key is inserted as the head of a link.

• (a) Insert these keys into the hash table in order. Show the hash table after the insertions.

• (b) Please describe the process of finding 45 from the hash table.

• (c) Please describe the process of inserting 46 into the hash table.

Page 9: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q4.

0

1

2

3

4

5

6

7

8

9

10

11

12

80

16 68

45 58

72

34

100

101

24

(a)

(b)45%13=6 ,在 index 6 的 link 上依次尋找45 ,直到找到或找完

(c)46%13=7 ,在 index 7 的 head 端接上 46 ,並將原先存於 head 端的 index 接到 46 之後

Page 10: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Question 5.

• Suppose we have 20 integer keys: 50, 90, 20, 70, 120, 150, 10, 30, 40, 60, 80, 100, 110, 130, 140, 160, 170, 180, 190, 200.

• (a) Please construct a binary search tree and show the resulting tree.

• (b) Please construct a 2-3 tree by inserting the keys one by one. Show the tree each time the height of the tree increases by one. Also, please show the final tree.

• (c) Please construct an AVL tree by inserting the keys one by one. Show the tree each time the height of the tree increases by one.

Page 11: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q5-a.

• (a)

50

20

10 30

40

90

70

60 80

120

100

110

150

160

170

180

190

200

130

140

Page 12: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q5-b.

501.

90

50

2. 50

20 90

10 30 70 120 150

3.

90

50 140

20 70 120 160

10

30,40

60 80

100,110

130150 170

4.90

50 140

20 70 120 160 180

10

30,40

60 80

100,110

130150 170

190,200

5.

20

Page 13: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q5-c.

501.

90

50

2. 50

20 90

70

3. 90

50 120

20 70 150

10

4.

50

30 90

2040 70

120

10

6080

100 150

5.

110

12050 160

30

40

90

18020

70 170 190

6.

10

60 80

140

100

110 200

130 150

Page 14: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Question 6.

• Suppose we have an 2-3 tree shown in Figure 1. Let a deleted item be replaced by the smallest of its right child. Please do the following:

• (a) From Figure 1, delete 130 and show the resulting tree.

• (b) From Figure 1, delete 10 and show the resulting tree.

• (c) From Figure 1, delete 160 and show the resulting tree.

• (d) From Figure 1, delete 120 and show the resulting tree.

• (e) From Figure 1, delete 60 and show the resulting tree.

Page 15: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

50, 90

20

10

70

30, 40 60 80

120, 150

160140100, 110

(a)

Solution to Q6-a.

Page 16: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

50, 90

30

20

70

40 60 80

120, 150

160130, 140100, 110

6.(b)

50, 90

20

10

70

30, 40 60 80

120, 140

150130100, 110

6.(c)

Solution to Q6-b.

Page 17: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

50, 90

20

10

70

30, 40 60 80

130, 150

160140100, 110

6.(d)

90

20, 50

10

30, 40

120, 150

160130, 140100, 110

6.(e)

70, 80

Solution to Q6-c.

Page 18: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Question 7.

• Consider Figure 2. and answer the following questions:

• (a) Please show the graph in the form of adjacency matrix.

• (b) Please show the graph in the form of adjacency list.

Page 19: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q7.

• (a)

a b c d e F

a ∞ 10 ∞ 3 ∞ 2

b ∞ ∞ ∞ ∞ ∞ ∞

c 1 ∞ ∞ ∞ ∞ ∞

d 2 5 7 ∞ 6 ∞

e ∞ ∞ 4 9 ∞ 8

f 1 15 ∞ ∞ ∞ ∞

(b)a -> b,10 -> d,3 -> f,2b c -> a,1d -> a,2 -> b,5 -> c,7 -> e,6e -> c,4 -> d,9 -> f,8f -> a,1 -> b,15

Page 20: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Question 8.

• Consider Figure 3. and answer the following questions:

• (a) Please do the graph traversal using the depth-first search, starting with vertex a.

• (b) Please do the graph traversal using the breadth-first search, starting with vertex a.

• Note that if two or more vertices qualify, then the one with the least alphabetical order is selected.

Page 21: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q8.

• (a) a, b, c, d, e, f, g, h, i

• (b)a, b, h, i, c, d, g, e, f

Page 22: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Question 9.

• Consider Figure 2. Let vertex a be the origin. Please find the shortest paths from a to the other vertices using the Dijkstra’s shortest-path algorithm. Note that if two or more vertices qualify, then the one with the least alphabetical order is selected.

• (a) Please show the table after one vertex is selected.

• (b) Please show each path from vertex a and its distance.

Page 23: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q9.

• (a)a b C d e F

a 0 10 ∞ 3 ∞ 2

a, f 0 10 ∞ 3 ∞ 2

a, f, d 0 8 10 3 9 2

a, f, d, b 0 8 10 3 9 2

a, f, d, b, e 0 8 10 3 9 2

a, f, d, b, e, c

0 8 10 3 9 2

(b) a-f : 2a-d : 3a-d-b : 8a-d-e : 9a-d-c : 10

Page 24: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Question 10.

• Consider Figure 4.

• Let vertex a be the origin. Please find the minimum spanning tree for it using the Prim’s algorithm.

• Note that if two or more vertices qualify, then the one with the least alphabetical order is selected.

Page 25: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q10.

Page 26: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Question 11.

• Consider Figure 3.

• Let vertex a be the origin. Please find a spanning tree for it using the breadth-first search algorithm.

• Note that if two or more vertices qualify, then the one with the least alphabetical order is selected.

Page 27: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

Solution to Q11.

Page 28: Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have 13 entries. Suppose we have 10 integer keys: 68, 100, 58, 80, 72, 101,

-END-