Presentation is loading. Please wait.

Presentation is loading. Please wait.

授課教授:李錫智 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.

Similar presentations


Presentation on theme: "授課教授:李錫智 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."— Presentation transcript:

1 授課教授:李錫智 Data Structures -2 nd exam-

2 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=6 b.[3] What is the result return by elemAtRank(5)? Ans: elementAtRank(5)=30

3 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:

4 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.

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

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

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

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

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

10 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

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

12 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.

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

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

15 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 x Algorithm TREE-Y(x) WHILE x has leftchild DO x  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: g b.[5] What is the value returned by TREE-Y(x) if x is the root of Fig. 3? Ans: d

16 6.[15] Suppose we have the following algorithm: Algorithm PQSort(S, P) WHILE !isEmpty(S) DO e  removeFirst(S) insertItem(P, e) WHILE !isEmpty(P) DO e  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.

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

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

19 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 +-952 Inorder 9-5+2 Preorder x+314 Inorder 3+1x4

20 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 +-952 Inorder 9-5+2 Preorder x+314 Inorder 3+1x4 x Preorder +31 Inorder 3+1 4

21 Ans: 接著繼續對其左半部,執行同樣的動作 直到 list 中只剩下一個 element 或是 NULL ,將其輸出 (Return) 。 則 Binary Tree 如下圖 右子樹的部分,與左子樹同理。

22 Ans: 結果,如下圖 ÷ x+ +4- 2 31 95 PS. 不能直接假設 + - x ÷ 為 Internal node ,數字為 External node


Download ppt "授課教授:李錫智 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."

Similar presentations


Ads by Google