108 年 國立中山大學電機工程學系碩士班丙組《資料結構》

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

第 1 題

Suppose we have a byte-addressable machine, i.e., each byte is accessed via an address.
Let the locations for an array be allocated in a row-major manner, and each element of an
array takes 4 bytes. Assume that the address of the first byte of the array is 500 in all the
following cases. Which of the following is true?
(A) The address of the element A[10][30] in an array declared as A[100][200] is 24586
(B) The address of the element A[10][20][30] in an array declared as A[100][200][300] is 177899
(C) The address of the element A[10] in an array declared as A[100] is 536
(D) The address of the element A[10][20][30][40] in an array declared as A[100][200][300][400] is 76457217

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

這一題的完整詳解

核心觀念與位址公式

在資料結構中,以列為主(Row-major order)的多維陣列記憶體位址計算,預設索引下界為 11(1-based indexing)。設起始位址為 Base=500\text{Base} = 500,每個元素大小 S=4 bytesS = 4\text{ bytes}:

  1. 一維陣列 A[d1]A[d_1]:
    Address(A[i])=Base+(i−1)×S\text{Address}(A[i]) = \text{Base} + (i - 1) \times S
  2. 二維陣列 A[d1][d2]A[d_1][d_2]:
    Address(A[i][j])=Base+[(i−1)×d2+(j−1)]×S\text{Address}(A[i][j]) = \text{Base} + [(i - 1) \times d_2 + (j - 1)] \times S
  3. 三維陣列 A[d1][d2][d3]A[d_1][d_2][d_3]:
    Address(A[i][j][k])=Base+[(i−1)×d2×d3+(j−1)×d3+(k−1)]×S\text{Address}(A[i][j][k]) = \text{Base} + [(i - 1) \times d_2 \times d_3 + (j - 1) \times d_3 + (k - 1)] \times S
  4. 四維陣列 A[d1][d2][d3][d4]A[d_1][d_2][d_3][d_4]:
    Address(A[i][j][k][l])=Base+[(i−1)×d2×d3×d4+(j−1)×d3×d4+(k−1)×d4+(l−1)]×S\text{Address}(A[i][j][k][l]) = \text{Base} + [(i - 1) \times d_2 \times d_3 \times d_4 + (j - 1) \times d_3 \times d_4 + (k - 1) \times d_4 + (l - 1)] \times S

選項推導與驗證

🔒

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

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

免費註冊

第 2 題

Let a, b and c are variables. Which of the following is a postfix expression?
(A) a/(b+c)a/(b + c)
(B) xabc/+xabc/+
(C) abc+/abc + /
(D) /a+bc/a + bc

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

這一題的完整詳解

核心觀念

本題考查算術運算式(Arithmetic Expression)的表示法及其轉換規則:

  1. 中序運算式(Infix Expression):運算子置於兩運算元中間,例如 a+ba + b。運算時需依賴「運算子優先權(Precedence)」與「括號(Parentheses)」來決定計算順序。
  2. 前序運算式(Prefix Expression / Polish Notation):運算子置於運算元之前,例如 +ab+ a b。不需括號即可唯一決定計算順序。
  3. 後序運算式(Postfix Expression / Reverse Polish Notation, RPN):運算子置於運算元之後,例如 ab+a b +。不需括號即可由左至右利用**堆疊(Stack)**進行線性時間的求值,是編譯器與虛擬機中最廣泛使用的表示法。

合法後序運算式的充要條件(針對二元運算子)

若運算式中皆為二元運算子(Binary Operators):

  • 設運算元個數為 NN、運算子個數為 MM,則必須滿足:
    N=M+1N = M + 1
  • 前綴累積條件:由左至右掃描時,任何時刻已讀取的運算元個數,必須隨時大於已讀取的運算子個數。

解題方法

將中序運算式轉換為後序運算式的標準括號法(Parenthesizing Method)如下:

  1. 依優先順序完全加上括號:
    以中序式 a/(b+c)a / (b + c) 為例,加法在括號內先做,除法後做:
    (a/(b+c))(a / (b + c))
  2. 將各運算子移至其對應括號的右外側:
    • 先處理內層括號:(b+c)→(bc+)(b + c) \to (b c +)
    • 再處理外層括號:(a/(bc+))→(a(bc+)/)(a / (b c +)) \to (a (b c +) /)
  3. 去除所有括號:
    得到後序運算式為:
    abc+/a b c + /

堆疊求值驗證流程

  • 讀入 a,b,ca, b, c →\to 依序推入堆疊,堆疊內容為 [a,b,c][a, b, c](頂端為 cc)。
🔒

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

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

免費註冊

第 3 題

Let a=3,b=8,c=6a = 3, b = 8, c = 6 and d=5d = 5. What is the value of the prefix expression ×ba+dc\times ba + dc?
(A) 13
(B) 14
(C) 15
(D) 16

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

這一題的完整詳解

觀念說明
前綴運算式(Prefix Expression)求值演算法採用堆疊(Stack)處理,由右至左掃描運算式:

  1. 遇到運算元(Operand)時,直接壓入堆疊。
  2. 遇到二元運算子(Operator)時,從堆疊頂端依序彈出前兩個運算元 op1op_1 與 op2op_2,進行 op1 [operator]op2op_1 \text{ [operator]} op_2 運算,並將結果壓回堆疊。

本題前綴運算式為 −×ba+dc- \times b a + d c(對應中綴式 (b×a)−(d+c)(b \times a) - (d + c))。


🔒

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

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

免費註冊

第 4 題

The ADT stack can be defined by the following axioms:
(aStack.createStack()).isEmpty()=true(aStack.createStack()).isEmpty() = \text{true}
(aStack.push(item)).isEmpty()=false(aStack.push(item)).isEmpty() = \text{false}
(aStack.createStack()).pop()=error(aStack.createStack()).pop() = \text{error}
(aStack.createStack()).getTop()=error(aStack.createStack()).getTop() = \text{error}
(aStack.push(item)).pop()=aStack(aStack.push(item)).pop() = aStack
(aStack.push(item)).getTop()=item(aStack.push(item)).getTop() = item

For the following expression:
(((((((aStack.createStack()).push(1)).push(2)).pop()).push(3)).pop()).pop()).isEmpty()(((((((aStack.createStack()).push(1)).push(2)).pop()).push(3)).pop()).pop()).isEmpty()
what is the value returned?
(A) an item
(B) a stack
(C) true
(D) false

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

這一題的完整詳解

核心觀念

本題考查堆疊(stack)的兩項核心特性:

  • 後進先出(LIFO, Last In First Out):最後放入的元素會最先被取出。
  • push(item):將元素放入堆疊頂端。
  • pop():移除並回傳堆疊頂端元素;依題目公理,運算結果表達為移除頂端元素後的堆疊。
  • isEmpty():判斷堆疊是否為空,回傳 true 或 false。

題目中的每個運算都會接在前一個運算結果後面,因此必須由內而外逐步追蹤堆疊內容。

解題方法

以堆疊底部到頂部表示目前內容:

  1. 建立空堆疊:

    aStack.createStack()=[ ]aStack.createStack() = [\ ]

  2. 執行 push(1):

    [ ]→push(1)[1][\ ] \xrightarrow{push(1)} [1]

  3. 執行 push(2):

    [1]→push(2)[1,2][1] \xrightarrow{push(2)} [1,2]

    此時 22 位於堆疊頂端。

  4. 執行 pop():

    依後進先出原則移除頂端元素 22:

    [1,2]→pop()[1][1,2] \xrightarrow{pop()} [1]

  5. 執行 push(3):

    [1]→push(3)[1,3][1] \xrightarrow{push(3)} [1,3]

  6. 執行 pop():

    移除頂端元素 33:

    [1,3]→pop()[1][1,3] \xrightarrow{pop()} [1]

  7. 再執行一次 pop():

    移除頂端元素 11:

🔒

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

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

免費註冊

第 5 題

Suppose a stack is implemented by a variable top and an array B[4]. Note that top denotes the index of the last element pushed. Initially, the stack is empty. Then the following operations are performed in order:
push(1), push(2), pop(), push(3), push(4), pop(), getTop(), push(5), pop(), push(6).
Which of the following is true in the end?
(A) B[0]=3B[0] = 3
(B) BB contains 4 integers available
(C) An overflow occurred, so the operations could not be completely done
(D) top is 2

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

這一題的完整詳解

觀念說明

本題考察以 0-indexed 陣列 B[4] 實作堆疊(Stack)的動態操作。依題意,top 表示最後壓入(push)元素的索引位置,且初始時堆疊為空,故初始設定 top=−1top = -1。

  • push(x):toptop 增加 1,並寫入 B[top]=xB[top] = x。
  • pop():取出頂端元素,toptop 減少 1。
  • getTop():僅讀取 B[top]B[top],toptop 與陣列內容保持不變。

操作追蹤

初始狀態:top=−1top = -1,堆疊為空。

  1. push(1):top=0top = 0,B[0]=1B[0] = 1
  2. push(2):top=1top = 1,B[1]=2B[1] = 2
  3. pop():top=0top = 0
  4. push(3):top=1top = 1,B[1]=3B[1] = 3
🔒

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

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

免費註冊

第 6 題

Let the height of a tree be the number of nodes along the longest path from the root node to the leaf nodes. Consider the integers 30, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67 in order to create a binary search tree. Which of the following is true?
(A) The node for 37 is a leaf node
(B) The root node is 41
(C) The node for 70 has only one child
(D) The height of the tree is 5

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

這一題的完整詳解

觀念說明

二元搜尋樹(Binary Search Tree, BST)插入新節點時,自根節點開始比對:小於當前節點走左子樹,大於當前節點走右子樹,直到找到空位置插入。題目定義樹高(Height)為根節點至葉節點最長路徑上的節點總數。


二元搜尋樹建構過程

依序插入數字:30, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67

  1. 插入 30:根節點 3030。
  2. 插入 41:41>30  ⟹  3041 > 30 \implies 30 的右子節點。
  3. 插入 25:25<30  ⟹  3025 < 30 \implies 30 的左子節點。
  4. 插入 29:29<30  ⟹  29>25  ⟹  2529 < 30 \implies 29 > 25 \implies 25 的右子節點。
  5. 插入 94:94>30  ⟹  94>41  ⟹  4194 > 30 \implies 94 > 41 \implies 41 的右子節點。
  6. 插入 37:37>30  ⟹  37<41  ⟹  4137 > 30 \implies 37 < 41 \implies 41 的左子節點。
  7. 插入 70:70>30  ⟹  70>41  ⟹  70<94  ⟹  9470 > 30 \implies 70 > 41 \implies 70 < 94 \implies 94 的左子節點。
  8. 插入 23:23<30  ⟹  23<25  ⟹  2523 < 30 \implies 23 < 25 \implies 25 的左子節點。
  9. 插入 65:65>30  ⟹  65>41  ⟹  65<94  ⟹  65<70  ⟹  7065 > 30 \implies 65 > 41 \implies 65 < 94 \implies 65 < 70 \implies 70 的左子節點。
🔒

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

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

免費註冊

第 7 題

Consider a complete binary tree with exactly 5000 nodes, implemented with an array starting from index 0. Suppose that a node has its value stored at index 999 in the array. What index is the value stored at for this node's left child?
(A) 999
(B) 1999
(C) 998
(D) 2998

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

這一題的完整詳解

核心觀念

以陣列從索引 00 開始儲存 complete binary tree 時,若某節點位於索引 ii:

  • 左子節點索引:2i+12i+1
  • 右子節點索引:2i+22i+2
  • 父節點索引:⌊i−12⌋\left\lfloor \dfrac{i-1}{2} \right\rfloor

題目給定節點位於索引 999999,因此直接套用左子節點公式。

解題方法

令目前節點索引為 i=999i=999,其左子節點索引為:

2i+1=2(999)+1=1998+1=19992i+1=2(999)+1=1998+1=1999

因此,左子節點的值儲存在索引 19991999。

題目雖然補充樹中共有 50005000 個節點,但此節點的左子節點索引為 19991999,小於最大有效索引 49994999,所以該左子節點確實存在。

選項分析

🔒

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

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

免費註冊

第 8 題

Consider a tree with A as the root node. The left and right child of A are B and C, respectively. The left and right child of B are D and E, respectively. The left and right child of E are F and G, respectively. What is the order of the nodes processed in the pre-order traversal?
(A) A-B-E-D-F-G-C
(B) D-B-F-E-A-G-C
(C) A-B-D-G-F-E-C
(D) A-B-D-E-F-G-C

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

這一題的完整詳解

核心觀念

本題考查樹的「前序走訪」(pre-order traversal)。

前序走訪的處理順序為:

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

對二元樹而言,每個節點都遵循:

  1. 先處理目前節點。
  2. 再走訪左子樹。
  3. 最後走訪右子樹。

題目中的樹結構如下:

        A
       / \
      B   C
     / \
    D   E
       / \
      F   G

解題方法

依照「根、左、右」的順序走訪:

  1. 根節點為 AA,先處理 AA。
  2. 進入 AA 的左子樹,根節點為 BB,處理 BB。
  3. 進入 BB 的左子樹,處理 DD。
  4. DD 沒有子節點,回到 BB,走訪 BB 的右子樹 EE。
  5. 進入 EE 的左子樹,處理 FF。
  6. 再走訪 EE 的右子樹,處理 GG。
  7. BB 的整個子樹完成後,回到 AA,走訪 AA 的右子樹 CC。

因此處理順序為:

A→B→D→E→F→G→CA \rightarrow B \rightarrow D \rightarrow E \rightarrow F \rightarrow G \rightarrow C

選項分析

🔒

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

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

免費註冊

第 9 題

Suppose we start with an empty max-heap of integers, and enter the numbers 20 through 30 into this heap in order. Let the resulting max-heap be stored in an array. What index is 28 stored at in the array?
(A) 2
(B) 3
(C) 4
(D) 5

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

這一題的完整詳解

核心觀念

本題考查「最大堆積(max-heap)」的插入與上浮(heapify-up)。

最大堆積必須同時滿足:

  1. 完全二元樹結構:新元素一律插入目前最底層最左邊的空位。
  2. 堆積順序性質:每個父節點的值都大於或等於其子節點。

若採用常見的 1-based 陣列索引,則:

  • 父節點索引:⌊i/2⌋\left\lfloor i/2 \right\rfloor
  • 左子節點索引:2i2i
  • 右子節點索引:2i+12i+1

插入新元素後,若它大於父節點,就必須持續與父節點交換,直到恢復最大堆積性質。

解題方法

依序插入 2020 至 3030,只列出與本題相關的最後幾步。

插入 2727 後,堆積陣列為:

[27,26,25,23,22,21,24,20][27,26,25,23,22,21,24,20]

插入 2828

先放入下一個完全二元樹位置,即索引 99:

[27,26,25,23,22,21,24,20,28][27,26,25,23,22,21,24,20,28]

索引 99 的父節點為:

⌊92⌋=4\left\lfloor \frac{9}{2} \right\rfloor=4

由於 28>2328>23,兩者交換:

[27,26,25,28,22,21,24,20,23][27,26,25,28,22,21,24,20,23]

此時 2828 位於索引 44,其父節點索引為:

⌊42⌋=2\left\lfloor \frac{4}{2} \right\rfloor=2

因為 28>2628>26,再次交換:

[27,28,25,26,22,21,24,20,23][27,28,25,26,22,21,24,20,23]

接著 2828 位於索引 22,其父節點是索引 11。因為 28>2728>27,繼續交換:

🔒

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

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

免費註冊

第 10 題

Suppose we start with an empty max-heap of integers, and enter the numbers 20 through 30 into this heap in order. Let the resulting max-heap be stored in an array. Then remove the root node from the heap. What index is 28 stored at in the array?
(A) 4
(B) 3
(C) 2
(D) 1

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

這一題的完整詳解

核心觀念

本題考查最大堆積(max-heap)的兩個操作:

  1. 插入元素:新元素先放在陣列末端,再向上調整(sift-up),直到符合父節點值大於等於子節點值。
  2. 刪除根節點:移除最大值後,將最後一個元素移到根節點,再向下調整(sift-down)。

採用常見的 11-based heap 陣列表示法:

  • 父節點索引:⌊i/2⌋\left\lfloor i/2\right\rfloor
  • 左子節點索引:2i2i
  • 右子節點索引:2i+12i+1

解題方法

依序插入 2020 至 3030,每次插入後向上調整,得到:

索引1234567891011值3029252328212420222627\begin{array}{c|ccccccccccc} \text{索引} & 1&2&3&4&5&6&7&8&9&10&11\\ \hline \text{值} &30&29&25&23&28&21&24&20&22&26&27 \end{array}

因此,刪除根節點 3030 時:

  1. 將最後一個元素 2727 移到根節點:
[27,29,25,23,28,21,24,20,22,26][27,29,25,23,28,21,24,20,22,26]
🔒

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

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

免費註冊

第 11 題

An empty hash table has a capacity of 13, and you insert six entries with keys 21, 16, 8, 10, 22, 34, and 49. Using linear probing and the hash function h(key)=key(mod13)h(key) = key \pmod{13}, what index 49 is stored at in the table? Note that % is the remainder operator, e.g., (100)%(13)=9(100)\%(13)=9.
(A) 0
(B) 5
(C) 11
(D) 8

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

這一題的完整詳解

核心觀念

本題考查雜湊表的「線性探測法(linear probing)」。

雜湊函數為:

h(key)=key mod 13h(key)=key\bmod 13

若計算出的索引已被占用,就依序往下一格尋找;到索引 1212 後,下一格回到索引 00,形成循環探測。

題目文字寫「六筆資料」,但實際列出 21,16,8,10,22,34,4921,16,8,10,22,34,49 共七個鍵值。因為題目要求查詢 4949 的位置,以下依照列出的七個鍵值,按照題目順序插入。

解題方法

依序計算雜湊位置,發生碰撞時採用線性探測。

插入鍵值原始雜湊位置線性探測結果最終索引
212121 mod 13=821\bmod13=8索引 88 空88
161616 mod 13=316\bmod13=3索引 33 空33
888 mod 13=88\bmod13=888 已占用,探測 9999
101010 mod 13=1010\bmod13=10索引 1010 空1010
222222 mod 13=922\bmod13=999、1010 已占用,探測 11111111
343434 mod 13=834\bmod13=888、99、1010、1111 已占用,探測 12121212
🔒

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

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

免費註冊

第 12 題

Consider the integers 30, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67 in order to create an AVL tree. Which of the following about the tree is false?
(A) The node for 37 is a leaf node
(B) The root node is 41
(C) The node for 70 has only one child
(D) The height of the tree is 4

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

這一題的完整詳解

依次插入數值建構 AVL 樹,各階段關鍵旋轉與平衡調整如下:

  1. 插入 30、41、25、29、94、37 均保持平衡。
  2. 插入 70、23 後,插入 65:節點 9494 失去平衡(LL 型),對 9494 做右旋轉,由 7070 替代其位置。
  3. 插入 75:節點 4141 失去平衡(RR 型),對 4141 做左旋轉,由 7070 提升為該子樹根節點。
  4. 插入 68:根節點 3030 失去平衡(RL 型),對 3030 做雙重旋轉(RL Rotation),最終由 4141 成為全樹根節點。
  5. 插入 67:節點 6565 失去平衡(RL 型),對 6565 做雙重旋轉,由 6767 提升為該子樹根節點。

最終 AVL 樹結構

🔒

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

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

免費註冊

第 13 題

Consider the integers 30,41,25,29,94,37,70,23,65,75,68,6730, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67 in order to create a 2-3 tree. Which of the following about the tree is true?

(A) The node for 3737 is not a leaf node.
(B) The root node is 4141.
(C) The node for 7070 has only one child.
(D) The height of the tree is 33.

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

這一題的完整詳解
載入中…

第 14 題

Consider the following recursive definition for a language:

⟨word⟩=⟨plus⟩∣⟨minus⟩⟨word⟩∣⟨word⟩⟨plus⟩\langle\text{word}\rangle = \langle\text{plus}\rangle \mid \langle\text{minus}\rangle\langle\text{word}\rangle \mid \langle\text{word}\rangle\langle\text{plus}\rangle

⟨plus⟩=+\langle\text{plus}\rangle = +

⟨minus⟩=−\langle\text{minus}\rangle = -

Which of the following strings are in this language?

(A) ++++++
(B) +−−+--
(C) −++-++
(D) −−+--+

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

這一題的完整詳解
載入中…

第 15 題

Consider an array AA containing 10 integers 42,3,17,22,32,7,12,74,47,842, 3, 17, 22, 32, 7, 12, 74, 47, 8. We use quicksort to sort the integers in ascending order. The first element of the underlying sequence is used as the pivot. Which of the following are false after the first partition?

(A) A[5]=7A[5]=7
(B) A[4]=42A[4]=42
(C) A[8]=47A[8]=47
(D) A[0]=3A[0]=3

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

這一題的完整詳解
載入中…

第 16 題

Which of the following are true?

(A) The worst-case running time for quicksort is O(nlog⁡n)O(n\log n).
(B) No additional memory for array is required for quicksort.
(C) The best-case running time for bubble-sort is O(nlog⁡n)O(n\log n).
(D) The best-case running time for insertion-sort is O(n)O(n).

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

這一題的完整詳解
載入中…

第 17 題

Consider an array AA containing, initially, 12 integers 30,41,25,29,94,37,70,23,65,75,68,6730, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67. We convert AA into a maxheap. Which of the following are false?

(A) A[2]=70A[2]=70
(B) A[5]=68A[5]=68
(C) A[8]=23A[8]=23
(D) A[11]=30A[11]=30

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

這一題的完整詳解
載入中…

第 18 題

Consider the integers 30,41,25,29,94,37,70,23,65,75,68,6730, 41, 25, 29, 94, 37, 70, 23, 65, 75, 68, 67 in order to create a binary search tree. Now delete 7070 from the tree. Note that the replacement should be the least of the numbers equal to or greater than the deleted element. Which of the following about the resulting tree are true?

(A) The node for 7575 has only one child.
(B) The node for 6565 has a left child.
(C) The node for 4141 has two children.
(D) The node for 6565 is a child of the node for 9494.

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

這一題的完整詳解
載入中…

第 19 題

Which of the following are true?

(A) The minimum height of a binary tree with 100 nodes is 6.
(B) The minimum height of a tree with 15 nodes is 2.
(C) The maximum height of a tree with 20 nodes is 20.
(D) The maximum height of a complete binary tree with 200 nodes is 8.

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

這一題的完整詳解
載入中…

其他考古題