PRG3201 (F) Jan14

You might also like

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

PRG3201 (F) / Page 1 of 6

INTI INTERNATIONAL UNIVERSITY

BACHELOR IN COMPUTER SCINCE. (HONS) - BCSI


PRG3201 :DATA STRUCTURE AND ALGORITHMS
FINAL EXAMINATION : JANUARY 2014

This paper consists of SIX (6) questions. Answer any FOUR (4) questions in the answer
booklet provided. All questions carry equal marks.

Question 1

(a) Explain in terms of efficiency, why the binary search algorithm is a better technique
compared with the sequential search?
(5 marks)

(b) Write a JAVA method to represent the binary search algorithm..

(14 marks)

(c) Given the following recursive method :-

int fun1(int x, int y)


{
if ( x < y)
return x+y;
else return fun1(x – 5, y – 2);
}

What will the following function returns if the value of x and y are :

i) 28, 24
ii) 20, 10
iii) 7,8
(6 marks)

(Total Marks: 25 marks)


PRG3201 (F) / Page 2 of 6

Question 2

(a) Define the following terms :-

i) Hashing
ii) Collision
iii) Primary Clustering
(6 marks)

(b) Explain how probability search is different from the general sequential algorithm by
providing both algorithms. You need to include only the parts of the algorithm that show the
differences.
(9 marks)

(c) Given the following keys, calculate the home address using pseudorandom technique for
each key. If there is a collision, use the pseudorandom collision technique to solve the
collision. (Assume a= 3,c = 1 and size of the array is 301)

i) 123456
ii) 123556
iii) 124615
iv) 124916

(10 marks)
(Total Marks: 25 marks)
PRG3201 (F) / Page 3 of 6

Question 3

Given the following STACK ADT interface.

public interface StackObjectInterface


{
public void push(Object newEntry);
public Object pop();
public Object peek();
public boolean isEmpty();
public void clear();
}

public class Student


{
private String name;
private int id_num;

Student(String name, int id_num)


{
this.name = name;
this.id_num = id_num;

String getName()
{
return name;
}

int getId()
{
return id_num;
}

public String toString()


{
return name + id_num;
}
}

Write a class to implement the above interface using an array based structure.
(25 marks)
(Total Marks: 25 marks)
PRG3201 (F) / Page 4 of 6

Question 4

(a) Figure Q4 represents a circular linked list. Answer the following questions based on the
diagram.

first

Object data Object data Object data Object data

Link Link Link Link

first

Figure Q4

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

public class Node


{
private Object data;
private Node next;

public Node (Object newData, Node newNext)


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

(i) Write the implementation of a method called addLast() to add a new node to the
end of the linked list.
(10 marks)

(ii) Write the implementation of a method called removeLast() that will remove the
last node from the linked list.
(15 marks)

(Total Marks: 25 marks)


PRG3201 (F) / Page 5 of 6

Question 5

public class Node


{
String name;
Node left;
Node right;

Node()
{
setNode(null,null,null);
}
Node(String name, Node l, Node r)
{
setNode(name,l,r);
}

void setNode(String name, Node left, Node right)


{
this.name = name;
this.left = left;
this.right = right;
}

String getName()
{
return name;
}

i) Write the method to insert a node into a Binary Search Tree structure.
(18 marks)

ii) Write the method to traverse the tree using the Inorder() traversal.
(7 marks)

(Total Marks: 25 marks)


PRG3201 (F) / Page 6 of 6

Question 6

(a) What will happen when 3 and 1 is inserted into the AVL (Adelson-Velskii and Landis)
tree in figure Q6(a) ? What must be done in order to correct the situation? Illustrate
your explanation using tree diagrams.

73
28 111
5
Figure Q6(a)
(8 marks)

(b) Show the traversal order using the breadth-first traversal algorithm and the depth first
traversal order for the graph in figure Q6(b), given the vertex A is the origin vertex.

A D G

B H

C E F

Figure Q6(b)
(6 marks)

(c) Given the following values:-

28 5 73 2 100 20 14 3

i) Draw the heap tree


ii) Draw the Binary Search tree and show the postOrder and preOrder traversal.
(11 marks)

(Total Marks: 25 marks)


-The End-
PRG3201(F)JAN14/HARPRITH/260414

You might also like