Page 1

1. a.

Maximum number of nodes = 1 + 2 + 4 + 8 + … + 2k-1 = 2k – 1 //只有答案扣 1 分 //投影片 tree01 P20

b. n = 2k – 1, ∴ k =

log2(n+1)┐

//少上界┌ ┐扣 1 分

//投影片 tree01 P21 c.

//投影片 tree01 P28  Start with a full binary tree that has at least n nodes. //1 分  Number the nodes as described earlier. //1 分  The binary tree defined by the nodes numbered 1 through n is the unique n node complete binary tree. //2 分

d. //投影片 tree02 P36  A binary tree. //各 1 分  Each node has a (key, value) pair.  For every node x, all keys in the left subtree of x are smaller than that in x.  For every node x, all keys in the right subtree of x are greater than that in x. 2. a.

class ENode { //投影片 Linked List P45 friend void Equivalence(); public: ENode(int d=0, ENode *next=0) //constructor {data = d; link = next;} private: int data; //2 分 ENode *link; //2 分 };

b. void Equivalence() //投影片 Linked List P46 { // initialization for (int i=0; i<n, i++) first[i] = 0; for (int i=0; i<n, i++)


out[i] = false;

//Phase 1 while (not end of input) //2 分 { read pair (x,y); //2 分 first[x] = new ENode(y, first[x]); //2 分 first[y] = new ENode(x, first[y]); //2 分 } c.

for (int i=0; i<n; i++) //投影片 Linked List P47 { if (!out[i]) { cout << endl; << "A new class: " << i; out[i] = true; ENode *x = first[i]; ENode *top = 0; while (1)

//initialize stack

//check if the stack is empty

{ while (x)

//check if the stack is empty,此區 6 分

{ int j = x->data; if (!out[j]) { cout << ", " << j; out[j] = true; push(j) } x = x->link; } if (stack is empty)

break;


x = pop(); //正確 output 4 分 } } } 3. a.

//投影片 Linked List P20 ptr->link = p->link; p->link = ptr; //順序錯就算全錯

b. //投影片 Linked List P11 deleteNode = p->link; p->link = p->link ->link; delete deleteNode; //錯一行扣 2 分 c.

//參考投影片 Linked List P10 void min_data(){ Node* p=first; int data = p->data // int 未宣告不扣分 while (p! =NULL) // 重點區開始 { if (

p->data

< data

)

data = p->data p=p->link; } return data; // 重點區結束 } 4. a.

//[0]漏寫、錯位等,每錯一個扣 1 分 0

1

2

3

4

5

6

7

8

9

-

78

63

34

45

-

23

25

-

6

b. class TreeNode{ //投影片 tree01 P34 int data; //1 分 TreeNode *leftChild, //2 分 *rightChild; //2 分


TreeNode() {leftChild = rightChild = NULL;} //1 分 }; // 用 struct 定義的參考解答如下 struct TreeNode{ int data; //2 分 struct TreeNode *leftChild, //2 分,沒寫 struct 扣 1 分 *rightChild; //2 分 }; c.

Inorder: 45 6 63 78 23 34 25//投影片 tree01 P42,P46 Postorder: 6 45 63 23 25 34 78 //上述兩小題各 4 分,當中有一個錯就 0 分

d. void PostOrder(TreeNode *t){ //投影片 tree01 P48 if (t != NULL){ PostOrder(t->leftChild); PostOrder(t->rightChild); Visit(t); } }//一個 statement 占 1 分,左右子樹遞迴各 2 分 e.

if (root==NULL) //投影片 tree01 P51 return; insert(root); while (Queue is not empty) { t=delete queue; visit(t); if (t->leftchild != NULL) insert(t->leftchild); if (t->rightchild != NULL) insert(t->rightchild); } //一個 statement 占 1 分,錯一個扣 1 分 //寫成以處理陣列為目標者,只給 1 分;無法正確表達題意者,不給分


5. Insert(int key) //投影片 tree02 P20 { n++;

//heap size+1

i=n; while (key > heap[i/2] && i>1) { heap[i]=heap[i/2]; i=i/2; } heap[i]=key; }//上述 12 分,時間複雜度分析 3 分,沒寫分析原因扣 2 分


13530339091764924004_aglzfmV4YW0tZGJyDAsSBGV4YW0Y84ECDA  
Advertisement
Read more
Read more
Similar to
Popular now
Just for you