I have to build two implementations of a priority queue: Ordered array and a Binary heap.

profilecrytal1
prog2.pdf

The Program:

For this assignment, you will write two implementations of a Priority Queue. For this ADT, removal operations always return the object in the queue of highest priority that has been in the queue the longest. That is, no object of a given priority is ever removed as long as the queue contains one or more objects of a higher priority. Within a given priority FIFO order must be preserved. Your two implementations will be:

 Ordered Array

 Binary Heap

Both implementations must have identical behavior, and must implement the PriorityQueue interface (provided). Both implementations must have two constructors, a default constructor with no arguments that uses the DEFAULT_MAX_CAPACITY constant from the PriorityQueue.java interface, and a constructor that takes a single integer parameter that represents the maximum capacity of the PQ. The PriorityQueue interface follows:

/* The PriorityQueue ADT may store objects in any order. However,

removal of objects from the PQ must follow specific criteria.

The object of highest priority that has been in the PQ longest

must be the object returned by the remove() method. FIFO return

order must be preserved for objects of identical priority.

Ranking of objects by priority is determined by the Comparable

interface. All objects inserted into the PQ must implement this

interface.

*/

package data_structures;

import java.util.Iterator;

public interface PriorityQueue<E> extends Iterable<E> {

static final int DEFAULT_MAX_CAPACITY = 1000;

// Inserts a new object into the priority queue. Returns true if

// the insertion is successful. If the PQ is full, the insertion

// is aborted, and the method returns false.

public boolean insert(E object);

// Removes the object of highest priority that has been in the

// PQ the longest, and returns it. Returns null if the PQ is empty.

public E remove();

// Returns the object of highest priority that has been in the

// PQ the longest, but does NOT remove it.

// Returns null if the PQ is empty.

public E peek();

// Returns the number of objects currently in the PQ.

public int size();

// Returns an iterator of the objects in the PQ, in no particular

// order. The iterator must be fail-fast.

public Iterator<E> iterator();

// Returns an iterator of the objects in the PQ, in exactly the

// same order in which they will be dequeued. The iterator must

// be fail-fast.

public Iterator<E> orderedIterator();

// Returns the PQ to an empty state.

public void clear();

// Returns true if the PQ is empty, otherwise false

public boolean isEmpty();

// Returns true if the PQ is full, otherwise false. List based

// implementations should always return false.

public boolean isFull();

}

Thus, your project will consist of the following files. You must use exactly these filenames.

 PriorityQueue.java The ADT interface (provided)

 ArrayPriorityQueue.java The ordered array implementation. The ordered array must use binary search to identify the correct insertion point for new additions.

 HeapPriorityQueue.java The binary heap implementation. The heap must be stable.

Additional Details:

 Your project must consist of only the three files specified, no additional source code files are permitted. (Do not hand in a copy of PriorityQueue.java, as it is provided to you).

 You may not make any modifications to the PriorityQueue interface provided. I will grade your project with my copy of this file.

 All source code files must have your name and class account number at the beginning of the file.

 All of the above classes must be in a package named 'data_structures'.

 You may import java.util.Iterator, java.util.NoSuchElementException, and ConcurrentModificationException only. If you feel that you need to import anything else, let me know. You are expected to write all of the code yourself, and you may not use the Java API for any containers.

 Your code must not print anything.

 Correction: Your PriorityQueue methods may not throw or catch any exceptions. Your iterators should throw:

o ConcurrentModificationException

o NoSuchElementException

o UnsupportedOperationException

 Your code should never crash, but must handle any/all error conditions gracefully. i.e. if the user attempts to call the clear() method on an empty PQ, or remove an item from an empty PQ, the program should not crash.

 You must write generic code according to the interface provided. You may not add any public methods to the implementations, but you may add private ones, or inner classes if needed.

 Your code may generate unchecked cast warnings when compiled, but it must compile and run correctly on rohan using JDK1.6 to receive any credit.

 You will need to sort for the orderedIterator method in the binary heap implementation. Sorting methods are available in the class lecture notes, and you are welcome to use the code (now and in the future). However, you should create a private method in the appropriate place rather than using any external class directly. You should select a O(n logn) method that is stable.

The Report:

In addition to the source code, you will write a report that contains an analysis of the runtime complexity of the insert and remove methods for both implementations. You will also perform timing tests on your code, and provide empirical proof that your methods perform in accordance with your analysis. Provide a summary that highlights the strengths and weaknesses of each implementation, and recommend the implementation that you think would be best for this application. We will discuss methods for timing your structures in class.