PRG3201 Final Jan 2016
PRG3201 Final Jan 2016
Answer TWO (2) compulsory questions in Section A and any TWO (2) questions in
Section B in the answer booklet provided. All questions carry equal marks.
SECTION A
Answer ALL questions in this section. All questions carry equal marks
Question 1
(a) Complete the following binary search method which is written recursively.
(b) The following diagram figure 1 shows the location of the keys inserted into an array
using modulo division hashing method.
Index Key
0 24660
-
-
5 12285
-
65 12345
-
100 24660
-
-
-
307
Figure 1: Array
i) What are the locations for the keys 24730 and 12284?
(3 marks)
ii) What happens when the key 10810 is inserted into the array in figure 1?
(3 marks)
(c) Given the program segment below, what is the efficiency of the program segment if the
method test() has the complexity of O(log 2 n)? Justify your answer.
int x = 8;
while( x > 1)
{
System.out.println(x);
x = x / 2;
test();
}
(4 marks)
Question 2
(a) Define the characteristics of an AVL (Georgy Adelson-Velsky and Evgenii Landis)
tree.
(2 marks)
(b) Starting with an empty tree, construct an AVLtree by inserting the following keys in
the order given: 100 28 3 200 300 50 35. If an insertion causes the tree to
become unbalanced, then perform the necessary rotations to maintain the balance.
Identify where the rotations have been performed.
(12 marks)
(c) A Java application uses the class GroceryProduct to represent grocery product sold in a
grocery store such as sugar, coffee, tea, and etc. Each GroceryProduct object holds the
barcode for the product, the year manufactured, the quantity, and the price per item.
Assume the instances of GroceryProduct class are to be stored in a Binary Search Tree
and the positions of the instances are depending on the year manufactured. Write the
Java code for the GroceryProduct class.
(Note: Objects that are to be stored in a Binary Search Tree must belong to a class that
implements the Comparable interface).
(11 marks)
PRG3201/PRG3205 (F) / Page 3 of 5
SECTION B
Answer ANY TWO (2) of the THREE (3) questions in this section. All questions carry
equal marks
Question 3
first
A H J Q
You may assume the following definition of Node is used in the linked list.
i) Write the method deleteNode() that will delete the node in the ordered linked
list above. The deletion is based on the value of data. Assume that the item is
in the list.
(15 marks)
ii) Write the method countNode() that will return the number of nodes in a linked
list.
(10 marks)
PRG3201/PRG3205 (F) / Page 4 of 5
Question 4
i) undirected graph
ii) a cycle
iii) a loop
(3 marks)
B C D
E F G
(ii) Apply the breadth-first traversal algorithm to figure 3, given the vertex A as the
origin vertex. Show the frontVertex value, the nextNeighbor value,
visitedVertex value, and the traversal order in a table.
(6 marks)
- /
* 10 28 4
15 5
ii) Show how the operands are evaluated using stack concept. Draw the diagrams to
show how each value is evaluated.
(10 marks)
PRG3201/PRG3205 (F) / Page 5 of 5
Question 5
int pop();
// PRE the stack is not empty
// POST the item at the top of the stack is returned
and removed from the stack.
int peek();
// PRE the stack is not empty
// POST the item at the top of the stack is returned
boolean isEmpty();
// POST true is returned if the stack is empty, else
false
boolean isFull();
// POST true is returned if the stack is full, else
false
- THE END -
PRG3201/PRG3205(F)/JAN16/HARPRITH