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

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

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

TheProgram:

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

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

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 extends Iterable {

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 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 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.

/* PQTester.java
A simple driver program to test your ArrayPQ and BinaryHeapPQ.
NOTE: Does not test all of the methods in your PQs. The fact
that your program runs with this driver does not mean that it
is error free.
Requires PQTestObject class
Alan Riggins
CS310 Fall 2012
Programming Assignment #2
*/

import java.util.Iterator;
import java.util.ConcurrentModificationException;
import data_structures.*;

public class PQTester {
public static void main(String [] args) {
final int SIZE = 50;
long sequence_number = 0;
int priority;

//////////////////////////////////////////////////////////////////////
/// Select the implementation you wish to test
PriorityQueue queue =
new ArrayPriorityQueue(SIZE);
//////////////////////////////////////////////////////////////////////
System.out.println(“Now testing the ARRRAY implementation”);
PQTestObject [] array = new PQTestObject[SIZE];
for(int i=0; i < SIZE; i++) array[i] = new PQTestObject((int) ((1000*Math.random() % 20) +1), sequence_number++); for(int i=0; i < SIZE; i++) queue.insert(array[i]); // check to see what happens if insertion beyond capacity try { if(queue.insert(new PQTestObject(1,1))) throw new RuntimeException("ERROR, inserted beyond capacity"); } catch(Exception e) { e.printStackTrace(); } if(queue.size() != SIZE) System.out.println("ERROR, wrong size!"); System.out.println("Now printing with the iterator. " + "They should come out in any order.\n" + "Priorty SequenceNumber"); Iterator iter = queue.iterator();
while(iter.hasNext())
System.out.print(iter.next()+ ” “);
System.out.println();
try {
PQTestObject obj = iter.next();

}
catch(Exception e) {
System.out.println(“Good, iterator passed.”);
}
for(PQTestObject o : queue)
;
System.out.println(“==============================================”);
System.out.println(“Now removing them all…”);
for(int i=0; i < SIZE; i++) System.out.print(queue.remove()+ " "); System.out.println(); if(!queue.isEmpty()) System.out.println("ERROR, queue should be empty"); queue.clear(); System.out.println("=============================================="); queue.insert(new PQTestObject(25,1)); queue.insert(new PQTestObject(25,2)); queue.insert(new PQTestObject(1,1)); queue.insert(new PQTestObject(1,2)); queue.insert(new PQTestObject(25,3)); System.out.println("Now emptying remaining elements"); while(!queue.isEmpty()) System.out.println(queue.remove()); ////////////////////////////////////////////////////////////////////// queue = new HeapPriorityQueue(SIZE);
//////////////////////////////////////////////////////////////////////
System.out.println(“\n\nNow testing the HEAP implementation”);
array = new PQTestObject[SIZE];
for(int i=0; i < SIZE; i++) array[i] = new PQTestObject((int) ((1000*Math.random() % 20) +1), sequence_number++); for(int i=0; i < SIZE; i++) queue.insert(array[i]); // check to see what happens if insertion beyond capacity try { if(queue.insert(new PQTestObject(1,1))) throw new RuntimeException("ERROR, inserted beyond capacity"); } catch(Exception e) { e.printStackTrace(); } if(queue.size() != SIZE) System.out.println("ERROR, wrong size!"); System.out.println("Now printing with the iterator. " + "They should come out in any order.\n" + "Priorty SequenceNumber"); iter = queue.iterator();

while(iter.hasNext())
System.out.print(iter.next()+ ” “);
System.out.println();
try {
PQTestObject obj = iter.next();
}
catch(Exception e) {
System.out.println(“Good, iterator passed.”);
}
for(PQTestObject o : queue)
;
System.out.println(“==============================================”);
System.out.println(“Now removing them all…”);
for(int i=0; i < SIZE; i++) System.out.print(queue.remove()+ " "); System.out.println(); if(!queue.isEmpty()) System.out.println("ERROR, queue should be empty"); queue.clear(); System.out.println("=============================================="); queue.insert(new PQTestObject(25,1)); queue.insert(new PQTestObject(25,2)); queue.insert(new PQTestObject(1,1)); queue.insert(new PQTestObject(1,2)); queue.insert(new PQTestObject(25,3)); System.out.println("Now emptying remaining elements"); while(!queue.isEmpty()) System.out.println(queue.remove()); } }

/* PQTestObject.java
A simple testing object that has a priority. The sequence
number in this class is NOT the insertion sequence number
you will have in the BinaryHeapPQ class. It is only used
for verification of correct behavior, and never used in
ordering objects of this class.
*/

public class PQTestObject implements Comparable {
private int priority;
private long sequence_number;

public PQTestObject(int p, long s) {
priority = p;
sequence_number = s;
}

public int compareTo(PQTestObject o) {
return priority – o.priority;
}

public String toString() {
return priority + ” ” + sequence_number;
}
}

/* PQTimer.java
A simple driver program to run timing tests on your ArrayPQ
and BinaryHeapPQ.
Programming Assignment #2
*/

import java.util.Iterator;
import java.util.ConcurrentModificationException;
import data_structures.*;

public class PQTimer {
public static void main(String [] args) {

///////////////////////////////////////////////////////////
/// Change this variable to something smaller if timing takes too long.
final int INITIAL_SIZE = 15000;
///////////////////////////////////////////////////////////

final int ITERATIONS = 10000;
final int NUMBER_OF_STEPS = 15;
final int MAX_SIZE = INITIAL_SIZE * NUMBER_OF_STEPS +1;
final int NUMBER_OF_PRIORITIES = 20;

int size = INITIAL_SIZE;
long sequence_number = 0;
long start, stop;
int priority;

PQTestObject [] array = new PQTestObject[MAX_SIZE];
for(int i=0; i < MAX_SIZE; i++) array[i] = new PQTestObject((int) ((10000*Math.random() % NUMBER_OF_PRIORITIES) +1), sequence_number++); for(int j=0; j < 15; j++) { PriorityQueue queue =
new HeapPriorityQueue(MAX_SIZE);
queue.clear();
for(int i = 0; i < size; i++) queue.insert(array[i]); start = System.currentTimeMillis(); // start the timer for(int i = 0; i < ITERATIONS; i++) { queue.insert(array[(int)(100000*Math.random() % MAX_SIZE)]); queue.remove(); } stop = System.currentTimeMillis(); System.out.println("HeapPQ, Time for n=" + size + ": " + (stop-start)); queue.clear(); queue = new ArrayPriorityQueue(MAX_SIZE);
for(int i = 0; i < size; i++) queue.insert(array[i]);

start = System.currentTimeMillis(); // start the timer
for(int i = 0; i < ITERATIONS; i++) { queue.insert(array[(int)(100000*Math.random() % MAX_SIZE)]); queue.remove(); } stop = System.currentTimeMillis(); System.out.println("ArrayPQ, Time for n=" + size + ": " + (stop-start)+"\n"); size += INITIAL_SIZE; } } }

Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER