110 年 國立政治大學資訊管理學系碩士班科技組《資料結構》
📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊
第 I. 題45 分
(45%) Consider the following keys: 2, 4, 26, 25, 13, 14, 51, 9, 17, 43, 68
- (15%) Insert the keys into a min heap one by one. Reconstruct the heap when it violates the heap order (for all node v, v.key > v.parent.key). Show each step and the final result.
- (15%) Insert the keys into a hash table that handles collision with double hashing. Let N=17. . . Show each step and the final result.
- (15%) Insert the keys into an AVL tree one by one. Reconstruct the AVL tree when it violates the AVL property (for all node v, ). Show each step and the final result.
登入後即可作答並保存紀錄。
這一題的完整詳解
1. Min‑Heap (array, 1 索引)
| 插入次序 | 插入鍵 | 失衡調整 | 最終陣列 (前 11 個) |
|---|---|---|---|
| 1 | 2 | – | [2] |
| 2 | 4 | – | [2,4] |
| 3 | 26 | – | [2,4,26] |
| 4 | 25 | – | [2,4,26,25] |
| 5 | 13 | – | [2,4,26,25,13] |
| 6 | 14 | 14 與 26 交換 → 左旋於 2 | [2,4,14,25,13,26] |
| 7 | 51 | – | [2,4,14,25,13,26,51] |
| 8 | 9 | 9 與 25 交換 → 左旋於 2 | [2,4,14,9,13,26,51,25] |
| 9 | 17 | – | [2,4,14,9,13,26,51,25,17] |
| 10 | 43 | – | [2,4,14,9,13,26,51,25,17,43] |
| 11 | 68 | – | [2,4,14,9,13,26,51,25,17,43,68] |
最終最小堆:[2, 4, 14, 9, 13, 26, 51, 25, 17, 43, 68]
2. 雙重雜湊 (N = 17)
| 鍵 | h(k) | d(k) | 探測序列 (index) | 放置位置 |
|---|---|---|---|---|
| 2 | 2 | 11 | 2 | 2 |
| 4 | 4 | 9 | 4 | 4 |
| 26 | 9 | 13 | 9 | 9 |
| 25 | 8 | 1 | 8 | 8 |
第 II. 題25 分
(25%) To sort the above keys in I, describe a sorting algorithm
- (10%) that uses the heap constructed in I.1
- (15%) that uses the AVL tree constructed in I.3
登入後即可作答並保存紀錄。
這一題的完整詳解
使用已建好的 heap(第 I.1)排序
- 若建的是 max‑heap,執行 Heap‑Sort:
- 初始已完成 建堆。
- 迭代 :
- 交換根節點與第 個元素(取得目前最大值),
- 對長度 的子堆呼叫 heapify(下沉)。
- 迴圈結束後,陣列由大到小有序;若欲升序,使用 min‑heap 同理。
- 時間複雜度:建堆 + 次下沉 ⇒ 。
- 空間複雜度:原地 (若使用額外陣列則 )。
使用已建好的 AVL 樹(第 I.3)排序
- 將所有鍵已插入 AVL 樹(已平衡),此步驟在題目中已完成。
- 以 中序走訪(In‑order Traversal)取得鍵的遞增序列:
第 III. 題30 分
(30%) Describe an algorithm to evaluate the expression 13 + 5 - 6 * 3 + 4 - 6 + 9 / 3
- (15%) that uses recursive call on the binary tree representation of the expression
- (15%) that uses two stacks for operators and operands (values) respectively
登入後即可作答並保存紀錄。
這一題的完整詳解
核心觀念
本題考查兩種算術運算式求值方法:
- 二元運算式樹(binary expression tree)
- 兩個堆疊(two stacks):一個存運算子,一個存運算元
運算優先順序為:
同一優先順序的運算子採由左至右結合,因此原式等價於:
一、使用二元樹遞迴求值
解題方法
二元運算式樹的規則如下:
- 葉節點是運算元,例如
- 內部節點是運算子,例如
- 每個運算子節點的左子樹代表左運算式,右子樹代表右運算式
本題的運算式樹如下:
+
/ \
- /
/ \ / \
+ 6 9 3
/ \
- 4
/ \
+ *
/ \ / \
13 5 6 3
其結構表示:
遞迴演算法
Evaluate(node):
if node 是葉節點:
return node.value
leftValue = Evaluate(node.left)
rightValue = Evaluate(node.right)
if node.operator == '+':
return leftValue + rightValue
if node.operator == '-':
return leftValue - rightValue
if node.operator == '*':
return leftValue * rightValue
if node.operator == '/':
return leftValue / rightValue
此方法採用後序走訪(postorder traversal):
計算過程
由最底層的運算開始:
因此根節點的加法為:
複雜度
若運算式樹共有 個節點:
- 每個節點只走訪一次,時間複雜度為
- 遞迴呼叫堆疊的空間複雜度為
其中 為樹高;若樹極度偏斜,最壞情況為 。
二、使用兩個堆疊求值
解題方法
使用:
operators:儲存運算子operands:儲存數值
掃描運算式時遵守以下規則:
- 讀到運算元,直接推入
operands - 讀到運算子時:
- 若堆疊頂端運算子的優先順序較高或相同,先進行堆疊頂端運算
- 再將目前運算子推入
operators
- 掃描結束後,依序處理剩餘運算子
由於本題的運算子皆為左結合,因此判斷條件為:
進行一次運算的程序
從 operands 取出兩個值時,先取出的是右運算元: