國立交通大學 工學院半導體材料與製程設備學程 摘要...錫銀覆晶銲錫中金屬墊層電遷移與熱遷移行為之研究 研究生:楊宗霖 指導教授:
Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have...
-
Upload
gertrude-lucas -
Category
Documents
-
view
237 -
download
3
Transcript of Data Structures -Final exam- 2015/6/22 授課教授:李錫智. Question 1. Let a hash table have...
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, 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.
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
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.
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
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.
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
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.
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 之後
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.
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
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
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
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.
50, 90
20
10
70
30, 40 60 80
120, 150
160140100, 110
(a)
Solution to Q6-a.
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.
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.
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.
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
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.
Solution to Q8.
• (a) a, b, c, d, e, f, g, h, i
• (b)a, b, h, i, c, d, g, e, f
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.
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
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.
Solution to Q10.
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.
Solution to Q11.
-END-