From 7b2dc2ac785d7c0c2e5e7b9c0ce32fb74f966b65 Mon Sep 17 00:00:00 2001 From: Joey Eamigh <55670930+JoeyEamigh@users.noreply.github.com> Date: Mon, 6 Nov 2023 14:35:25 -0500 Subject: [PATCH] assignment 5 --- src/assn05/BinaryHeap.java | 37 ++++++ src/assn05/Main.java | 194 ++++++++++++++++++++++++++++ src/assn05/MaxBinHeapER.java | 153 ++++++++++++++++++++++ src/assn05/Patient.java | 38 ++++++ src/assn05/Prioritized.java | 7 + src/assn05/SimpleEmergencyRoom.java | 49 +++++++ 6 files changed, 478 insertions(+) create mode 100644 src/assn05/BinaryHeap.java create mode 100644 src/assn05/Main.java create mode 100644 src/assn05/MaxBinHeapER.java create mode 100644 src/assn05/Patient.java create mode 100644 src/assn05/Prioritized.java create mode 100644 src/assn05/SimpleEmergencyRoom.java diff --git a/src/assn05/BinaryHeap.java b/src/assn05/BinaryHeap.java new file mode 100644 index 0000000..33c309c --- /dev/null +++ b/src/assn05/BinaryHeap.java @@ -0,0 +1,37 @@ +package assn05; + + +public interface BinaryHeap> { + /** + * 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[] getAsArray(); +} diff --git a/src/assn05/Main.java b/src/assn05/Main.java new file mode 100644 index 0000000..213c20c --- /dev/null +++ b/src/assn05/Main.java @@ -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 binHeap = new MaxBinHeapER(); + 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[] 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 transfer = new MaxBinHeapER(); + 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 postTransfer = new MaxBinHeapER(transfer.getAsArray()); + + int size = postTransfer.size(); + System.out.println("Size: " + size); + + Prioritized[] 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; + } + +} diff --git a/src/assn05/MaxBinHeapER.java b/src/assn05/MaxBinHeapER.java new file mode 100644 index 0000000..9c5f630 --- /dev/null +++ b/src/assn05/MaxBinHeapER.java @@ -0,0 +1,153 @@ +package assn05; + +import java.lang.reflect.Array; +import java.util.ArrayList; +import java.util.List; + +public class MaxBinHeapER> implements BinaryHeap { + + private List> _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[] initialEntries) { + _heap = new ArrayList<>(); + + for (Prioritized 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 newEntry = new Patient(value, priority); + _heap.add(_heap.size(), newEntry); + bubbleUp(_heap.size() - 1); + } + + public void enqueue(V value) { + Prioritized newEntry = new Patient(value); + _heap.add(_heap.size(), newEntry); + bubbleUp(_heap.size() - 1); + } + + @Override + public V dequeue() { + if (size() == 0) + return null; + + Prioritized 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[] getAsArray() { + Prioritized[] result = (Prioritized[]) Array.newInstance(Prioritized.class, size()); + return _heap.toArray(result); + } + + private int bubbleUp(int index) { + if (index == 0) + return index; + + Prioritized child = _heap.get(index); + Prioritized 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 parent = _heap.get(index); + + if (!hasLeftChild(index) && !hasRightChild(index)) + return; + + if (!hasRightChild(index)) { + Prioritized 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 leftChild = _heap.get(leftChildIndex(index)); + Prioritized 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(); + } +} diff --git a/src/assn05/Patient.java b/src/assn05/Patient.java new file mode 100644 index 0000000..b815836 --- /dev/null +++ b/src/assn05/Patient.java @@ -0,0 +1,38 @@ +package assn05; + +import java.util.Random; + +public class Patient> implements Prioritized { + 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 other) { + return this.priority.compareTo(other.getPriority()); + } +} diff --git a/src/assn05/Prioritized.java b/src/assn05/Prioritized.java new file mode 100644 index 0000000..8c079d4 --- /dev/null +++ b/src/assn05/Prioritized.java @@ -0,0 +1,7 @@ +package assn05; + +public interface Prioritized> { + V getValue(); + P getPriority(); + int compareTo(Prioritized other); +} \ No newline at end of file diff --git a/src/assn05/SimpleEmergencyRoom.java b/src/assn05/SimpleEmergencyRoom.java new file mode 100644 index 0000000..4b7a4e4 --- /dev/null +++ b/src/assn05/SimpleEmergencyRoom.java @@ -0,0 +1,49 @@ +package assn05; + +import java.util.ArrayList; +import java.util.List; + +public class SimpleEmergencyRoom { + private List 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 priority = patients.get(i).getPriority(); + if (priority.compareTo(maxPriority) > 0) { + maxPriority = (Integer) priority; + maxIndex = i; + } + } + + return patients.remove(maxIndex); + } + + public void addPatient(V value, P priority) { + Patient patient = new Patient(value, (Integer) priority); + patients.add(patient); + } + + public void addPatient(V value) { + Patient patient = new Patient(value); + patients.add(patient); + } + + public List getPatients() { + return patients; + } + + public int size() { + return patients.size(); + } + +}