You are on page 1of 13

CONFIDENTIAL 1 CS/JULY 2022/CSC508

UNIVERSITI TEKNOLOGI MARA


FINAL ASSESSMENT

COURSE : DATA STRUCTURES


COURSE CODE : CSC508
EXAMINATION : JULY 2022
TIME : 3 HOURS

INSTRUCTIONS TO CANDIDATES

1. This question paper consists of five(5) questions.

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.

3. Please scan and save your answers as pdf file.

4. No discussion and do not share your answers with other students. Any copying of or
plagiarized answers will be awarded 0 mark.

5. Answer ALL questions in English.

ANSWER SCHEME

DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO


This examination paper consists of 7 printed pages

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL


CONFIDENTIAL 2 CS/JULY 2022/CSC508

QUESTION 1 (25 MARKS)

a) Assume 1000 Covid-19 patient data has been inserted in an ArrayList object named
c19List. Write a Java program segment to:

i. Declare a LinkedList object named OmicronList.

LinkedList OmicronList = new LinkedList(); (2 marks)

ii. Insert another 100 Covid-19 patient data to c19List list.

Scanner sc = new Scanner (System.in);


System.out.println (“Insert another 100 Covid-19 patient data “);

for (int i=0; i < 100; i++)


{
System.out.println (“Patient’s Name: “);
String pName = sc.nextLine();
System.out.println (“Patient’s IC Number: “);
int pID = sc.nextInt();
System.out.println (“virus variant SARS-CoV-2, Omicron, Alpha,
Beta: “);
String virus = sc.nextLine();
System.out.println (“Date start quarantine: “);
String dateQ = sc.nextLine();
System.out.println (“Covid status : 1-positif or 0-negative “);
int C_Stat = sc.nextInt();
System.out.println (“Oksijen Level: “);
float O_Level= sc.nextFloat();
System.out.println (“Covid category : 1,2,3,4,5 “);
int C_Cat = sc.nextInt();

CovidPatient C19 = new CovidPatient(pName, pID, virus, dateQ,


C_Stat, O_Level, C_Cat);
//2 marks

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();

for (int i = 0; i < c19List.size(); i++)// 1 m


{
temp = (CovidPatient)c19List.get(i); // 1 m

if (temp.getcVariant().equals(“omicron”) // 1 m
OmicronList.insertAtFront(temp); //1 m
}

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/JULY 2022/CSC508

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.

int cat; //category

for (int i = 0; i < c19List.size(); i++)// 1 m


{
temp = (CovidPatient)c19List.get(i); // 1 m

cat = temp. getcCategory();


if (cat == 4 || cat == 5) // 2 m
{
if (temp. getoLevel() < 85) //1 m

temp.displayInfo(); // 1 mark, display patient’s info


}
}
(6 marks)

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.

LinkedList OmicronList = new LinkedList();

CovidPatient data = (CovidPatient) OmicronList.getFirst();


//1 m

while (data != null) // 1.5 m


{
if(data.getcStatus()== 0) // 1.5 m
data.displayInfo(); // 1 m

data = (CovidPatient)OmicronList.getNext(); //1 m


}
(6 marks)

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.

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CS/JULY 2022/CSC508

QUESTION 2 (15 MARKS)

a)

Content of myQueue:
Empty – 1m

Content of queue1:
5 5 21 10 10 31 40 40 - 3m

Content of queue2:
21 31 – 1m

b) Given the following infix expression:

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 / +

c) Consider the following function:

public static int test(int x, int y)


{
if (x == y)
return x;
else if (x > y)
return (x + y);
else
return test(x + 1, y – 1);
}

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

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL


CONFIDENTIAL 5 CS/JULY 2022/CSC508

QUESTION 3(20 MARKS)

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

ii. Given the following list, draw the BST.

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

iii. Write a recursive method to count number of leave nodes in a BST.

// public method for function call from main


public int countLeavesBST()
{
return countLeaves(root);
}

// private recursive method


private int countLeaves(TreeNode node)
{
if (node.left == NULL && node.right == NULL)
return 1;
else
return countLeaves(node.left) + countLeaves(node.right);
}
(5 marks)

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

Balance factor is violated


Alpha 2 here
Have to do double rotation

1 Beta

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL


CONFIDENTIAL 7 CS/JULY 2022/CSC508

Beta

Covid19
Alpha

Balance factor is violated here Omicron

Gamma

Covid19

Beta Omicron

Alpha Gamma

Covid19

Omicron
Beta

after inserting Delta, balance


factor is violated here. Have
Alpha Gamma to do single rotation to the
right

Delta

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL


CONFIDENTIAL 8 CS/JULY 2022/CSC508

Covid19

Gamma
Beta

Alpha Delta Omicron

after inserting Epsilon


and Eta, balance factor Epsilon
is violated here. Have
to do single rotation
to the left 1 Eta

Covid19
-1
-1
Gamma Final AVL Tree with
Beta
0 0 balance factor
10
Epsilon Omicron
Alpha 0 0

Delta Eta

QUESTION 4(20 MARKS)

a) From the given list below illustrate the execution of each pass of the following sorting
algorithms to sort the list in ascending order.

grape peach apple manggo banana rambutan durian

i. Insertion Sort
(5 marks)

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL


CONFIDENTIAL 9 CS/JULY 2022/CSC508

grape peach | apple manggo banana rambutan durian

apple grape peach | manggo banana rambutan durian

apple grape mango peach | banana rambutan durian

apple banana grape mango peach | rambutan durian

apple banana grape mango peach rambutan | durian

apple banana durian grape mango peach rambutan

ii. Selection Sort


(5 marks)
grape peach apple manggo banana rambutan durian

apple | peach grape manggo banana rambutan durian

apple banana | grape manggo peach rambutan durian

apple banana durian | manggo peach rambutan grape

apple banana durian grape | peach rambutan manggo

apple banana durian grape manggo | rambutan peach

apple banana durian grape manggo peach rambutan

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL


CONFIDENTIAL 10 CS/JULY 2022/CSC508

b) Given the following list:

153 306 451 600 759 801 900 950

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)

Iteration First Last Mid List[mid] No. of


comparison
1 0 7 3 759 > 600 2
2 4 7 5 759< 801 2
3 4 4 4 759 == 759 1
Total = 5
Successful
search)

ii. 501
(5 marks)

Iteration First Last Mid List[mid] No. of


comparison
1 0 7 3 501 < 600 2
2 0 2 1 501> 306 2
3 2 2 2 501 > 451 2
4 2 1 The loop stops because first >last
Total = 6
(Unsuccessful
search)

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL


CONFIDENTIAL 11 CS/JULY 2022/CSC508

QUESTION 5(20 MARKS)

Given the following graph:

a) Draw the weighted adjacency matrix from the graph.


(6 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) }
=

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL


CONFIDENTIAL 12 CS/JULY 2022/CSC508

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

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL


CONFIDENTIAL 13 CS/JULY 2022/CSC508

END OF QUESTION PAPER

© Hak Cipta UniversitiTeknologi MARA CONFIDENTIAL

You might also like