Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

PRG3201/PRG3205 (F) / Page 1 of 5

INTI INTERNATIONAL UNIVERSITY

BACHELOR OF COMPUTER SCIENCE (HONS)


PRG3201/PRG3205: DATA STRUCTURE AND ALGORITHMS
FINAL EXAMINATION: JANUARY 2016 SESSION

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.

boolean binarysearch(int list[], int target, int first, int last)


// Pre : list contains array of integer values
// target is the value to search
// first is the index of the first element in the array
// last is the index of the last element in the array
//Post : Returns true if target is found or false if target is not found
(11 marks)

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

iii) What should be done to resolve the problem encountered in 1(b)(ii).


(4 marks)
PRG3201/PRG3205 (F) / Page 2 of 5

(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

Figure 2 represents an ordered linked list.

first

A H J Q

Link Link Link Link

Figure 2: Ordered Linked List

You may assume the following definition of Node is used in the linked list.

public class Node


{
private char data;
private Node next;

public Node (char newData, Node newNext)


{
data = newData;
next = newNext;
}
public char getData(){return data;}
public void setData(char newData){data = newData;}
public Node getNext() {return next;}
public void setNext(Node newNext){next = newNext;}

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

(a) Define the following terms in the context of a graph.

i) undirected graph

ii) a cycle

iii) a loop
(3 marks)

(b) The following diagram Figure 3 represents a directed graph.

B C D

E F G

Figure 3: Directed Graph

(i) Provide the adjacent matrix for figure 3.


(4 marks)

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

(c) Figure 4 is an expression tree.

- /

* 10 28 4

15 5

Figure 4: Expression Tree

i) Write the postfix expression for figure 4.


(2 marks)

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

Given the following Stack Interface :-

public interface SimpleStack


{
void push(int obj);
// PRE the stack is not full
// POST obj has been added at the top of the stack

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

Write an array-based Stack class to implement the above Interface.


(25 marks)

- THE END -
PRG3201/PRG3205(F)/JAN16/HARPRITH

You might also like