Exercise 6 (Comparable, Priority Queue) - Deadline is May 22nd
Completion requirements
Opened: Thursday, 7 May 2015, 6:00 PM
Due: Saturday, 23 May 2015, 8:00 AM
Modify the priority queue from the lecture
The priority queue from the lecture features fast removal of the high-priority item \(O(1)\) but slow insertion of new items \(O(n)\).
Take the priority queue from the lecture, which can be found on the Moodle page and modify it such that:
- The priority queue guarantees \(O(1)\) insertion time but slower removal of the high-priority item \(O(n)\).
- Make the priority queue generic (like the circular queue from the lecture).
- Therefore, you should only allow types which implement the Comparable interface. Define an upper bound for the type variable!
- The priority is determined by the method compareTo.
- Override the method toString from java.lang.Object such that it returns the string representation of the contents of the priority queue.
Hint
Java does not allow instantiating an array of variable type, i.e. in Java you can not write "new T[size]" if T is a type variable. This is for some technical reasons (type erasure). Use an Object[] instead, like demonstrated in the lecture, and cast the elements to T.
Define an upper bound for the type variable such that you can always compare Objects of type T.