109 年 國立中正大學資訊管理學系碩士班乙組《資料結構》

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

第 1 題

Figure 1 is a social network of persons.
(a) Please draw the adjacent array for the network.
(b) Suppose the hub of a social network is defined as the one with the most links upon him/her. In this graph, the hub is Bob. Please write a subroutine (in any programming code) to find the hub. (10 points for each sub-question)

🖼️【此處有附圖,請對照原卷】

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 2 頁

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

這一題的完整詳解

核心觀念

本題考查無向圖的表示法與頂點度數。社交網絡中的每個人是頂點,每條連線是邊;某頂點的度數,就是與它相連的邊數。題目所稱的 hub,是度數最大的頂點。

依圖中連線,頂點順序採 Bob、Charles、Justin、Jack、Mary、Tom。線段交叉處有跨線記號,表示交叉點不構成頂點,也不代表兩人相連。

解題方法

(a) 鄰接矩陣與鄰接串列

圖中邊為 Bob–Charles、Bob–Justin、Bob–Jack、Bob–Mary、Charles–Justin、Jack–Mary、Jack–Tom。因為是無向圖,鄰接矩陣沿主對角線對稱;相連填 11,不相連填 00。

BobCharlesJustinJackMaryTom
Bob011110
Charles101000
Justin110000
Jack100011
Mary100100
Tom000100

相同的圖也可用鄰接串列表示:

  • Bob:Charles、Justin、Jack、Mary
  • Charles:Bob、Justin
  • Justin:Bob、Charles
🔒

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

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

免費註冊

第 2 題

Assume you use AVL tree to store the data and there come the six data 15, 4, 26, 1, 8 and 14 in the order and one by one.
(a) Please draw the AVL tree from an empty AVL tree to insert the data into the tree (i.e., you need to draw six AVL trees to show the six insertions).
(b) Suppose you delete the data 26, and 4 one by one from the tree. Please draw the AVL tree. Also, you need to draw two AVL tree to show the results of the two deletions. (10 points for each sub-question)

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 2 頁原卷第 3 頁

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

這一題的完整詳解

核心觀念

AVL 樹是每個節點都符合平衡條件的二元搜尋樹。每個節點的平衡因子定義為:

BF=左子樹高度−右子樹高度BF=\text{左子樹高度}-\text{右子樹高度}

AVL 樹要求每個節點的 BFBF 都是 −1、0、1-1、0、1。插入或刪除後若有節點失衡,便依失衡型態旋轉:

  • LL 型:對失衡節點右旋。
  • RR 型:對失衡節點左旋。
  • LR 型:先對左子節點左旋,再對失衡節點右旋。
  • RL 型:先對右子節點右旋,再對失衡節點左旋。

解題方法

依序按二元搜尋樹規則插入資料,每次插入後由下往上檢查平衡因子;若失衡,找出第一個失衡節點並旋轉修正。刪除時同樣先按二元搜尋樹規則刪除,再由下往上檢查 AVL 平衡條件。

(a) 依序插入六筆資料

插入 15:

15

插入 4:

  15
 /
4

插入 26:

  15
 /  \
4    26

插入 1:

    15
   /  \
  4    26
 /
1

插入 8:

    15
   /  \
  4    26
 / \
1   8

插入 14:

🔒

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

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

免費註冊

第 3 題

(a) Write a subroutine to count the number of nodes in a double circularly linked list. Suppose each node in the circular linked list has three fields, as follows:
node
data: string
Rlink: *node
Llink: *node
In addition, there is a link variable, Top, pointing to a node in the circular linked list.
(b) Suppose you want to delete a node with data being 'CCU'. Please write the subroutine to do it. Note your program should consider the situation that the data with value being 'CCU' may or may not exist in the double circularly linked list. (10 points for each sub-question)

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 2 頁原卷第 3 頁

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

這一題的完整詳解

核心觀念

本題考查**雙向環狀鏈結串列(Double Circularly Linked List)**的基本操作與指標操作能力:

  1. 結構定義:每個節點包含三個欄位:data(資料)、Rlink(指向右/下一個節點的指標)與 Llink(指向左/前一個節點的指標)。
  2. 環狀特徵(Circular Property):若串列非空,任一節點沿著 Rlink 最終都會繞回自身;終止遍歷的條件不是遇到 NULL,而是指針再次等於起始節點(例如 curr == Top)。
  3. 節點計數(Counting):需要注意串列是否為空(Top == NULL)的邊界情況;若非空,以 Top 為起點遍歷一圈回到 Top 即可統計節點總數。
  4. 節點刪除(Deletion):雙向環狀串列刪除節點需維護前驅與後繼節點的雙向指標。特別需處理邊界條件:
    • 串列為空串列(無節點)。
    • 串列僅剩單一節點(刪除後串列變為空串列)。
    • 欲刪除的節點恰好為指標變數 Top 所指的節點(刪除後需更新 Top)。
    • 找不到欲刪除的資料(資料不存在)。

解題方法

(a) 計算雙向環狀鏈結串列之節點數

  1. 處理空串列:若 Top == NULL,直接回傳 0。
  2. 走訪計數:設指標變數 curr = Top,計數器 count = 0。
  3. 迴圈終止條件:採用 do-while 迴圈或先前進再判斷,每造訪一個節點 count++ 並將 curr = curr->Rlink,直到 curr == Top 為止,回傳 count。

(b) 刪除值為 'CCU' 的節點

  1. 處理空串列:若 Top == NULL,串列為空,直接結束。
  2. 搜尋目標節點:以 curr = Top 出發走訪環狀串列,比對 curr->data == "CCU"。若遍歷一整圈皆未找到,表示資料不存在,直接返回。
  3. 刪除與指標重串接(Re-linking):
    • 情況一:串列只有一個節點(curr->Rlink == curr):釋放該節點記憶體,並將外部指標變數更新為 Top = NULL。
    • 情況二:串列有多個節點:
      • 若刪除的剛好是 Top 所指節點,需先將 Top 指向鄰近節點(例如 Top = curr->Rlink)。
      • 調整雙向鏈結:
        curr→Llink→Rlink=curr→Rlinkcurr\to Llink\to Rlink = curr\to Rlink
        curr→Rlink→Llink=curr→Llinkcurr\to Rlink\to Llink = curr\to Llink
      • 釋放 curr 所佔用的記憶體。

程式實作

以 C/C++ 語法實作對應副程式(Subroutine):

🔒

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

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

免費註冊

第 4 題

(a) Write a recursive function to add all the elements in a n×nn \times n (two dimensional) array of integers, n≥1n \ge 1. For example, the result of

[3819]\begin{bmatrix} 3 & 8 \\ 1 & 9 \end{bmatrix}

for the addition is 33.
(b) Assume the addition of two matrixes is defined as the addition of the two elements in the same position of the two arrays. For example,

[3819]+[103−213]=[1311−122]\begin{bmatrix} 3 & 8 \\ 1 & 9 \end{bmatrix} + \begin{bmatrix} 10 & 3 \\ -2 & 13 \end{bmatrix} = \begin{bmatrix} 13 & 11 \\ -1 & 22 \end{bmatrix}

(Note: your answer must be written in a recursive function, otherwise, no points). (10 points for each sub-question)

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 3 頁

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

這一題的完整詳解

題目辨識

題目文字與附圖內容不一致;附圖實際顯示的是「八面骰子擲 100 次」的 C++ 題,以下依附圖中的第 B-4 題作答。

核心觀念

本題考查:

  • 陣列索引與陣列大小的關係
  • rand() 產生指定範圍亂數
  • 頻率統計陣列
  • 二維陣列的使用
  • C++ 輸出欄位寬度 setw()

八面骰子的點數範圍為 11 至 88,因此亂數應寫成:

1+rand⁡()%81+\operatorname{rand}()\%8

頻率陣列至少需要索引 00 至 88,其中索引 00 不使用,因此大小應為 99。

(a)找出錯誤並修正

第 3 行錯誤

原程式:

int size = 7;

題目為八面骰子,骰面應為 11 至 88,因此應改為:

int size = 8;

第 4 行

修正後:

int freq[size] = {};

此時陣列索引為 00 至 77,無法存放骰面 88,因此必須配合索引方式改成:

int freq[size + 1] = {};

第 8 行錯誤

原程式只有存取陣列:

freq[1 + rand() % 7];

沒有將對應頻率加一,且亂數只會產生 11 至 77。應改為:

freq[1 + rand() % 8]++;

修正後完整程式

int main()
{
    int size = 8;
    int freq[size + 1] = {};

    srand(time(0));

    for (int roll = 1; roll <= 100; roll++)
        freq[1 + rand() % size]++;

    cout << "Dice Face" << setw(13) << "Frequency" << endl;

    for (int face = 1; face <= size; face++)
        cout << setw(9) << face
             << setw(13) << freq[face] << endl;
}

複雜度

  • 時間複雜度:O(100)O(100),一般化為擲骰子 mm 次時為 O(m)O(m)
  • 空間複雜度:O(8)O(8),一般化為 O(k)O(k),其中 kk 為骰面數

(b)統計兩顆八面骰子的結果

第一維表示第一顆骰子的點數,第二維表示第二顆骰子的點數,因此使用:

freq[firstFace][secondFace]

兩顆骰子的點數皆為 11 至 88,所以二維陣列大小設為 9×9‘,索引9 \times 9`,索引 0

🔒

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

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

免費註冊

第 5 題

Figures 3(a) and 3(b) are the depth-first and breath-first search algorithms, respectively. The two algorithms are annotated with numbers before each of their statements. Please write the execution sequence in these numbers step by step to travel the graph from vertex 3 in Figure 3(c). If more than one link can be traverse, the algorithm always chooses the link the smallest one in alphabetical order.

You only need to write the step numbers, like 1, 2, 3, 4, 3, 4, to answer the following two sub-questions.
(a) Travel the graph in DFS manner;
(b) Travel the graph in BFS manner. (10 points for each sub-question)

🖼️【此處有附圖,請對照原卷】

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 3 頁原卷第 4 頁

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

這一題的完整詳解

核心觀念

本題考查深度優先搜尋(DFS)與廣度優先搜尋(BFS)的邊標記、頂點探索狀態,以及依指定順序追蹤演算法行號。DFS 遇到已探索的邊,只執行第 1、2 行;遇到尚未探索的邊,再檢查另一端頂點是否已探索。BFS 則將新發現的頂點排入下一層,並把連到已發現頂點的邊標記為交叉邊。

解題方法

圖 3(c) 是無向圖,頂點為 1,2,3,41,2,3,4,邊為 a=(1,4)a=(1,4)、b=(1,2)b=(1,2)、c=(4,2)c=(4,2)、d=(4,3)d=(4,3)、e=(2,3)e=(2,3),起點是頂點 33。各頂點依相連邊的字母順序掃描;以下只列有編號的敘述行。

(a) DFS

從頂點 33 出發,先沿 dd 到 44,再沿 aa 到 11,再沿 bb 到 22。在頂點 22,邊 cc、ee 尚未探索,但另一端頂點已探索,因此各執行第 7、8 行,標記為回邊。返回上層後,已標記的邊只會執行第 1、2 行。

目前頂點掃描邊處理情形行號
3d發現頂點 41, 2, 3, 4, 5, 6
4a發現頂點 11, 2, 3, 4, 5, 6
1a邊已探索1, 2
1b發現頂點 21, 2, 3, 4, 5, 6
2b邊已探索1, 2
2c頂點 4 已探索,標記回邊1, 2, 3, 4, 7, 8
2e頂點 3 已探索,標記回邊1, 2, 3, 4, 7, 8
4c邊已探索1, 2
4d邊已探索1, 2
3e邊已探索1, 2

(b) BFS

初始化後,先處理第 0 層 {3}\{3\},發現頂點 44、22。接著依序處理第 1 層 {4,2}\{4,2\}:頂點 44 經由 aa 發現頂點 11;

🔒

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

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

免費註冊

其他考古題