My Beautiful Java
My Beautiful Java
CITS2200
Data Structures and Algorithms
There are 5 questions worth 20 marks each. Marks for this paper total 100. Candidates must
answer all questions.
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.
1.
(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)
(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.
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)
(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).
(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 E getItem() {
return root.item;
}
(a) Provide an Iterator for the BTree class. The iterator should provide methods:
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.