1 / 21

[2.5] Draw the result after insertAtRank(1,40).

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.

moe
Download Presentation

[2.5] Draw the result after insertAtRank(1,40).

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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. [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

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

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

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

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

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

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

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

  10. [5] Please list the node in this tree using the postorder traversal. Sol: 3 1 + 4 × 9 5 - 2 + /

  11. [5] Please list the node in this tree using the inorder traversal. Sol: 3 + 1 × 4 / 9 - 5 + 2

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

  13. • [5] Please show the linked data structure for representing the binary tree of Fig. 3. Sol:            

  14. 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:

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

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

  17. 6 removeMin().2 9 7 15 12 8 20 16 25 14 35 11 30 25 27

  18. 7 removeMin().3 9 8 15 12 11 20 16 25 14 35 27 30 25

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

  20. [10] Please show S and P after each iteration in the first while loop. Sol: 1. 2. 3. 4. 5. 6.

  21. [10] Please show S and P after each iteration in the second while loop. Sol: 1. 2. 3. 4. 5. 6.

More Related