data structures -2 nd exam-

22
授授授授 授授授 Data Structures -2 nd exam-

Upload: shino

Post on 23-Jan-2016

45 views

Category:

Documents


0 download

DESCRIPTION

Data Structures -2 nd exam-. 授課教授:李錫智. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Data Structures -2 nd   exam-

授課教授:李錫智

Data Structures-2nd exam-

Page 2: Data Structures -2 nd   exam-

1. [15] A simple way for implementing the vector ADT is to use an array A, where A[i] stores the element at rank i. In this case, let N be the size of array A and n be the number of elements stored in A. Assume the content of A is shown in Fig. 1.Do the following sub-problems. Note that all the sub-problems are independent of each other.

a. [3] What are the values of N and n for A? Ans: N=8, n=6b. [3] What is the result return by elemAtRank(5)? Ans: elementAtRank(5)=30

Page 3: Data Structures -2 nd   exam-

c. [3] Draw the result after replaceAtRank(4,55)? Ans:

d. [3] Draw the result after insertAtRank(3,45)? Ans:

e. [3] Draw the result after removeAtRank(2)? Ans:

Page 4: Data Structures -2 nd   exam-

2. [20] Suppose we have a doubly-linked list L as shown in Fig. 2. Note that the header node is located at address 700, the BWI node is located at address 500, the PVD node is located at address 200, the JFK node is located at address 600, the SFO node is located at address 900, and the trailer node is located at address 50. Do the following sub-problems. Note that all the sub-problems are independent of each other.

Page 5: Data Structures -2 nd   exam-

a. [4] Draw L after insertBefore(500,NYU). Ans:

Page 6: Data Structures -2 nd   exam-

b. [4] Draw L after insertFirst(TPI). Ans:

Page 7: Data Structures -2 nd   exam-

c. [4] Draw L after insertLast(KAH). Ans:

Page 8: Data Structures -2 nd   exam-

d. [4] Draw L after remove(200). Ans:

Page 9: Data Structures -2 nd   exam-

e. [4] Draw L after remove(900). Ans:

Page 10: Data Structures -2 nd   exam-

3. [15] Suppose we have a binary tree as shown in Fig. 3. Do the following sub-problems. Note that all the sub-problems are independent of each other.

a. [5] Please list the node in this tree using the preorder traversal. Ans: a, b, d, e, h, i, j, k, c, f, g

Page 11: Data Structures -2 nd   exam-

b. [5] Please list the node in this tree using the postorder traversal.

Ans: d, h, j ,k, i ,e, b, f, g, c, ac. [5] Please list the node in this tree using the inorder traversal. Ans: d, b, h, e, j, i, k, a, f, c, g

Page 12: Data Structures -2 nd   exam-

4. [10] Suppose we have a heap shown in Fig. 4. Do the following sub-problems. Note that all the sub-problems are independent of each other.

Page 13: Data Structures -2 nd   exam-

a. [5] What is the result after insertItem(9)?

Page 14: Data Structures -2 nd   exam-

b. [5] What is the result after removeMin()?

Page 15: Data Structures -2 nd   exam-

5. [10] Suppose we have the following two functions:Algorithm TREE-X(x)

WHILE x has rightchild DO x rightchild of x

RETURN key of xAlgorithm TREE-Y(x)

WHILE x has leftchild DOx leftchild of x

RETURN key of x

a. [5] What is the value returned by TREE-X(x) if x is the root of Fig. 3?

Ans: gb. [5] What is the value returned by TREE-Y(x) if x is the root of Fig.

3? Ans: d

Page 16: Data Structures -2 nd   exam-

6. [15] Suppose we have the following algorithm:Algorithm PQSort(S, P)

WHILE !isEmpty(S) DOe removeFirst(S)insertItem(P, e)

WHILE !isEmpty(P) DOe removeMin(P)insertLast(S, e)

Let S be an array of integers shown in Fig. 5 and P is an array of the same size as S. Initially, P is empty. Note that function removeFirst(S) removes and returns the first element in S, and shifts one position left all the elements to the right of the removed element. insertItem(P, e) inserts e as the last element in P. Function removeMin(P) removes and returns the element with the minimum value in P, and shifts one position left all the elements to the right of the removed element. Function insertLast(S, e) inserts e as the last element in S. Do the following sub-problems.

Page 17: Data Structures -2 nd   exam-

a. [5] Please show S and P after each iteration in the first while loop.

Ans:

Page 18: Data Structures -2 nd   exam-

b. [10] Please show S and P after each iteration in the second while loop.

Ans:

Page 19: Data Structures -2 nd   exam-

7. [15] Suppose we have a binary tree whose preorder traversal is “÷×+ 314+- 952” and whose inorder traversal is “3+1×4÷9- 5+ 2”. Please draw out the binary tree. Please describe in Chinese, English, or Pseudo-code your algorithm. Note that you will get no points without a correct description.

Ans: Preorder list 的 first element “÷” 即為此 Binary Tree 的 Root再到 Inorder list 中找尋此 element ,找到後,其左半部為左子樹的 Inorder list右半部為右子樹的 Inorder list並找出其對應的 Preorder list示意圖如右圖:

÷

Preorder

+ - 9 5 2Inorder

9 - 5 + 2

Preorder

x + 3 1 4Inorder

3 + 1 x 4

Page 20: Data Structures -2 nd   exam-

Ans:

接著以左子樹為例, Preorder list: x + 3 1 4, Inorder list: 3 + 1 x 4 一樣取出 Preorder list 的 first element “x” 為左子樹的 root Inorder list 中,對此 element 一樣把 list 分成左半部與右半 部,當右半部只剩下一個 element 或是 NULL ,將其 輸出 (Return) ,結果如右圖

÷

Preorder

+ - 9 5 2Inorder

9 - 5 + 2

Preorder

x + 3 1 4Inorder

3 + 1 x 4

x

Preorder

+ 3 1Inorder

3 + 1

4

Page 21: Data Structures -2 nd   exam-

Ans: 接著繼續對其左半部,執行同樣的動作 直到 list 中只剩下一個 element 或是 NULL ,將其輸出

(Return) 。 則 Binary Tree 如下圖

右子樹的部分,與左子樹同理。

Page 22: Data Structures -2 nd   exam-

Ans: 結果,如下圖

÷

x +

+ 4 - 2

3 1 9 5

PS. 不能直接假設 + - x ÷ 為 Internal node ,數字為 External node