2023-Quiz2 Ans
2023-Quiz2 Ans
(80%)
(1) What is the maximum number of nodes on level i for a binary tree T? What is the
maximum number of nodes in T, if the tree depth is h? (6%)
(2) Insert the following records to a binary tree level by level. (6%)
(7, 9, 26, 4, 16, 42, 23, 8, 12, 19, 33)
(3) Give the pre-order and in-order tree traversal procedures and use them to trace the
above tree. (8%)
(4) Use “binary search tree” procedure to construct the tree from the records listed in (2). (10%)
(5) Show how to remove the number 16 from the tree. (6%) → 從 "Binary Search Tree” 將 16 移除
(6) Transfer the binary tree in (2) to a max heap. (8%) → 要畫出轉換過程
(7) Show how to insert the number 38 into the heap. (6%)
(8) Show how to remove the number 12 from the heap. (6%) → 可⽤ (6) 或 (7) 的 heap 接續
(9) Use the concept of max heap to sort the above numbers. (8%)
(10) Write an algorithm to count the number of internal nodes in a binary tree. (8%)
(11) Write a procedure to count the “even” nodes of the tree. (8%)
(1)
※評分標準:
⼀⼩題 3 分。
the maximum number of nodes on level i for a binary tree T is 2i−1
the maximum number of nodes on depth h for a binary tree T is 2h − 1
(2)
※評分標準:
樹畫錯不給分。
(3)
※評分標準:
程式⼀⼩題 3 分,序列⼀⼩題 1 分。
typedef struct node *tree_pointer;
typedef struct node {
int data;
tree_pointer left_child, right_child;
};
(4)
※評分標準:
圖 4 分,程式碼 6 分。
邏輯錯誤不給分。
未考慮部分條件扣 3 分。
程式內⼀個⼩錯扣 1 分,扣完為⽌。
struct node{
int data;
struct node* left;
struct node* right;
};
return newNode;
}
struct node* insert(struct node* root, int data){
if (root == NULL) return createNode(data);
return root;
}
(5)
※評分標準:
非選擇 node 16 left subtree 中最⼤ (12) 或 right subtree 中最⼩ (19) 不給分。
(6)
※評分標準:
最後 max heap 錯誤不給分。
只有最後答案正確,過程錯扣 6 分。
沒畫 branch 扣 2 分。
(7)
※評分標準:
最後 max heap 錯誤不給分。
沒畫 branch 扣 2 分。
(8)
※評分標準:
最後 max heap 錯誤不給分。
沒畫 branch 扣 2 分。
(9)
※評分標準:
使⽤文字或圖⽰解釋如何使⽤ Max heap 進⾏數值的排序(Heap sort),若只敘述 Max heap
的概念 未解釋如何做排序扣 4 分,依據內容完整度斟酌扣分
文字敘述 :
Heap sort : 排序時, 將 Max heap 的 root 取出 , 並與最後⼀個節點互換位置, 再持續調整以符
合 Max heap 的規範, 重複進⾏上述步驟直到整顆 Tree 為空, 按照順序所取出來的 root 即為
排序好的數列。
圖⽰ :
(10)
※評分標準:
程式內⼀個⼩錯扣 2 分,扣完為⽌,邏輯錯誤 0 分
int countNonleaf(struct Node* root){
if (root == NULL || (root->left == NULL && root->right == NULL))
return 0;
(11)
※評分標準:
語法錯誤⼀個扣 1 分,邏輯錯誤 0 分
1. 加總 Node 裡為偶數的值
int sum = 0;
// [2] array
int main(int argc, const char * argv[]) {
int binarytree[] = {8, 4, 12, 9, 19, 16, 33, 7, -1,
42, -1, 26, -1, 23, -1};
int sum = 0;
int i=0;
for (i=0;i<pow(2, 4)-1; i++) {
if (binarytree[i] %2 == 0) {
sum += binarytree[i];
}
}
printf("%d\n", sum);
}
2. 計算總共有幾個偶數的 Node
int c = 0;
// [2] array
int main(int argc, const char * argv[]) {
int binarytree[] = {8, 4, 12, 9, 19, 16, 33, 7, -1,
42, -1, 26, -1, 23, -1};
int count = 0;
int i=0;
for (i=0;i<pow(2, 4)-1; i++) {
if (binarytree[i] %2 == 0) {
count++;
}
}
printf("%d\n", count);
}
2. (20%)
For a graph G = (V, E), V = {0, 1, 2, 3, 4, 5, 6, 7}, E = {(0, 1), (0, 2), (0, 7), (1, 2), (2, 3), (2, 7), (3,
4), (3, 5), (3, 7), (4, 5), (5, 6), (6, 7)}, and the cost for above edges are {16, 13, 27, 11, 20, 6, 9, 18,
19, 14, 26, 10}, respectively.
(1) Use adjacency matrix and adjacency list to represent this graph. (8%)
(2) Write a procedure to count the degree of each node. (6%)
(3) For an undirected graph with n vertices, what is the maximum number of edges? (6%)
(1)
※評分標準:
adjacency matrix : 數值錯⼀格扣1分,最多扣4分
0 1 2 3 4 5 6 7
0 0 16 13 27
1 16 0 11
2 13 11 0 20 6
3 20 0 9 18 19
4 9 0 14
5 18 14 0 26
6 26 0 10
7 27 6 19 10 0
adjacency matrix
#include <stdio.h>
#define M 8
#define N 8
int main()
{
int adj_mat[M][N] = {{0, 1, 1, 0, 0, 0, 0, 1},
{1, 0, 1, 0, 0, 0, 0, 0},
{1, 1, 0, 1, 0, 0, 0, 1},
{0, 0, 1, 0, 9, 1, 0, 1},
{0, 0, 0, 1, 0, 1, 0, 0},
{0, 0, 0, 1, 1, 0, 1, 0},
{0, 0, 0, 0, 0, 1, 0, 1},
{1, 0, 1, 1, 0, 0, 1, 0}};
int degree[N];
int i, j;
return 0;
}
adjacency list
#include <stdio.h>
#include <stdlib.h>
return 0;
}
(3) for undirected graph with n vertices, the maximum number of edges is n(n-1)/2