CSC508 Test 2
CSC508 Test 2
INSTRUCTIONS TO CANDIDATES
3. Do not bring any material into the examination room including CALCULATOR
unless permission is given by the invigilator.
ANSWER SCHEME
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)
i) Draw a binary search tree diagram according to the author of the books.
(3 marks)
Clifford Stein
Robert Martin
Andrew Hunt
Cory Althoff
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)
if (node.data.getPrice()> 100)
return 1 +
count(node.left)+ count(node.right);
else
count(node.left)+ count(node.right);
}
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)
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)
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.
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)
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