110 年 國立政治大學資訊管理學系碩士班科技組《資料結構》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 I. 題45 分

(45%) Consider the following keys: 2, 4, 26, 25, 13, 14, 51, 9, 17, 43, 68

  1. (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.
  2. (15%) Insert the keys into a hash table that handles collision with double hashing. Let N=17. h(k)=k(mod17)h(k) = k \pmod{17}. d(k)=13−k(mod13)d(k) = 13-k \pmod{13}. Show each step and the final result.
  3. (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, ∣v.leftsubtree.height−v.rightsubtree.height∣≤1|v.\text{leftsubtree.height} - v.\text{rightsubtree.height}| \le 1). Show each step and the final result.

登入後即可作答並保存紀錄。

這一題的完整詳解

1. Min‑Heap (array, 1 索引)

插入次序插入鍵失衡調整最終陣列 (前 11 個)
12–[2]
24–[2,4]
326–[2,4,26]
425–[2,4,26,25]
513–[2,4,26,25,13]
61414 與 26 交換 → 左旋於 2[2,4,14,25,13,26]
751–[2,4,14,25,13,26,51]
899 與 25 交換 → 左旋於 2[2,4,14,9,13,26,51,25]
917–[2,4,14,9,13,26,51,25,17]
1043–[2,4,14,9,13,26,51,25,17,43]
1168–[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)=k mod 17,d(k)=(13−k mod 13)(mod13)  (d=0⇒13)h(k)=k\bmod 17,\qquad d(k)=\bigl(13-k\bmod13\bigr)\pmod{13}\;(d=0\Rightarrow13)
鍵h(k)d(k)探測序列 (index)放置位置
221122
44944
2691399
258188
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 II. 題25 分

(25%) To sort the above keys in I, describe a sorting algorithm

  1. (10%) that uses the heap constructed in I.1
  2. (15%) that uses the AVL tree constructed in I.3

登入後即可作答並保存紀錄。

這一題的完整詳解

使用已建好的 heap(第 I.1)排序

  1. 若建的是 max‑heap,執行 Heap‑Sort:
    • 初始已完成 O(n)O(n) 建堆。
    • 迭代 i=n downto 2i=n\ downto\ 2:
      • 交換根節點與第 ii 個元素(取得目前最大值),
      • 對長度 i−1i-1 的子堆呼叫 heapify(下沉)O(log⁡n)O(\log n)。
    • 迴圈結束後,陣列由大到小有序;若欲升序,使用 min‑heap 同理。
  2. 時間複雜度:建堆 O(n)O(n) + n−1n-1 次下沉 O(nlog⁡n)O(n\log n) ⇒ O(nlog⁡n)O(n\log n)。
  3. 空間複雜度:原地 O(1)O(1)(若使用額外陣列則 O(n)O(n))。

使用已建好的 AVL 樹(第 I.3)排序

  1. 將所有鍵已插入 AVL 樹(已平衡),此步驟在題目中已完成。
  2. 以 中序走訪(In‑order Traversal)取得鍵的遞增序列:
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 III. 題30 分

(30%) Describe an algorithm to evaluate the expression 13 + 5 - 6 * 3 + 4 - 6 + 9 / 3

  1. (15%) that uses recursive call on the binary tree representation of the expression
  2. (15%) that uses two stacks for operators and operands (values) respectively

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考查兩種算術運算式求值方法:

  1. 二元運算式樹(binary expression tree)
  2. 兩個堆疊(two stacks):一個存運算子,一個存運算元

運算優先順序為:

∗、/  >  +、−*、/ \;>\; +、-

同一優先順序的運算子採由左至右結合,因此原式等價於:

((((13+5)−(6×3))+4)−6)+(9÷3)((((13+5)-(6\times3))+4)-6)+(9\div3)

一、使用二元樹遞迴求值

解題方法

二元運算式樹的規則如下:

  • 葉節點是運算元,例如 13、5、613、5、6
  • 內部節點是運算子,例如 +、−、×、/+、-、\times、/
  • 每個運算子節點的左子樹代表左運算式,右子樹代表右運算式

本題的運算式樹如下:

                         +
                      /     \
                     -       /
                   /   \    / \
                  +     6  9   3
                /   \
               -     4
             /   \
            +     *
           / \   / \
          13  5 6   3

其結構表示:

(((13+5)−(6×3))+4−6)+(9÷3)\left(\left(\left(13+5\right)-\left(6\times3\right)\right)+4-6\right)+\left(9\div3\right)

遞迴演算法

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):

左子樹→右子樹→根節點\text{左子樹} \rightarrow \text{右子樹} \rightarrow \text{根節點}

計算過程

由最底層的運算開始:

13+5=1813+5=18 6×3=186\times3=18 18−18=018-18=0 0+4=40+4=4 4−6=−24-6=-2 9÷3=39\div3=3

因此根節點的加法為:

−2+3=1-2+3=1

複雜度

若運算式樹共有 nn 個節點:

  • 每個節點只走訪一次,時間複雜度為
O(n)O(n)
  • 遞迴呼叫堆疊的空間複雜度為
O(h)O(h)

其中 hh 為樹高;若樹極度偏斜,最壞情況為 O(n)O(n)。


二、使用兩個堆疊求值

解題方法

使用:

  • operators:儲存運算子
  • operands:儲存數值

掃描運算式時遵守以下規則:

  1. 讀到運算元,直接推入 operands
  2. 讀到運算子時:
    • 若堆疊頂端運算子的優先順序較高或相同,先進行堆疊頂端運算
    • 再將目前運算子推入 operators
  3. 掃描結束後,依序處理剩餘運算子

由於本題的運算子皆為左結合,因此判斷條件為:

precedence⁡(stack top)≥precedence⁡(current operator)\operatorname{precedence}(\text{stack top}) \geq \operatorname{precedence}(\text{current operator})

進行一次運算的程序

從 operands 取出兩個值時,先取出的是右運算元:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題