Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— Presentation transcript:

1 Data Structures -Final exam- 2015/6/22 授課教授:李錫智

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

3 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 ,直至找到空位或找完為止 8068165872341001012445

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

5 Solution to Q2. (a) (b) 45%13=6 ,比對 hash table index 為 (6+n 2 )%13 的位址內是否為 45 ,其中 n=0, 1, 2, …. ,若是 45 則回傳 (6+n 2 ) % 13 就是 45 的 hash table 位址 (c) 46%13=7 ,尋找 hash table index 為 (7+n 2 ) % 13 的位址,其中 n=0, 1, 2, …. ,直到找到第一個 空的位址或找完為止 8068164558723410010124

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

7 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, … 直到找到空位或找完為止 1645806858723410010124

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

9 Solution to Q4. 0 1 2 3 4 5 6 7 8 9 10 11 12 80 1668 4558 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 之後

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

11 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

12 Solution to Q5-b. 50 1. 90 50 2. 50 2090 103070120 150 3. 90 50140 2070120160 10 30,40 60 80 100,110 130 150 170 4. 90 50140 2070120160 180 10 30,40 60 80 100,110 130 150 170 190,200 5. 20

13 Solution to Q5-c. 50 1. 90 50 2. 50 2090 70 3. 90 50120 20 70 150 10 4. 50 30 90 20 40 70 120 10 60 80 100 150 5. 110 120 50 160 30 40 90 180 20 70 170190 6. 10 60 80 140 100 110 200 130 150

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

15 50, 90 20 10 70 30, 40 60 80 120, 150 160 140100, 110 (a) Solution to Q6-a.

16 50, 90 30 20 70 40 60 80 120, 150 160 130, 140100, 110 6.(b) 50, 90 20 10 70 30, 40 60 80 120, 140 150 130100, 110 6.(c) Solution to Q6-b.

17 50, 90 20 10 70 30, 40 60 80 130, 150 160 140100, 110 6.(d) 90 20, 50 10 30, 40 120, 150 160 130, 140100, 110 6.(e) 70, 80 Solution to Q6-c.

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

19 Solution to Q7. (a) abcdeF a∞10∞3∞2 b∞∞∞∞∞∞ c1∞∞∞∞∞ d257∞6∞ e∞∞49∞8 f115∞∞∞∞ (b) a -> b,10 -> d,3 -> f,2 b c -> a,1 d -> a,2 -> b,5 -> c,7 -> e,6 e -> c,4 -> d,9 -> f,8 f -> a,1 -> b,15

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

21 Solution to Q8. (a) a, b, c, d, e, f, g, h, i (b)a, b, h, i, c, d, g, e, f

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

23 Solution to Q9. (a) abCdeF a010∞3∞2 a, f010∞3∞2 a, f, d0810392 a, f, d, b0810392 a, f, d, b, e0810392 a, f, d, b, e, c0810392 (b) a-f : 2 a-d : 3 a-d-b : 8 a-d-e : 9 a-d-c : 10

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

25 Solution to Q10.

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

27 Solution to Q11.

28 -END-


Download ppt "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,"

Similar presentations


Ads by Google