0% found this document useful (0 votes)
10 views11 pages

2023-Quiz2 Ans

The document outlines various tasks related to binary trees and graphs, including calculating maximum nodes, inserting records, tree traversal, constructing binary search trees, and heap operations. It also covers graph representation using adjacency matrices and lists, counting node degrees, and determining the maximum number of edges in an undirected graph. Each task includes grading criteria and sample code for implementation.

Uploaded by

張添淞
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views11 pages

2023-Quiz2 Ans

The document outlines various tasks related to binary trees and graphs, including calculating maximum nodes, inserting records, tree traversal, constructing binary search trees, and heap operations. It also covers graph representation using adjacency matrices and lists, counting node degrees, and determining the maximum number of edges in an undirected graph. Each task includes grading criteria and sample code for implementation.

Uploaded by

張添淞
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1.

(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;
};

void preorder(tree_pointer ptr){


if(ptr){
printf("%d", ptr -> data);
preorder(ptr -> left_child);
preorder(ptr -> right_child);
}
}

void inorder(tree_pointer ptr){


if(ptr){
inorder(ptr -> left_child);
printf("%d", ptr -> data);
inorder(ptr -> right_child);
}
}
Preorder: 7, 9, 4, 8, 12, 16, 19, 33, 26, 42, 23
Inorder: 8, 4, 12, 9, 19, 16, 33, 7, 42, 26, 23

(4)
※評分標準:
圖 4 分,程式碼 6 分。
邏輯錯誤不給分。
未考慮部分條件扣 3 分。
程式內⼀個⼩錯扣 1 分,扣完為⽌。
struct node{
int data;
struct node* left;
struct node* right;
};

struct node* createNode(value){


struct node* newNode = malloc(sizeof(struct node));
newNode -> data = value;
newNode -> left = NULL;
newNode -> right = NULL;

return newNode;
}
struct node* insert(struct node* root, int data){
if (root == NULL) return createNode(data);

if (data < root -> data)


root -> left = insert(root -> left, data);
else if (data > root -> data)
root -> right = insert(root -> right, 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;

return 1 + countNonleaf(root->left) + countNonleaf(root->right);


}

(11)
※評分標準:
語法錯誤⼀個扣 1 分,邏輯錯誤 0 分

1. 加總 Node 裡為偶數的值

//[1] linked List


struct node{
int data;
struct node *L_next;
struct node *R_next;
};
typedef struct node Node;

int sum = 0;

void countEvenNode(Node *ptr)


{
if (ptr != NULL)
{
if (ptr -> data %2 ==0)
{
sum += ptr -> data;
}
countEvenNode(ptr -> L_next);
countEvenNode(ptr -> R_next);
}
}

// [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

//[1] linked List


struct node{
int data;
struct node *L_next;
struct node *R_next;
};
typedef struct node Node;

int c = 0;

void countEvenNode(Node *ptr)


{
if (ptr != NULL)
{
if (ptr -> data %2 ==0)
{
c++;
}
countEvenNode(ptr -> L_next);
countEvenNode(ptr -> R_next);
}
}

// [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分

沒有考慮到⾛訪成本扣 2 分,adjacency matrix 主對⾓線沒有補 0 扣 1 分

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 list : 錯⼀個扣1分,最多扣4分,如果沒有標⽰ NULL 扣 2 分


(2)
※評分標準:
程式有⼩錯誤⼀個扣 1 分,邏輯錯誤 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;

for (i = 0; i < M; i++)


{
degree[i] = 0;
for (j = 0; j < N; j++)
{
if (adj_mat[i][j] == 1)
{
degree[i]++;
}
}
}

for (i = 0; i < M; i++)


{
printf("Degree of node %d: %d\n", i, degree[i]);
}

return 0;
}

adjacency list
#include <stdio.h>
#include <stdlib.h>

#define V 8 // number of vertices

typedef struct node *node_pointer;


typedef struct node {
int vertex;
struct node *link;
} node;

node_pointer graph[V]; // adjacency list

// function to add an edge to the graph


void add_edge(int u, int v) {
node_pointer temp;
temp = (node_pointer) malloc(sizeof(node));
temp->vertex = v;
temp->link = graph[u];
graph[u] = temp;

temp = (node_pointer) malloc(sizeof(node));


temp->vertex = u;
temp->link = graph[v];
graph[v] = temp;
}

// function to count the degree of each node


void count_degrees() {
int i, degree;
node_pointer temp;

for (i = 0; i < V; i++) {


degree = 0;
temp = graph[i];

// traverse the adjacency list for the i-th vertex


while (temp != NULL) {
degree++;
temp = temp->link;
}

printf("Degree of node %d: %d\n", i, degree);


}
}
int main() {
// add the edges to the graph
add_edge(0, 1);
add_edge(0, 2);
add_edge(0, 7);
add_edge(1, 2);
add_edge(2, 3);
add_edge(2, 7);
add_edge(3, 4);
add_edge(3, 5);
add_edge(3, 7);
add_edge(4, 5);
add_edge(5, 6);
add_edge(6, 7);

// count the degrees of each node


count_degrees();

return 0;
}

(3) for undirected graph with n vertices, the maximum number of edges is n(n-1)/2

You might also like