高普考題庫
108 年 108年公務人員高等考試三級考試暨普通考試

資料結構

本卷皆為申論題,點「看答案與解析」查看擬答。

申論 1給予如下二元樹節點的宣告,分別寫出C 的遞迴程式計算二元樹節點個數及計算二元樹葉節點(leaves)個數(Count the number of nodes in abinary tree and count the number of leaf nodes in a binary tree,respectively)。(25 分)struct node{int info;struct node *left;struct node *right;}typedef struct node *NODEPTR;void countTree(NODEPTR tree){}void countLeaves(NODEPTR tree){}
申論 2給予如下二元樹節點的宣告,寫一C 的遞迴程式swapTree(NODEPTRtree)將每一節點的左、右節點互換(Swap the left and right children ofevery node of a binary tree)。(25 分)struct node{int info;struct node *left;struct node *right;}typedef struct node *NODEPTR;void swapTree(NODEPTR tree){}
申論 3給予如下程式,假設x[] = [30, 75, 53, 47, 21, 94, 88, 39],lb = 0,ub = 7,請問執行完下列程式後,x[]的內容為何?(25 分)void divide&conquer(int x[], int lb, int ub, int *pj){int a, down, temp, up;a = x[lb];up = ub;down = lb;while(down < up){while(x[down] <= a && down < ub)down++;while(x[up] > a)up--;if(down < up){temp = x[down];x[down] = x[up];x[up] = temp;}}x[lb] = x[up];x[up] = a;*pj = up;}
申論 4用G = (V, E)表示一個無方向性圖形,其中V 是點的集合,E 是一組節點(Vertices)形成一個邊及對應權重(Weights)所組成的集合,例如:(0, 1, 28)表示節點0 至節點1 有一個邊,而且權重為28。今有一圖形G =(V, E),V = {0, 1, 2, 3, 4, 5, 6},E = {(0, 1, 27), (1, 2, 15), (2, 3, 11), (0, 5, 9),(1, 6, 13), (4, 5, 24), (4, 6, 23), (3, 4, 21), (3, 6, 17)}。請利用Kruskal 演算法計算最小擴張樹(Minimum spanning tree)之最低權重或成本值。(25 分)