Professional Documents
Culture Documents
INSTRUCTIONS TO CANDIDATES
2. Answer ALL questions in your answer sheet. Start each answer on a new page.Write your
name, id and group on the answer sheet.
4. No discussion and do not share your answers with other students. Any copying of or
plagiarized answers will be awarded 0 mark.
ANSWER SCHEME
a) Assume 1000 Covid-19 patient data has been inserted in an ArrayList object named
c19List. Write a Java program segment to:
C19List.addFirst(C19); // 1 mark
}
(3 marks)
iii. copy all the patient data who has contracted virus Omicron from c19List to
OmicronList.
(4 marks)
CovidPatient temp = new CovidPatient();
if (temp.getcVariant().equals(“omicron”) // 1 m
OmicronList.insertAtFront(temp); //1 m
}
iv. Search and display all details of patients who are in category 4 and 5, and has
dropped their oxygen level less than 85 from the array list c19List.
v. display the details of the patients in the LinkedList named OmicronList, for
all patients who already confirmed negative and can be discharged from the
hospital.
b) Compare the performance of inserting an element in the array list and inserting an
element in the linked list. Determine which one is faster. Briefly explain your reason.
(4 marks)
Inserting into an array list depends on the position that we want to insert the
new element. If we insert at the end of the array list, it is straightforward and no
shifting need to be done. But inserting a new element into the first index or any other
index in the middle of the array list, will require shifting of the rest of the elements to
the right and requires some time.
While inserting a new element into a linked list do not require any shifting of elements
because all that need to be connected are the link from the previous node and the link
to the next node in the list.
a)
Content of myQueue:
Empty – 1m
Content of queue1:
5 5 21 10 10 31 40 40 - 3m
Content of queue2:
21 31 – 1m
3 + 5 * 8 – 13 + 20 / 2
Show the steps how a compiler would evaluate its value using a stack
(5 marks)
3 5 8 * + 13 – 20 2 / +
8 2
5 5 40 13 20 20 10
Stack 3 3 3 3 43 43 30 30 30 30 40
Postfix 3 5 8 * + 13 - 20 2 / +
What is the value returned by the statement test(5,3)? Show all the steps taken.
(5 marks)
ANS:
test (5,3)
(5 > 3) : return (5 +3) = 8
a) There are three different traversals of a binary tree which are inorder, preorder and
postorder traversals.
i. Given the following Binary Search Tree (BST), give the order of nodes visited when
traverse using inorder, preorder and postorder traversal.
(3 marks)
Inorder 15 30 46 50 55 65 80 // 1 mark
Preorder 55 30 15 46 50 80 65 // 1 mark
Postorder 15 50 46 30 65 80 55 // 1 mark
89 99 159 55 32 76 80 65 120
89
55 99
159
32 76
80 120
65
(5 marks)
© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL
CONFIDENTIAL 6 CS/JULY 2022/CSC508
b) Construct an AVL tree by using the following list of strings and show the balance factor
after each insertion:
Covid19 Alpha Beta Omicron Gamma Delta Epsilon Eta
(6 marks)
Covid19
1 Beta
Beta
Covid19
Alpha
Gamma
Covid19
Beta Omicron
Alpha Gamma
Covid19
Omicron
Beta
Delta
Covid19
Gamma
Beta
Covid19
-1
-1
Gamma Final AVL Tree with
Beta
0 0 balance factor
10
Epsilon Omicron
Alpha 0 0
Delta Eta
a) From the given list below illustrate the execution of each pass of the following sorting
algorithms to sort the list in ascending order.
i. Insertion Sort
(5 marks)
How many comparisons are required to find whether the following items are in the list using
binary search algorithm? Show the values of the variable first, last, mid, and the
number of comparisons for searching the item.
i. 759
(5 marks)
ii. 501
(5 marks)
a b c d e f
a 0 3 ∞ ∞ 6 5
b 3 0 1 ∞ ∞ 4
c ∞ 1 0 6 ∞ 4
d ∞ ∞ 6 0 8 5
e 6 ∞ ∞ 8 0 2
f 5 4 4 5 2 0
b) Find and calculate the cost for a minimum spanning tree using Kruskal’s algorithm.
(6 marks)
Visited = { (b,c) }
Visited = {(b,c), (e,f)}
Visited = {(b,c), (e,f), (a,b) } MST = 1 + 2 + 3 + 4 + 5 = 15
Visited = {(b,c), (e,f), (a,b), (b,f) }
Visited = {(b,c), (e,f), (a,b), (b,f), (f,d) }
=
c) Using Djikstra Algorithm, find the shortest path from node a to all other vertices for the
graph. Show all the working steps.
(8 marks)
VERTEX a b c d e f
smallest
0 3 ∞ ∞ 6 5
weight
V(a)
weight
T
found
smallest
0 3 4 ∞ 6 5
weight
V(c)
weight
T T
found
smallest
0 3 4 10 6 5
weight
V(d)
weight
T T T
found
smallest
0 3 4 10 6 5
weight
V(b)
weight
T T T T
found
smallest
0 3 4 10 6 5
weight
V(e)
weight
T T T T T
found
smallest
0 3 4 10 6 5
weight
V(f)
weight
T T T T T T
found