assignment 5
This commit is contained in:
37
src/assn05/BinaryHeap.java
Normal file
37
src/assn05/BinaryHeap.java
Normal file
@@ -0,0 +1,37 @@
|
||||
package assn05;
|
||||
|
||||
|
||||
public interface BinaryHeap<V,P extends Comparable<P>> {
|
||||
/**
|
||||
* Returns number of elements in heap
|
||||
* @return numbers of elements in heap
|
||||
*/
|
||||
int size();
|
||||
|
||||
/**
|
||||
* Create new hospital.Prioritized object and insert it into the heap
|
||||
* @param value
|
||||
* @param priority
|
||||
*/
|
||||
void enqueue(V value, P priority);
|
||||
|
||||
/**
|
||||
* Remove the element with the smallest priority from the heap
|
||||
* and return its value
|
||||
* @return the value of the removed element
|
||||
*/
|
||||
V dequeue();
|
||||
|
||||
/**
|
||||
* return the largest value in the heap without removing it
|
||||
* @return the largest value in the heap
|
||||
*/
|
||||
V getMax();
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves contents of heap as an array.
|
||||
* @return array of hospital.Prioritized objects in the order stored in the heap
|
||||
*/
|
||||
Prioritized<V,P>[] getAsArray();
|
||||
}
|
||||
194
src/assn05/Main.java
Normal file
194
src/assn05/Main.java
Normal file
@@ -0,0 +1,194 @@
|
||||
package assn05;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
testP1();
|
||||
testP2();
|
||||
testP3();
|
||||
testP4();
|
||||
}
|
||||
|
||||
// test Part 1
|
||||
public static void testP1() {
|
||||
System.out.println("\nSimpleEmergencyRoom Tests (Part 1)...");
|
||||
|
||||
// add patients
|
||||
SimpleEmergencyRoom simplePQ = new SimpleEmergencyRoom();
|
||||
simplePQ.addPatient("Patient 1", 1);
|
||||
simplePQ.addPatient("Patient 2", 2);
|
||||
simplePQ.addPatient("Patient 3", 3);
|
||||
simplePQ.addPatient("Patient 4", 4);
|
||||
simplePQ.addPatient("Patient 99", 99);
|
||||
simplePQ.addPatient("Patient 5", 5);
|
||||
simplePQ.addPatient("Patient 6", 6);
|
||||
simplePQ.addPatient("Patient 7", 7);
|
||||
simplePQ.addPatient("Patient 8", 8);
|
||||
simplePQ.addPatient("Patient 9", 9);
|
||||
simplePQ.addPatient("Patient -1", -1);
|
||||
simplePQ.addPatient("Patient 0", 0);
|
||||
|
||||
int size = simplePQ.size();
|
||||
System.out.println("Size: " + size);
|
||||
|
||||
// dequeue patients
|
||||
System.out.println("Dequeueing patients...");
|
||||
for (int i = 0; i < size; i++) {
|
||||
System.out.println("Dequeued " + simplePQ.dequeue().getValue());
|
||||
}
|
||||
|
||||
System.out.println("SimpleEmergencyRoom Tests Complete");
|
||||
}
|
||||
|
||||
// test Part 2
|
||||
public static void testP2() {
|
||||
System.out.println("\nMaxBinHeapER Tests (Part 2)...");
|
||||
|
||||
// add patients
|
||||
MaxBinHeapER<String, Integer> binHeap = new MaxBinHeapER<String, Integer>();
|
||||
binHeap.enqueue("Patient 1", 1);
|
||||
binHeap.enqueue("Patient 2", 2);
|
||||
binHeap.enqueue("Patient 3", 3);
|
||||
binHeap.enqueue("Patient 4", 4);
|
||||
binHeap.enqueue("Patient 99", 99);
|
||||
binHeap.enqueue("Patient 5", 5);
|
||||
binHeap.enqueue("Patient 6", 6);
|
||||
binHeap.enqueue("Patient 7", 7);
|
||||
binHeap.enqueue("Patient 8", 8);
|
||||
binHeap.enqueue("Patient 9", 9);
|
||||
binHeap.enqueue("Patient -1", -1);
|
||||
binHeap.enqueue("Patient 0", 0);
|
||||
|
||||
int size = binHeap.size();
|
||||
System.out.println("Size: " + size);
|
||||
|
||||
String max = binHeap.getMax();
|
||||
System.out.println("Max: " + max);
|
||||
|
||||
Prioritized<String, Integer>[] inOrder = binHeap.getAsArray();
|
||||
System.out.print("Array: [");
|
||||
for (int i = 0; i < size; i++) {
|
||||
System.out.print((inOrder[i].getPriority()) + (i < size - 1 ? ", " : ""));
|
||||
}
|
||||
System.out.println("]");
|
||||
|
||||
// dequeue patients
|
||||
System.out.println("Dequeueing patients...");
|
||||
for (int i = 0; i < size; i++) {
|
||||
System.out.println("Dequeued " + binHeap.dequeue());
|
||||
}
|
||||
|
||||
System.out.println("MaxBinHeapER Tests Complete");
|
||||
}
|
||||
|
||||
/*
|
||||
* Part 3
|
||||
*/
|
||||
public static void testP3() {
|
||||
System.out.println("\nER Transfer Tests (Part 3)...");
|
||||
|
||||
MaxBinHeapER<String, Integer> transfer = new MaxBinHeapER<String, Integer>();
|
||||
transfer.enqueue("Patient 1", 1);
|
||||
transfer.enqueue("Patient 2", 2);
|
||||
transfer.enqueue("Patient 3", 3);
|
||||
transfer.enqueue("Patient 4", 4);
|
||||
transfer.enqueue("Patient 99", 99);
|
||||
transfer.enqueue("Patient 5", 5);
|
||||
transfer.enqueue("Patient 6", 6);
|
||||
transfer.enqueue("Patient 7", 7);
|
||||
transfer.enqueue("Patient 8", 8);
|
||||
transfer.enqueue("Patient 9", 9);
|
||||
transfer.enqueue("Patient -1", -1);
|
||||
transfer.enqueue("Patient 0", 0);
|
||||
|
||||
System.out.println("Transferring patients...");
|
||||
MaxBinHeapER<String, Integer> postTransfer = new MaxBinHeapER<String, Integer>(transfer.getAsArray());
|
||||
|
||||
int size = postTransfer.size();
|
||||
System.out.println("Size: " + size);
|
||||
|
||||
Prioritized<String, Integer>[] inOrder = postTransfer.getAsArray();
|
||||
System.out.print("Array: [");
|
||||
for (int i = 0; i < size; i++) {
|
||||
System.out.print((inOrder[i].getPriority()) + (i < size - 1 ? ", " : ""));
|
||||
}
|
||||
System.out.println("]");
|
||||
|
||||
System.out.println("ER Transfer Tests Complete");
|
||||
}
|
||||
|
||||
/*
|
||||
* Part 4
|
||||
*/
|
||||
public static void testP4() {
|
||||
System.out.println("\nER Transfer Tests (Part 3)...");
|
||||
double[] results = compareRuntimes();
|
||||
|
||||
System.out
|
||||
.println("\nSimpleEmergencyRoom dequeue time: " + results[0] + " ns");
|
||||
System.out.println("SimpleEmergencyRoom dequeue time: "
|
||||
+ results[0] / 1000000 + " ms");
|
||||
System.out.println("SimpleEmergencyRoom average dequeue time per patient: "
|
||||
+ results[1] + " ns");
|
||||
|
||||
System.out.println("\nMaxBinHeapER dequeue time: " + results[2] + " ns");
|
||||
System.out.println("MaxBinHeapER dequeue time: " + results[2] / 1000000
|
||||
+ " ms");
|
||||
System.out.println("MaxBinHeapER average dequeue time per patient: "
|
||||
+ results[3] + " ns");
|
||||
|
||||
System.out.println("\nER Transfer Tests Complete");
|
||||
}
|
||||
|
||||
public static void fillER(MaxBinHeapER complexER) {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
complexER.enqueue(i);
|
||||
}
|
||||
}
|
||||
|
||||
public static void fillER(SimpleEmergencyRoom simpleER) {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
simpleER.addPatient(i);
|
||||
}
|
||||
}
|
||||
|
||||
public static Patient[] makePatients() {
|
||||
Patient[] patients = new Patient[10];
|
||||
for (int i = 0; i < 10; i++) {
|
||||
patients[i] = new Patient(i);
|
||||
}
|
||||
return patients;
|
||||
}
|
||||
|
||||
public static double[] compareRuntimes() {
|
||||
// Array which you will populate as part of Part 4
|
||||
double[] results = new double[4];
|
||||
|
||||
SimpleEmergencyRoom simplePQ = new SimpleEmergencyRoom();
|
||||
fillER(simplePQ);
|
||||
|
||||
int origSize = simplePQ.size();
|
||||
long currentTime = System.nanoTime();
|
||||
|
||||
for (int i = 0; i < origSize; i++)
|
||||
simplePQ.dequeue();
|
||||
|
||||
results[0] = System.nanoTime() - currentTime;
|
||||
results[1] = results[0] / origSize;
|
||||
|
||||
MaxBinHeapER binHeap = new MaxBinHeapER();
|
||||
fillER(binHeap);
|
||||
|
||||
origSize = binHeap.size();
|
||||
currentTime = System.nanoTime();
|
||||
|
||||
for (int i = 0; i < origSize; i++)
|
||||
binHeap.dequeue();
|
||||
|
||||
results[2] = System.nanoTime() - currentTime;
|
||||
results[3] = results[2] / origSize;
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
}
|
||||
153
src/assn05/MaxBinHeapER.java
Normal file
153
src/assn05/MaxBinHeapER.java
Normal file
@@ -0,0 +1,153 @@
|
||||
package assn05;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MaxBinHeapER<V, P extends Comparable<P>> implements BinaryHeap<V, P> {
|
||||
|
||||
private List<Prioritized<V, P>> _heap;
|
||||
|
||||
/**
|
||||
* Constructor that creates an empty heap of hospital.Prioritized objects.
|
||||
*/
|
||||
public MaxBinHeapER() {
|
||||
_heap = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor that builds a heap given an initial array of hospital.Prioritized
|
||||
* objects.
|
||||
*/
|
||||
public MaxBinHeapER(Prioritized<V, P>[] initialEntries) {
|
||||
_heap = new ArrayList<>();
|
||||
|
||||
for (Prioritized<V, P> entry : initialEntries)
|
||||
_heap.add(entry);
|
||||
|
||||
for (int i = size() - 1; i >= 0; i--)
|
||||
bubbleDown(i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return _heap.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enqueue(V value, P priority) {
|
||||
Prioritized<V, P> newEntry = new Patient(value, priority);
|
||||
_heap.add(_heap.size(), newEntry);
|
||||
bubbleUp(_heap.size() - 1);
|
||||
}
|
||||
|
||||
public void enqueue(V value) {
|
||||
Prioritized<V, P> newEntry = new Patient(value);
|
||||
_heap.add(_heap.size(), newEntry);
|
||||
bubbleUp(_heap.size() - 1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public V dequeue() {
|
||||
if (size() == 0)
|
||||
return null;
|
||||
|
||||
Prioritized<V, P> max = _heap.get(0);
|
||||
_heap.set(0, _heap.get(size() - 1));
|
||||
_heap.remove(size() - 1);
|
||||
bubbleDown(0);
|
||||
|
||||
return max.getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getMax() {
|
||||
if (size() == 0)
|
||||
return null;
|
||||
|
||||
return _heap.get(0).getValue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Prioritized<V, P>[] getAsArray() {
|
||||
Prioritized<V, P>[] result = (Prioritized<V, P>[]) Array.newInstance(Prioritized.class, size());
|
||||
return _heap.toArray(result);
|
||||
}
|
||||
|
||||
private int bubbleUp(int index) {
|
||||
if (index == 0)
|
||||
return index;
|
||||
|
||||
Prioritized<V, P> child = _heap.get(index);
|
||||
Prioritized<V, P> parent = _heap.get(parentIndex(index));
|
||||
|
||||
if (child.getPriority().compareTo(parent.getPriority()) > 0) {
|
||||
_heap.set(parentIndex(index), child);
|
||||
_heap.set(index, parent);
|
||||
return bubbleUp(parentIndex(index));
|
||||
} else {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
private void bubbleDown(int index) {
|
||||
if (index >= size())
|
||||
return;
|
||||
|
||||
Prioritized<V, P> parent = _heap.get(index);
|
||||
|
||||
if (!hasLeftChild(index) && !hasRightChild(index))
|
||||
return;
|
||||
|
||||
if (!hasRightChild(index)) {
|
||||
Prioritized<V, P> leftChild = _heap.get(leftChildIndex(index));
|
||||
|
||||
if (leftChild.getPriority().compareTo(parent.getPriority()) > 0) {
|
||||
_heap.set(leftChildIndex(index), parent);
|
||||
_heap.set(index, leftChild);
|
||||
bubbleDown(leftChildIndex(index));
|
||||
}
|
||||
} else {
|
||||
Prioritized<V, P> leftChild = _heap.get(leftChildIndex(index));
|
||||
Prioritized<V, P> rightChild = _heap.get(rightChildIndex(index));
|
||||
|
||||
if (leftChild.getPriority().compareTo(rightChild.getPriority()) > 0) {
|
||||
if (leftChild.getPriority().compareTo(parent.getPriority()) > 0) {
|
||||
_heap.set(leftChildIndex(index), parent);
|
||||
_heap.set(index, leftChild);
|
||||
bubbleDown(leftChildIndex(index));
|
||||
}
|
||||
} else {
|
||||
if (rightChild.getPriority().compareTo(parent.getPriority()) > 0) {
|
||||
_heap.set(rightChildIndex(index), parent);
|
||||
_heap.set(index, rightChild);
|
||||
bubbleDown(rightChildIndex(index));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean hasLeftChild(int index) {
|
||||
return validIndex(leftChildIndex(index));
|
||||
}
|
||||
|
||||
private boolean hasRightChild(int index) {
|
||||
return validIndex(rightChildIndex(index));
|
||||
}
|
||||
|
||||
static int leftChildIndex(int index) {
|
||||
return 2 * index + 1;
|
||||
}
|
||||
|
||||
static int rightChildIndex(int index) {
|
||||
return 2 * index + 2;
|
||||
}
|
||||
|
||||
static int parentIndex(int index) {
|
||||
return (index - 1) / 2;
|
||||
}
|
||||
|
||||
private boolean validIndex(int index) {
|
||||
return index >= 0 && index < size();
|
||||
}
|
||||
}
|
||||
38
src/assn05/Patient.java
Normal file
38
src/assn05/Patient.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package assn05;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class Patient<V, Integer extends Comparable<Integer>> implements Prioritized<V, Integer> {
|
||||
private Integer priority;
|
||||
private V value;
|
||||
|
||||
public Patient(V value, Integer priority) {
|
||||
this.value = value;
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public Patient(V value) {
|
||||
this.value = value;
|
||||
calculatePriority();
|
||||
}
|
||||
|
||||
@Override
|
||||
public V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
private void calculatePriority() {
|
||||
Random random = new Random();
|
||||
this.priority = (Integer) new java.lang.Integer(random.nextInt(1000000));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Prioritized<V, Integer> other) {
|
||||
return this.priority.compareTo(other.getPriority());
|
||||
}
|
||||
}
|
||||
7
src/assn05/Prioritized.java
Normal file
7
src/assn05/Prioritized.java
Normal file
@@ -0,0 +1,7 @@
|
||||
package assn05;
|
||||
|
||||
public interface Prioritized<V,P extends Comparable<P>> {
|
||||
V getValue();
|
||||
P getPriority();
|
||||
int compareTo(Prioritized<V, P> other);
|
||||
}
|
||||
49
src/assn05/SimpleEmergencyRoom.java
Normal file
49
src/assn05/SimpleEmergencyRoom.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package assn05;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SimpleEmergencyRoom {
|
||||
private List<Patient> patients;
|
||||
|
||||
public SimpleEmergencyRoom() {
|
||||
patients = new ArrayList<>();
|
||||
}
|
||||
|
||||
public Patient dequeue() {
|
||||
if (patients.size() == 0)
|
||||
return null;
|
||||
|
||||
Integer maxPriority = java.lang.Integer.MIN_VALUE;
|
||||
int maxIndex = 0;
|
||||
|
||||
for (int i = 0; i < patients.size(); i++) {
|
||||
Comparable<Integer> priority = patients.get(i).getPriority();
|
||||
if (priority.compareTo(maxPriority) > 0) {
|
||||
maxPriority = (Integer) priority;
|
||||
maxIndex = i;
|
||||
}
|
||||
}
|
||||
|
||||
return patients.remove(maxIndex);
|
||||
}
|
||||
|
||||
public <V, P> void addPatient(V value, P priority) {
|
||||
Patient patient = new Patient(value, (Integer) priority);
|
||||
patients.add(patient);
|
||||
}
|
||||
|
||||
public <V> void addPatient(V value) {
|
||||
Patient patient = new Patient(value);
|
||||
patients.add(patient);
|
||||
}
|
||||
|
||||
public List getPatients() {
|
||||
return patients;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return patients.size();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user