115 年 國立政治大學資訊管理學系碩士班科技組《資料結構》
第 1 題10 分
Figure 1 shows a binary tree in which each node stores a string.
🖼️【此處有附圖,請對照原卷】
Tree Traversal and Heap Construction
- (10%) Define a recursive pre-order traversal algorithm that prints the strings stored in the nodes of a binary tree. You may write the algorithm in pseudocode or Java. Then, apply your algorithm to the binary tree shown in Figure 1 and write down the resulting output sequence.
登入後即可作答並保存紀錄。
核心觀念
本題考查**二元樹走訪(Binary Tree Traversal)中的前序走訪(Pre-order Traversal)**之遞迴定義與追蹤應用。
二元樹的前序走訪核心定義為「根節點 左子樹 右子樹」():
- 拜訪根節點(Visit Root):輸出當前節點儲存的字串資料。
- 走訪左子樹(Traverse Left Subtree):遞迴呼叫前序走訪函式處理左子節點。
- 走訪右子樹(Traverse Right Subtree):遞迴呼叫前序走訪函式處理右子節點。
遞迴終止條件(Base Case)為:當節點為空(null)時直接返回。
解題方法
1. 遞迴前序走訪演算法(Java 實作)
定義二元樹節點結構 TreeNode 與靜態遞迴方法 preOrderTraversal:
// 二元樹節點定義
class TreeNode {
String data;
TreeNode left;
TreeNode right;
TreeNode(String data) {
this.data = data;
this.left = null;
this.right = null;
}
}
public class BinaryTreeTraversal {
// 遞迴前序走訪演算法
public static void preOrderTraversal(TreeNode node) {
// Base Case:空節點直接返回
if (node == null) {
return;
}
// 1. 拜訪並印出當前節點儲存的字串
System.out.print(node.data + " ");
// 2. 遞迴走訪左子樹
preOrderTraversal(node.left);
// 3. 遞迴走訪右子樹
preOrderTraversal(node.right);
}
}
複雜度分析
- 時間複雜度:,其中 為樹中節點總數(本題 )。每個節點均被存取常數次。
- 空間複雜度:,其中 為二元樹的高度(本題 )。空間消耗來自系統執行遞迴時呼叫堆疊(Call Stack)的深度。在最差情況(單斜樹)為 ,最佳情況(完全平衡樹)為 。
2. 套用於 Figure 1 之推導步驟
依據原卷 Figure 1,二元樹的階層與父子結構如下:
- 根節點:
Computers"R"Us- 左子節點:
SalesSales的左子節點:US(葉節點)
- 左子節點:
第 2 題20 分
Figure 1 shows a binary tree in which each node stores a string.
🖼️【此處有附圖,請對照原卷】
Tree Traversal and Heap Construction
- (20%) Construct a min heap by inserting the strings one by one in the order obtained from Q1. The heap ordering rule is defined as follows:
(1) Strings with shorter length have higher priority.
(2) If two strings have the same length, they are compared lexicographically from left to right using the Ord encoding defined below.
The character encoding Ord is defined as:
Ord('A') = 1, Ord('B') = 2, ..., Ord('Z') = 26
Ord('a') = 27, Ord('b') = 28, ..., Ord('z') = 52
Ord("") = 0
Draw the heap as a binary tree after all insertions.
登入後即可作答並保存紀錄。
1. 前序走訪(Pre-order Traversal)順序
依據前序走訪(根 左 右),Q1 走訪 Figure 1 得到的字串插入順序為:
Computers"R"Us(長度 14)Sales(長度 5)US(長度 2)International(長度 13)Asia(長度 4)Canada(長度 6)Manufacturing(長度 13)Laptops(長度 7)Desktops(長度 8)
2. Min Heap 優先權排序規則
- 規則 (1):字串長度越短,優先權越高(越小,越靠近根節點)。
- 規則 (2):若字串長度相同,依 編碼由左至右進行字典順序比較(大小關係為 )。
長度相同的字串比較:
International(長度 13) 與Manufacturing(長度 13);第一個字元 ,因此優先權為:International>Manufacturing。
字串優先權由高至低(小至大)排序為:
3. 逐一插入與堆積調整步驟
以一維陣列 表示二元堆積(索引 的左子節點為 ,右子節點為 ):
- 插入
Computers"R"Us: - 插入
Sales:- 插入於 ,與 比較,長度 ,向上調整(Up-heap)。
- 插入
US:- 插入於 ,與 比較,長度 ,向上調整。
- 插入
International:- 插入於 ,與父節點 比較,長度 ,向上調整。
- 插入
Asia:
第 3 題10 分
Figure 1 shows a binary tree in which each node stores a string.
🖼️【此處有附圖,請對照原卷】
AVL Tree Construction
- (10%) Define an in-order traversal algorithm that prints the strings stored in the nodes of a binary tree. Apply it to the tree in Figure 1 and write down the resulting output sequence.
登入後即可作答並保存紀錄。
核心觀念
本題考查二元樹的「中序走訪」(in-order traversal)。其固定順序為:
每個節點儲存一個字串,因此走訪時依上述順序依序印出節點中的字串。
解題方法
遞迴演算法如下:
InOrder(node):
if node ≠ NULL:
InOrder(node.left)
print(node.string)
InOrder(node.right)
若以空樹作為遞迴終止條件,則:
- 先走訪目前節點的左子樹;
- 再印出目前節點儲存的字串;
- 最後走訪目前節點的右子樹。
因此,套用至 Figure 1 時,必須從最左側節點開始,依「左、根、右」順序逐一列出字串。
第 4 題20 分
Figure 1 shows a binary tree in which each node stores a string.
🖼️【此處有附圖,請對照原卷】
AVL Tree Construction
- (20%) Construct an AVL tree by inserting the strings one by one according to the output sequence of Q3. Use the same string comparison rule as in Q2 when ordering keys in the AVL tree. Clearly indicate where rotations occur and show the resulting tree.
登入後即可作答並保存紀錄。
一、 Q3 中序走訪(In-order Traversal)輸出序列
依左子樹 根節點 右子樹之順序走訪 Figure 1,其輸出序列為:
二、 鍵值排序規則與屬性長度
AVL Tree 的排序規則與 Q2 相同:
- 長度優先:字串長度較短者,其鍵值較小(排在左側)。
- 長度相同時以字元編碼(Ord)字典序比較:從左至右比較,首個不同字元之 值較小者,其鍵值較小。
- 其中 ,,。
各字串鍵值之長度與排序基準如下:
- (長度 2)
- (長度 4)
- (長度 5)
- (長度 6)
- (長度 7)
- (長度 8)
- (長度 13) 與 (長度 13):首字 ,故 。
- (長度 14)
三、 AVL Tree 逐一插入與旋轉步驟
1. 插入 US
US
2. 插入 Sales
因 ,插入右子樹:
US
\
Sales
3. 插入 Asia
因 ,插入為 的左子樹:
US (BF = -2)
\
Sales (BF = 1)
/
Asia (BF = 0)
- 旋轉:根節點 失衡(),其插入路徑為右偏後左偏(RL),在 處執行 RL 雙旋轉:
- 對 進行右旋轉。
- 對 進行左旋轉。
- 旋轉後結果:
Asia
/ \
US Sales
4. 插入 International
因 ,插入至 的右子樹:
Asia
/ \
US Sales
\
International
5. 插入 Canada
因 ,插入至 的左子樹:
Asia (BF = -1)
/ \
US Sales (BF = -2)
\
International (BF = 1)
/
Canada (BF = 0)
第 5 題10 分
Figure 1 shows a binary tree in which each node stores a string.
🖼️【此處有附圖,請對照原卷】
Hash Table Construction
- (10%) Define a post-order traversal algorithm that prints the strings stored in the nodes of a binary tree. Apply it to the tree in Figure 1 and write down the resulting output sequence.
登入後即可作答並保存紀錄。
核心觀念
Post-order traversal(後序走訪)遵循:
因此,每個節點必須等其左、右子樹都走訪完成後,才輸出該節點儲存的字串。
解題方法
遞迴演算法如下:
PostOrder(node):
if node == NULL:
return
PostOrder(node.left)
PostOrder(node.right)
print(node.string)
套用到 Figure 1 時,依序執行:
第 6 題30 分
Figure 1 shows a binary tree in which each node stores a string.
🖼️【此處有附圖,請對照原卷】
Hash Table Construction
- (30%) Construct a hash table of size 13 by inserting the strings in the order obtained from Q5. Use double hashing to resolve collisions.
For a string s:
HashCode(s) = the sum of Ord(c) for each character c in s. (Use the Ord encoding defined above.)
H(s) = HashCode(s) mod 13
D(s) = 11 - (HashCode(s) mod 11)
The ith probe position is given by (H(s) + i · D(s)) mod 13, for i = 0, 1, 2, ...
You may assume that the probing sequence will not cycle and that an empty slot will always be found.
Show the probing sequence (i = 0, 1, 2, ...) for each insertion.
登入後即可作答並保存紀錄。
資料不足說明
題目所需的「Q5 取得的字串插入順序」、「Figure 1 的二元樹」及「Ord encoding 定義」未提供;目前附圖內容為物理化學考卷,並非本題的二元樹。因此無法唯一計算每個字串的實際雜湊位置與最終雜湊表。以下給出可直接套用的定案解法。
核心觀念
本題考查:
- 二元樹走訪結果作為字串插入順序。
- 字串雜湊值的計算。
- 雙重雜湊(double hashing)處理碰撞。
- 雜湊表大小為 ,索引為 至 。
對每個字串 :
初始位置:
探查步距:
第 次探查位置:
其中 是第一次嘗試的位置。
解題方法
先依 Q5 指定的二元樹走訪方式取得字串順序,記為:
接著依序處理每個字串。
對任一字串 :
1. 計算字串雜湊碼
將字串中每個字元的 Ord 值相加:
2. 計算初始位置與步距
3. 依序探查
計算:
依此類推,直到找到空格為止。每次插入都必須保留完整探查序列。