0% found this document useful (0 votes)
128 views6 pages

CSC508 Test 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
128 views6 pages

CSC508 Test 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CONFIDENTIAL CS/JAN 2023/CSC508/TEST 2

UNIVERSITI TEKNOLOGI MARA


TEST 2

COURSE : DATA STRUCTURES


COURSE CODE : CSC508
TIME : 1 HOUR 30 MINUTES

INSTRUCTIONS TO CANDIDATES

1. This paper consists of three (3) questions.

2. Answer ALL questions.

3. Do not bring any material into the examination room including CALCULATOR
unless permission is given by the invigilator.

ANSWER SCHEME

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


This examination paper consists of 7 printed pages
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 2 CS/JAN 2023/CSC508/TEST 2

Answer ALL questions.

QUESTION 1 (20 Marks)

a) Describe an AVL tree.


(2 marks)

AVL tree is a self-balancing BST(Binary Search Tree). In the AVL tree, the difference
between heights of the right and left subtree doesn't exceed one for all nodes.

b) The following keys are inserted into an initially empty AVL tree.

25 35 40 46 43 13 10

Show the AVL tree after each key insertion. The resulting tree must be an AVL tree.
(7 marks)

Correct answers by Asri (CS2534B)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 3 CS/JAN 2023/CSC508/TEST 2

c) The following table shows data of books:

Author ISBN Price (RM)


Clifford Stein 12345677 50
Benjamin Ford 33445566 100
Steve Connel 23454567 70
Andrew Hunt 13425678 90
Robert Martin 56789345 200
Cory Althoff 25688797 80

i) Draw a binary search tree diagram according to the author of the books.
(3 marks)

Clifford Stein

Benjamin Ford Steve Connel

Robert Martin
Andrew Hunt

Cory Althoff

ii) Traverse the tree in i) using the inorder traversal method.


(2 marks)

Andrew Hunt, Benjamin Ford, Clifford Stein, Cory Althoff, Robert Martin, Steve
Connel

iii) Write method definition for countBook() and its recursive method to count the
number of books with price greater than 100.
(6 marks)

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL


CONFIDENTIAL 4 CS/JAN 2023/CSC508/TEST 2

public int countBook()


{ return count( root); }

private int( TreeNode node)


{
if ( node == null )
return 0;

if (node.data.getPrice()> 100)
return 1 +
count(node.left)+ count(node.right);
else
count(node.left)+ count(node.right);
}

QUESTION 2 (12 Marks)

Given the following list:

5 80 15 22 33 76 35 45

i) Sort the list in descending order using selection sort. Show all the steps for
sorting the list.
( 6 marks)

80 | 5, 15, 22, 33, 76, 35, 45


80, 76 | 15, 22, 33, 5, 35, 45
80, 76, 45 | 22, 33, 5, 35, 15
80, 76, 45, 35 | 33, 5, 22, 15
80, 76, 45, 35, 33 | 5, 22, 15
80, 76, 45, 35, 33, 22| 5, 15
80, 76, 45, 35, 33, 22, 15 | 5
80, 76, 45, 35, 33, 22, 15, 5

ii) Sort the list in ascending order using merge sort. Show all the steps for
sorting the list.
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 5 CS/JAN 2023/CSC508/TEST 2

(6 marks)

Correct answers by Alvey (CS2534A)

QUESTION 3 (18 Marks)

a) Describe TWO(2) types of searching algorithms.


(4 marks)

1. Linear search is a search that finds an element in the list by searching the
element sequentially until the element is found in the list.

2. A binary search is a search that finds the middle element in the list
recursively until the middle element is matched with a searched element.

b) Given the following list:

10 20 30 40 50 60 70 80

Using linear search algorithm, how many comparisons are required to find whether the
following items are in the list? Determine whether it is a best, average, or worst case.
© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL
CONFIDENTIAL 6 CS/JAN 2023/CSC508/TEST 2

i) 40
4 comparisons - Average case
(2 marks)

ii) 10
1 comparison – Best case
(2 marks)

iii) 75
8 comparisons – Worst case
(2 marks)

c) Given the following values to be inserted into a hash table of size 10:

1999 1998 2005 2009 2007 1991 1994 2018 2000 2015 2002 2008

Draw the diagram of the hash table using the hash function: h(x) = x mod 10.
Use chaining to solve the collision in the hash table.
(6 marks)

0 2000 h(1999) = 1999 mod 10 = 9


h(1998) = 1998 mod 10 = 8
1 1991 .
.
2 2002
3

4 1994

5 2005 →2015

7 2007
8 1998 → 2018 →2008

9 1999 →2009

d) Calculate the load factor for the hash table produced in c).
(2 marks)
12/10 = 1.2

END OF QUESTION PAPER

© Hak Cipta Universiti Teknologi MARA CONFIDENTIAL

You might also like