E N D
Data Structures-2nd exam- 授課教授:李錫智
[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. • [3] What are the values of N and n for A? Ans:N=8, n=6 • [3] What is the result return by elemAtRank(5)? Ans:elementAtRank(5)=30
[3] Draw the result after replaceAtRank(4,55)? Ans: • [3] Draw the result after insertAtRank(3,45)? Ans: • [3] Draw the result after removeAtRank(2)? Ans:
[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.
[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. • [5] Please list the node in this tree using the preorder traversal. Ans: a, b, d, e, h, i, j, k, c, f, g
[5] Please list the node in this tree using the postorder traversal. Ans: d, h, j ,k, i ,e, b, f, g, c, a • [5] Please list the node in this tree using the inorder traversal. Ans: d, b, h, e, j, i, k, a, f, c, g
[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.
[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 • [5] What is the value returned by TREE-X(x) ifx is the root of Fig. 3? Ans: g • [5] What is the value returned by TREE-Y(x) if x is the root of Fig. 3? Ans: d
[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.
[5] Please show S and P after each iteration in the first while loop. Ans:
[10] Please show S and P after each iteration in the second while loop. Ans:
[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 示意圖如右圖: ÷
÷ 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),結果如右圖 x 4
Ans: 接著繼續對其左半部,執行同樣的動作 直到list中只剩下一個element或是NULL,將其輸出(Return)。 則Binary Tree如下圖 右子樹的部分,與左子樹同理。
÷ Ans: 結果,如下圖 x + 2 + 4 - 3 1 9 5 • PS.不能直接假設+ - x ÷為Internal node,數字為External node