0% found this document useful (0 votes)
13 views

My Beautiful Java

Uploaded by

philipshen1969
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)
13 views

My Beautiful Java

Uploaded by

philipshen1969
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/ 8

Computer Science and Software Engineering

SEMESTER 1, 2014 EXAMINATIONS

CITS2200
Data Structures and Algorithms

FAMILY NAME: ____________________________ GIVEN NAMES: ______________________

STUDENT ID: SIGNATURE: ________________________

This Paper Contains: 8 pages (including title page)


Time allowed: 2:10 hours (including reading time)
INSTRUCTIONS:

There are 5 questions worth 20 marks each. Marks for this paper total 100. Candidates must
answer all questions.

Answers are to be written in the provided answer booklets.

Calculators are permitted, but they are not necessary.

PLEASE NOTE

Examination candidates may only bring authorised materials into the examination room. If a supervisor
finds, during the examination, that you have unauthorised material, in whatever form, in the vicinity of your
desk or on your person, whether in the examination room or the toilets or en route to/from the toilets, the
matter will be reported to the head of school and disciplinary action will normally be taken against you. This
action may result in your being deprived of any credit for this examination or even, in some cases, for the
whole unit. This will apply regardless of whether the material has been used at the time it is found.

Therefore, any candidate who has brought any unauthorised material whatsoever into the examination
room should declare it to the supervisor immediately. Candidates who are uncertain whether any material is
authorised should ask the supervisor for clarification.

Supervisors Only - Student left at:


SEMESTER 1 EXAM 2
DATA STRUCTURES AND ALGORITHMS 2200 CITS2200

This page is deliberately left blank


SEMESTER 1 EXAM 3
DATA STRUCTURES AND ALGORITHMS 2200 CITS2200

1.

(a) Explain the following concepts and give an example of each:

(i) Generics (in Java)


(ii) Dynamic Programming
(iii) Expected Case Complexity

(6)

(b) Describe how amortized case analysis differs from worst case analysis and provide an example.
(3)

(c) Describe as clearly as possible what it means for f (n) to be O(g(n)) but g(n) is not O(f (n))
and give an example of how they differ.
(3)

(d) Give pseudo-code for the Merge-Sort algorithm.


(4)

(e) Show that Merge-Sort runs in time O(n lg n) where n is the number of elements being sorted.
(4)
SEMESTER 1 EXAM 4
DATA STRUCTURES AND ALGORITHMS 2200 CITS2200

2.

(a) A Priority Queue is specified as follows

2 Constructors
(i) PQueue(max): create a priority queue, with maximum size max
2 Checkers
(ii) isEmpty(): returns true if the queue is empty, false otherwise.
(iii) isFull(): returns true if the queue is full, false otherwise.
(iv) examine(): returns the element at the front of the priority queue.
2 Manipulators
(v) enqueue(e, p): Inserts the element e into the priority queue with priority p. The element e
is placed in front of all elements with priority q where q > p, but behind all other elements
with priority q where q <≤ p. It throws an Overflow exception if the table is full.
(vi) dequeue(): removes and returns the element e, at the front of the queue. If the queue is
empty, it throws an Underflow exception.

Write a block implementation of the PQueue ADT. You do not have to give lg time implemen-
tations of the methods.
(15)

(b) Describe an alternative implementation of a priority queue that can guarantee O(lg n) perfor-
mance for enqueue and dequeue operations and justify the O(lg n) complexity of these methods.
(5)
SEMESTER 1 EXAM 5
DATA STRUCTURES AND ALGORITHMS 2200 CITS2200

3.

(a) Describe, as clearly as possible, the problem that the Bellman-Ford Algorithm is designed to
solve.
(4)

(b) Give the pseudo-code for the Bellman-Ford Algorithm.


(5)

(c) Show that the Bellman-Ford Algorithm is correct.


(5)

(d) A Map class may be implemented in a number of ways. Given a map with comparable keys K,
mapping to values V, suppose that we want to rank implementations, so that one implementation
is better than another if its worst case performance is better, or if its worst case performance is
the same, but its expected case performance is the same. Rank the following implementations
from best to worst, explaining your reasoning. (It is possible that two implementations may
have equal rank).

(i) A hashmap using separate chaining.


(ii) A hashmap using dynamic tables.
(iii) A self-balancing tree map, such as a red-black tree, ordered by K
(iv) A binary search tree of pairs, ordered by K.

(6)
SEMESTER 1 EXAM 6
DATA STRUCTURES AND ALGORITHMS 2200 CITS2200

4.

A recursive implementation of an immutable binary tree, BTree, with an inner classes, MyIterator
and Node, is defined with the following declarations, member variables, and constructors:

public class BTree<E> {


private Node<E> root;

//empty constructor. makes an empty tree


public BTree() {root = null;}

//constructor for non-empty tree


public BTree(E item, BTree<E> left, BTree<E> right){
root = new Node<E>(item, left.root, right.root);
}

public Boolean isEmpty(){


return root == null;
}

public E getItem() {
return root.item;
}

public Iterator<E> iterator() {return new MyIterator();}

private class MyIterator implements Iterator<E> {


//missing code
}

private class Node<E> {


E item; Node<E> left; Node<E> right;

public Node(E item, Node<E> left, Node<E> right){


this.item = item; this.left = left; this.right = right;
}
}
}
SEMESTER 1 EXAM 7
DATA STRUCTURES AND ALGORITHMS 2200 CITS2200

(a) Provide an Iterator for the BTree class. The iterator should provide methods:

//returns true if and only there are elements left to iterate


public boolean hasNext();

//returns the next element, or throws a NoSuchElementException if there is none


public E next() throws NoSuchElementException;

The iterator should traverse the elements of the tree in a level-order traversal. You may assume
java.util classes or similar data structures are available.
(10)

(b) Provide Java code to test the iterator. The code should build a tree with at least 5 nodes, use
an iterator to print all the elements out, and confirm calling next() on an iterator with no next
element will throw a NoSuchElementException
(6)

(c) Explain the problem that fail-fast iterators are designed to address. Why do we not need to
consider a fail-fast approach for the BTree iterator?
(4)
SEMESTER 1 EXAM 8
DATA STRUCTURES AND ALGORITHMS 2200 CITS2200

5.

(a) Describe the expected case and the worst case look-up times for a dictionary implemented as a
binary search tree, and give and example of each.
(4)

(b) Select one type of self-balancing search tree, and carefully describe the mechanism that allows
the tree to remain balanced. Explain how this impacts the complexity of the insert and remove
methods
(8)

(c) What is a perfect hash function? Explain why it is almost always impossible to have a perfect
hash function.
(4)

(d) Suppose that we are using linear hashing, and start with an empty table with 2 buckets (M = 2),
split = 0 and a load factor of 0.9. Explain the steps we go through when the following hashes
are added (in order):
5, 7, 12, 11, 9

(4)

End of paper.

You might also like