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

選項推導與驗證

  • (A) 錯誤
    宣告 A[100][200]A[100][200],求 A[10][30]A[10][30] 之位址:
    Address(A[10][30])=500+[(10−1)×200+(30−1)]×4=500+[1800+29]×4=7816≠24586\text{Address}(A[10][30]) = 500 + [(10 - 1) \times 200 + (30 - 1)] \times 4 = 500 + [1800 + 29] \times 4 = 7816 \neq 24586

  • (B) 錯誤
    宣告 A[100][200][300]A[100][200][300],求 A[10][20][30]A[10][20][30] 之位址:

🔒

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

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

免費註冊

第 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)。
  • 讀入 ++ →\to 彈出 cc 與 bb,計算 (b+c)(b + c),將結果推回堆疊,堆疊內容為 [a,(b+c)][a, (b + c)]。
  • 讀入 // →\to 彈出 (b+c)(b + c) 與 aa,計算 a/(b+c)a / (b + c),推回堆疊,堆疊內容為 [a/(b+c)][a / (b + c)]。
  • 掃描結束,堆疊內僅存一個元素,計算結果完全正確且合法。

選項分析

  • (A) 錯誤:a/(b+c)a/(b + c) 的運算子位於運算元之間且帶有括號,此為標準的中序運算式(Infix Expression)。
  • (B) 錯誤:xabc/+xabc/+ 出現未定義的額外變數 xx;
🔒

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

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

免費註冊

第 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))。


計算步驟
已知變數值 a=3,b=8,c=6,d=5a = 3, b = 8, c = 6, d = 5。

🔒

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

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

免費註冊

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

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

  8. 最後執行 isEmpty():

    目前堆疊為空,因此:

    [ ].isEmpty()=true[\ ].isEmpty() = \text{true}

選項分析

  • (A) an item:錯誤。
    item 是 getTop() 或某些定義下的 pop()可能回傳的元素型態,但本題最後呼叫的是 isEmpty(),回傳值必定是布林值。
🔒

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

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

免費註冊

第 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
  5. push(4):top=2top = 2,B[2]=4B[2] = 4
  6. pop():top=1top = 1
  7. getTop():top=1top = 1(僅讀取 B[1]=3B[1] = 3,狀態不變)
  8. push(5):top=2top = 2,B[2]=5B[2] = 5
🔒

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

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

免費註冊

第 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 的左子節點。
  10. 插入 75:75>30  ⟹  75>41  ⟹  75<94  ⟹  75>70  ⟹  7075 > 30 \implies 75 > 41 \implies 75 < 94 \implies 75 > 70 \implies 70 的右子節點。
  11. 插入 68:68>30  ⟹  68>41  ⟹  68<94  ⟹  68<70  ⟹  68>65  ⟹  6568 > 30 \implies 68 > 41 \implies 68 < 94 \implies 68 < 70 \implies 68 > 65 \implies 65 的右子節點。
🔒

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

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

免費註冊

第 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,所以該左子節點確實存在。

選項分析

  • (A) 999:錯誤。999999 是目前節點本身的索引,不是左子節點索引。
  • (B) 1999:正確。依公式 2i+12i+1,得 2(999)+1=19992(999)+1=1999。
  • (C) 998:錯誤。
🔒

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

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

免費註冊

第 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

選項分析

  • (A) A-B-E-D-F-G-C:錯誤

    BB 的左子節點是 DD,右子節點是 EE。前序走訪處理 BB 後,必須先處理左子樹 DD,再處理右子樹 EE,因此不可能先出現 EE 再出現 DD。

  • (B) D-B-F-E-A-G-C:錯誤

    此順序不是前序走訪,因為前序走訪必須先處理根節點 AA,但此選項以 DD 開始,較接近後序或其他錯誤排列。

  • (C) A-B-D-G-F-E-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,繼續交換:

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

因此,插入 2828 完成後,2828 位於索引 11。

插入 2929

插入 2929 至索引 1010,並上浮後:

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

插入 3030

插入 3030 至索引 1111:

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

3030 依序上浮:

[29,28,25,26,30,21,24,20,23,22,27][29,28,25,26,30,21,24,20,23,22,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]
  1. 根節點 2727 與兩個子節點 2929、2525 比較,和較大的 2929 交換:
[29,27,25,23,28,21,24,20,22,26][29,27,25,23,28,21,24,20,22,26]
  1. 此時 2727 位於索引 22,其子節點為索引 44 的 2323 與索引 55 的 2828。因為 28>2728>27,兩者交換:
[29,28,25,23,27,21,24,20,22,26][29,28,25,23,27,21,24,20,22,26]

最後,2828 位於索引 22。

🔒

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

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

免費註冊

第 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
494949 mod 13=1049\bmod13=101010、1111、1212 已占用,下一格回到 0000

插入 4949 前,相關位置如下:

  • 索引 88:2121
  • 索引 99:88
  • 索引 1010:1010
  • 索引 1111:2222
  • 索引 1212:3434

因此:

h(49)=49 mod 13=10h(49)=49\bmod13=10

索引 1010、1111、1212 均已占用,線性探測後回到索引 00,所以 4949 存放於索引 00。

選項分析

🔒

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

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

免費註冊

第 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 樹結構

  • 根節點(Root):4141
  • 左子樹:
    • 4141 的左子節點為 3030
    • 3030 的左子節點為 2525(包含左子 2323、右子 2929)
    • 3030 的右子節點為 3737(無子節點)
  • 右子樹:
🔒

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

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

免費註冊

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

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

這一題的完整詳解

核心觀念

2-3 樹是一種平衡搜尋樹:

  • 2-節點含有 1 個鍵值,若不是葉節點,便有 2 個子樹。
  • 3-節點含有 2 個鍵值,若不是葉節點,便有 3 個子樹。
  • 所有葉節點都位於同一層。
  • 插入後若節點暫時含有 3 個鍵值,便將中間鍵值提升至父節點,剩下的兩個鍵值分別形成左右節點;若根節點也溢位,提升中間鍵值建立新根。

解題方法

依題目順序逐一插入。圖中方括號表示同一個節點內的鍵值;節點溢位時,將中間鍵值提升。

插入 30,41,2530,41,25 後,根節點溢位並分裂:

        [30]
       /    \
    [25]    [41]

插入 29,94,3729,94,37。鍵值 3737 插入右側葉節點後造成溢位,提升 4141:

          [30 | 41]
         /    |    \
    [25 | 29] [37] [94]

插入 7070 至最右側葉節點,再插入 2323。左側葉節點溢位,提升 2525;父節點也因此溢位,再將 3030 提升成新根:

             [30]
            /    \
         [25]    [41]
         / \     /  \
      [23] [29] [37] [70 | 94]

再插入 65,75,6865,75,68。插入 6565 後最右側葉節點溢位,提升 7070 至右側父節點;接著 75,6875,68 分別進入相應葉節點:

               [30]
              /    \
           [25]   [41 | 70]
           / \     /   |    \
        [23][29] [37] [65 | 68] [75 | 94]

最後插入 6767。葉節點 [65∣68][65|68] 變成 [65∣67∣68][65|67|68],提升中間鍵值 6767。其父節點也溢位,將 6767 提升至根節點:

🔒

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

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

免費註冊

第 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) −−+--+

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

這一題的完整詳解

核心觀念

這題考的是上下文無關文法的遞迴產生式:一個字串若能從 <word> 出發,反覆套用規則,最後化為終結符號 +、-,便屬於此語言。

產生式為:

  • <word> → <plus>:以 + 作為基本字串。
  • <word> → <minus><word>:在既有字串前方加上 -。
  • <word> → <word><plus>:在既有字串後方加上 +。

解題方法

從基本字串 + 開始,兩種遞迴操作分別只能在前方加 -、或在後方加 +。因此所有可生成字串都符合以下形式:

−m+n,m≥0,n≥1-^m +^n,\qquad m\ge 0,\quad n\ge 1

也就是零個或多個 - 必須出現在一個或多個 + 之前。只要字串中有 - 出現在 + 之後,就不符合此形式。

選項分析

  • (A) +++:屬於語言。
    連續在基本字串後方加上兩個 +:

    ⟨word⟩⇒⟨word⟩⟨plus⟩⇒⟨word⟩⟨plus⟩⟨plus⟩⇒+++\langle\text{word}\rangle \Rightarrow \langle\text{word}\rangle\langle\text{plus}\rangle \Rightarrow \langle\text{word}\rangle\langle\text{plus}\rangle\langle\text{plus}\rangle \Rightarrow +++
  • (B) +--:不屬於語言。
    字串中的 - 出現在 + 之後;文法只能把 - 加在字串前方,無法在 + 後方產生 -。

  • (C) -++:屬於語言。
    先在基本字串前方加 -,再於後方加 +:

🔒

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

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

免費註冊

第 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

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

這一題的完整詳解

核心觀念

快速排序的分割(partition)會選定樞紐值,並重新排列序列,使樞紐左側的元素不大於樞紐、右側的元素不小於樞紐。完成分割後,樞紐位於它在排序結果中的正確位置;但分割通常不會把左右兩側各自排好序。

本題以第一個元素 4242 為樞紐,採用由右往左、由左往右掃描,找到左右兩側位置不合條件的元素後交換,最後將樞紐放到分割位置的方式。

解題方法

初始陣列為:

[42,3,17,22,32,7,12,74,47,8][42,3,17,22,32,7,12,74,47,8]

由右側開始尋找小於 4242 的元素,找到 88;由左側開始尋找大於 4242 的元素,找到 7474。交換兩者:

[42,3,17,22,32,7,12,8,47,74][42,3,17,22,32,7,12,8,47,74]

繼續掃描時,左右指標交錯,表示分割完成。將樞紐 4242 與左側最後一個位置的元素 88 交換,得到:

[8,3,17,22,32,7,12,42,47,74][8,3,17,22,32,7,12,42,47,74]

此時 4242 左側的元素都小於 4242,右側的元素都大於 4242。

選項分析

🔒

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

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

免費註冊

第 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).

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

這一題的完整詳解

核心觀念

本題考查快速排序、泡沫排序與插入排序的最佳、平均、最壞時間複雜度,以及快速排序是否需要額外陣列空間。大 OO 表示輸入規模 nn 增大時,執行時間或空間用量的漸近上界。

解題方法

逐一比對各排序法的標準複雜度,並分清楚「額外陣列空間」與「所有額外記憶體」:

  • 快速排序每次選擇樞紐,將資料分成兩側再遞迴排序。若每次都分得極不平均,遞迴式為
    T(n)=T(n−1)+Θ(n)T(n)=T(n-1)+\Theta(n)
    因而最壞時間為 Θ(n2)\Theta(n^2)。
  • 泡沫排序在已排序資料上,若使用「本輪沒有交換就停止」的最佳化,最佳時間為 Θ(n)\Theta(n);未使用此最佳化時,仍須做完各輪比較,時間為 Θ(n2)\Theta(n^2)。
  • 插入排序在已排序資料上,每個元素只需檢查一次即可插入,最佳時間為 Θ(n)\Theta(n)。

選項分析

(A) 錯誤。 快速排序的最壞情況是樞紐每次都把資料分成大小為 00 與 n−1n-1 的兩部分。每層分割需 Θ(n)\Theta(n) 時間,因此:

T(n)=T(n−1)+Θ(n)=Θ(n2)T(n)=T(n-1)+\Theta(n)=\Theta(n^2)

所以最壞時間不是 O(nlog⁡n)O(n\log n)。

(B) 正確。 常見的原地快速排序會在原陣列內交換元素完成分割,不需要配置另一個同等大小的陣列。因此,若題目所指的是「額外陣列空間」,快速排序不需要額外陣列。不過,遞迴呼叫仍會使用堆疊空間;

🔒

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

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

免費註冊

第 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

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

這一題的完整詳解

核心觀念

最大堆(max heap)是滿足下列條件的完全二元樹:每個父節點的值都大於或等於其子節點的值。題目中的陣列以 1 為起始索引表示,因此索引 ii 的左右子節點分別位於 2i2i 與 2i+12i+1。

將陣列轉成最大堆時,採用自底向上的建堆法:從最後一個非葉節點 ⌊n/2⌋\lfloor n/2\rfloor 開始,依序向根節點進行下沉調整。本題 n=12n=12,最後一個非葉節點為索引 66。

解題方法

初始陣列如下:

索引123456789101112
值304125299437702365756867

由索引 66 往上調整至索引 11:

  • 索引 66 的值為 3737,子節點為 6767,交換兩者。
  • 索引 55 的值為 9494,已大於子節點 7575、6868,不需調整。
  • 索引 44 的值為 2929,與較大的子節點 6565 交換。
  • 索引 33 的值為 2525,與較大的子節點 7070 交換。
  • 索引 22 的值為 4141,先與較大的子節點 9494 交換;下沉到索引 55 後,再與較大的子節點 7575 交換。
  • 索引 11 的值為 3030,依序與較大的子節點 9494、7575、6868 交換,直到符合最大堆條件。

建堆後的陣列為:

| 索引 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |

🔒

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

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

免費註冊

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

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

這一題的完整詳解

核心觀念

二元搜尋樹(BST)中,對任一節點:

  • 左子樹的鍵值小於該節點。
  • 右子樹的鍵值大於該節點。

刪除有兩個子節點的節點時,可用其中序後繼取代。中序後繼是該節點右子樹中的最小值,也就是「大於或等於被刪除值的數字中最小者」。

解題方法

依序插入題目給定的數字,得到下列 BST:

             30
           /    \
         25      41
        / \     /  \
      23  29   37   94
                    /
                   70
                  /  \
                 65  75
                   \
                    68
                   /
                  67

節點 7070 有兩個子節點,因此以其中序後繼取代。7070 的右子樹只有節點 7575,所以其中序後繼是 7575。將 7070 改為 7575,再刪除原本的 7575 節點。

刪除後的樹為:

🔒

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

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

免費註冊

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

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

這一題的完整詳解

核心觀念

樹的高度是根節點到最深葉節點路徑的長度。本題採資料結構常見的定義:路徑長度以邊數計算,因此單一節點的樹高度為 00。

二元樹高度為 hh 時,最多可有

1+2+22+⋯+2h=2h+1−11+2+2^2+\cdots+2^h=2^{h+1}-1

個節點。一般樹若有 nn 個節點,最長的樹形是單鏈,其高度為 n−1n-1。

解題方法

逐一比較各選項的高度與節點數上限:

  • 求最小高度時,使用該高度能容納的最大節點數。
  • 求最大高度時,將節點排成單鏈;邊數比節點數少 11。
  • 完全二元樹有 h+1h+1 層時,節點數介於 2h2^h 與 2h+1−12^{h+1}-1 之間,因此高度為 hh。

選項分析

(A) 正確。 高度為 55 的二元樹最多有 26−1=632^6-1=63 個節點,不足 100100 個;高度為 66 時最多有 27−1=1272^7-1=127 個節點,足以容納 100100 個。因此最小高度是 66。

(B) 錯誤。 一般樹不限制每個節點的子節點數。1515 個節點可以排成一個根節點連接 1414 個葉節點的星狀樹,根到葉只有 11 條邊,所以最小高度是 11,不是 22。

🔒

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

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

免費註冊

其他考古題