114 年 國立成功大學工程科學系碩士班乙組《資料結構》
第 1 題20 分
- Given the following two traversals of a binary tree. (20%)
- Preorder traversal: F, B, A, D, C, E, G, I, H
- Inorder traversal: A, B, C, D, E, F, G, H, I
Answer the following questions:
(1) Reconstruct the binary tree based on the given preorder and inorder traversals and draw the structure of the tree. (10%)
(2) Calculate the sum of the depths of all nodes in the reconstructed tree. Assume the root has a depth of 0. (10%)
登入後即可作答並保存紀錄。
核心觀念
前序走訪的順序是「根、左子樹、右子樹」;中序走訪的順序是「左子樹、根、右子樹」。因此,前序序列的第一個節點是整棵樹的根,而該節點在中序序列中的位置,能將其餘節點分成左、右子樹。
解題方法
前序序列第一個節點是 ,所以 為根。在中序序列 中, 左側的 屬於左子樹,右側的 屬於右子樹。
左子樹的前序序列為 ,故其根為 。在對應的中序序列 中, 左側只有 ,右側為 。因此, 是 的左子節點;右子樹根為 ,且 的左子節點為 、右子節點為 。
右子樹的前序序列為 ,故其根為 。在對應的中序序列 中, 沒有左子樹,右子樹包含 ;該右子樹的根為 ,而 是 的左子節點。
第 2 題20 分
- Consider the following recursive function, where the input is an integer n
def mystery(n):
if n <= 1:
return 1
else:
return mystery(n/2) + mystery(n/2)
Analyze the time complexity of this function and express the result using big-O notation. (20%)
登入後即可作答並保存紀錄。
核心觀念
本題主要考驗遞迴函式之時間複雜度分析,核心觀念包含以下三項:
- 遞迴關係式(Recurrence Relation)的建立:根據函式內部的條件分歧與 recursive calls 的次數,將執行時間表述為輸入規模 的遞迴關係式。
- 遞迴樹法(Recursion Tree Method):將遞迴呼叫展開成樹狀結構,計算樹的高度、每層的節點數與工作量,最終加總求得總複雜度。
- 主定理(Master Theorem):用於快速求解符合 形式之分治遞迴關係式漸近界限的定理。
解題方法
步驟一:建立遞迴關係式(Recurrence Relation)
觀察程式碼 mystery(n):
- Base Case:當 時,執行
if判斷並回傳1,花費常數時間 。 - Recursive Case:當 時,回傳敘述中寫著
mystery(n/2) + mystery(n/2)。注意這裡呼叫了mystery(n/2)兩次,加上一次加法運算與函式傳參,非遞迴部分的額外基本運算花費 。
因此,設 為輸入為 時的執行時間,可建立遞迴關係式:
步驟二:時間複雜度推導(提供兩種標準解法)
解法 A:遞迴樹法(Recursion Tree Method)
-
樹的高度:每次將問題規模折半(),直到規模縮小至 。樹的高度為 。
-
每層節點數與工作量:
- 第 層(根節點): 個節點,規模為 ,工作量為 。
- 第 層: 個節點,規模為 ,工作量為 。
- 第 層: 個節點,規模為 ,工作量為 。
- 第 層: 個節點,規模為 ,工作量為 。
- 第 層(葉節點): 個葉節點,工作量為 。
-
總工作量加總:
利用等比級數求和公式:
第 3 題20 分
- Consider the following graph. (20%)
🖼️【此處有附圖,請對照原卷】
(1) Starting from node E, please use depth-first search, prioritizing smaller weights first, to calculate the sum of the weights. (10%)
(2) Starting from node E, please use breadth-first search, prioritizing smaller weights first, to calculate the sum of the weights. (10%)
登入後即可作答並保存紀錄。
核心觀念
本題考加權圖的深度優先搜尋(DFS)與廣度優先搜尋(BFS),並依搜尋順序計算首次發現新節點時所經過的邊之權重總和。DFS 會沿著目前可走且尚未拜訪的最小權重邊深入,無路可走時回溯;BFS 則用佇列依序處理節點,並按邊權重由小到大將新節點加入佇列。
解題方法與計算
圖中有頂點 ,無向邊及權重為 、、、、、、、、、。圖上權重 對應邊 ,沒有 邊。以下從 開始,節點在首次發現時即標記為已拜訪;只加總搜尋樹中的發現邊。
(1) 深度優先搜尋
從 出發,依未拜訪鄰點的邊權重由小到大選擇:
| 步驟 | 搜尋動作 | 邊權重 |
|---|---|---|
| 1 | 2 | |
| 2 | 7 | |
| 3 | 5 | |
| 4 | 1 |
第 4 題20 分
- What type of sorting algorithm is being executed in the process of changing the contents of the following array?
(20%)
[5, 4, 3, 2, 1]>>[1, 4, 3, 2, 5]>>[4, 1, 3, 2, 5]>>[4, 2, 3, 1, 5]>>[1, 2, 3, 4, 5]
[3, 2, 1, 4, 5]>>[1, 2, 3, 4, 5]>>[2, 1, 3, 4, 5]>>[1, 2, 3, 4, 5]
登入後即可作答並保存紀錄。
在每一次遍歷中,先自左向右比較相鄰元素並將較大的向右移(等同於一次 bubble‑up),接著再自右向左比較相鄰元素將較小的向左移(一次 bubble‑down)。
此「左右來回」的兩階段交替執行,使得每回合同時把最大值送至最右端、最小值送至最左端,遂出現序列
第 5 題20 分
- Consider the following graph. (20%)
🖼️【此處有附圖,請對照原卷】
(1) Using Prim's algorithm, start from node A and determine the weight of the third edge added during the construction of the minimum spanning tree. (10%)
(2) Using Kruskal's algorithm, determine the weight of the fourth edge added during the construction of the minimum spanning tree. (10%)
登入後即可作答並保存紀錄。
核心觀念
最小生成樹(MST)要以最小總權重連接所有頂點,且不形成迴圈;若有 個頂點,生成樹恰有 條邊。
- Prim 演算法:從指定頂點開始,每次加入一條連接「已加入頂點」與「未加入頂點」的最小權重邊。
- Kruskal 演算法:將所有邊依權重由小到大排列,逐一加入不會形成迴圈的邊。
解題方法
依原卷圖,頂點為 ;圖上的邊與權重為 、、、、、、、、、、、。圖示沒有 邊。
(1) Prim 演算法,從 開始
從 出發,先選連到未加入頂點的最小邊;之後每步都在連接已加入與未加入頂點的邊中選最小者:
| 加入順序 | 選取的邊 | 權重 |
|---|---|---|
| 第 1 條 | ||
| 第 2 條 | ||
| 第 3 條 | ||
| 第 4 條 | ||
| 第 5 條 | ||
| 第 6 條 |