210 likes | 273 Views
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.
E N D
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. • [2.5] What is the result return by elemAtRank(4)? Sol:30 • [2.5] Draw the result after replaceAtRank(5,100). Sol: N = 8 , n = 6
[2.5] Draw the result after insertAtRank(1,40). Sol: N = 8 , n = 7 • [2.5] Draw the result after removeAtRank(2). Sol:N = 8 , n = 5 • [5] Draw the result after the following successive operations: insertAtRank(3,70), removeAtRank(1), removeAtRank(4), and insertAtRank(4,90). Sol: N = 8 , n = 6
2. [20] Suppose we have a doubly-linked list L as shown in Fig. 2. Note that the header node is located at address 1200, the BWI node is located at address 200, the PVD node is located at address 400, the JFK node is located at address 800, the SFO node is located at address 1000, and the trailer node is located at address 100. Do the following sub-problems. Note that all the sub-problems are independent of each other.
Header 1200 800 • [4] Draw L after insertBefore(400,NYU). Sol: 200 800 BWI a a 200 NYU 400 400 a PVD 1000 800 1200 JFK 200 1000 400 SFO 100 trailer 100 1000
Header 1200 a • [4] Draw L after insertFirst(TPI). Sol: a 1200 TPI 800 200 800 BWI 400 400 200 PVD 1000 800 a JFK 200 1000 400 SFO 100 trailer 100 1000
Header 1200 800 • [4] Draw L after insertLast(KAH). Sol: 200 800 BWI 400 400 200 PVD 1000 800 1200 JFK 200 1000 400 SFO a a 1000 KAH 100 trailer 100 a
Header 1200 800 • [4] Draw L after remove(400). Sol: 200 800 BWI 1000 800 1200 JFK 200 1000 200 SFO 100 trailer 100 1000
Header 1200 800 • [4] Draw L after swapElements(800,1000). Sol: 200 800 BWI 400 400 200 PVD 1000 800 1200 SFO 200 1000 400 JFK 100 trailer 100 1000
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. • [5] Please list the node in this tree using the preorder traversal. Sol: / × + 3 1 4 + - 9 5 2
[5] Please list the node in this tree using the postorder traversal. Sol: 3 1 + 4 × 9 5 - 2 + /
[5] Please list the node in this tree using the inorder traversal. Sol: 3 + 1 × 4 / 9 - 5 + 2
4. [10] 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 show the representation of the tree by means of an array S of size 16. Sol:
• [5] Please show the linked data structure for representing the binary tree of Fig. 3. Sol:
5. [20] 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. • [5] What is the array representation of Fig. 4? Sol:
4 • [5] What is the result after insertItem(6)? Sol: 5 6 6 9 7 20 15 14 12 11 8 25 27 16 30 35 25
5 • [10] What is the result after three successive operations of removeMin()? Sol: removeMin().1 9 6 15 12 7 20 16 25 14 35 11 8 25 27 30
6 removeMin().2 9 7 15 12 8 20 16 25 14 35 11 30 25 27
7 removeMin().3 9 8 15 12 11 20 16 25 14 35 27 30 25
6. [20] Suppose we have the following algorithm: Algorithm PQSort(S,P) while !S.isEmpty() do e <- S.removeFirst(); P.insertItem(e); while !P.isEmpty() do e <- P.removeMin(); S.insertLast(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 S.removeFirst() removes and returns the first element in S, and shifts one position left all the elements to the right of the removed element. P.insertItem(e) inserts e as the last element in P. P.removeMin() 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. S.insertLast(e) inserts e as the last element in S. Do the following sub-problems. Note that all the sub-problems are independent of each other.
[10] Please show S and P after each iteration in the first while loop. Sol: 1. 2. 3. 4. 5. 6.
[10] Please show S and P after each iteration in the second while loop. Sol: 1. 2. 3. 4. 5. 6.