[2.5] Draw the result after insertAtRank(1,40).
description
Transcript of [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.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 = 610 90 60 80 30 100
0 1 2 3 4 5 6 7
• [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
10 40 90 60 80 30 500 1 2 3 4 5 6 7
10 90 80 30 500 1 2 3 4 5 6 7
10 60 70 80 90 50
0 1 2 3 4 5 6 7
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] Draw L after insertBefore(400,NYU).Sol:
800
800 BWI a
a PVD 1000
1200 JFK 200
400 SFO 100
1000
Header 1200
200
400
800
1000
trailer 100
200 NYU 400a
• [4] Draw L after insertFirst(TPI).Sol:
a
800 BWI 400
200 PVD 1000
a JFK 200
400 SFO 100
1000
Header 1200
200
400
800
1000
trailer 100
1200 TPI 800a
• [4] Draw L after insertLast(KAH).Sol:
800
800 BWI 400
200 PVD 1000
1200 JFK 200
400 SFO a
a
Header 1200
200
400
800
1000
trailer 100
1000 KAH 100a
• [4] Draw L after remove(400).Sol:
800
800 BWI 1000
1200 JFK 200
200 SFO 100
1000
Header 1200
200
800
1000
trailer 100
• [4] Draw L after swapElements(800,1000).Sol:
800
800 BWI 400
200 PVD 1000
1200 SFO 200
400 JFK 100
1000
Header 1200
200
400
800
1000
trailer 100
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: / × + + 4 - 2 3 1 9 5
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
• [5] Please show the linked data structure for representing the binary tree of Fig. 3.Sol:
/
× +
+ 4
3 1
-
9 5
2
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 6 15 9 7 20 16 25 14 12 11 8 25 27 30 35
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
• [5] What is the result after insertItem(6)?Sol:
4
5 6
6 9 7 20
16 15 11 814 12 25 27
30 35 25
• [10] What is the result after three successive operations of removeMin()?Sol: removeMin().1
5
9 6
15 12 7 20
16 25 11 814 35 25 27
30
removeMin().2
6
9 7
15 12 8 20
16 25 11 3014 35 25 27
removeMin().3
7
9 8
15 12 11 20
16 25 27 3014 35 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() doe <- 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.
S: 20 80 40 60 30
P: 50
S: 80 40 60 30
P: 50 20
S: 40 60 30
P: 50 20 80
S: 60 30
P: 50 20 80 40
S: 30
P: 50 20 80 40 60
S:
P: 50 20 80 40 60 30
• [10] Please show S and P after each iteration in the second while loop.
Sol: 1.
2.
3.
4.
5.
6.
S: 20
P: 50 80 40 60 30
S: 20 30
P: 50 80 40 60
S: 20 30 40
P: 50 80 60
S: 20 30 40 50
P: 80 60
S: 20 30 40 50 60
P: 80
S: 20 30 40 50 60 80
P: