// CSE 373, Winter 2013, Marty Stepp // A HeapPriorityQueue implements a priority queue of objects using a // binary min-heap array as the internal data structure. // // This is the best version of this file to use as an example for your HW5. // // (In your homework, you should not declare E as a Comparable type; // just cast to Comparable in appropriate place(s) right before you compare.) import java.util.Arrays; public class HeapPriorityQueue> implements PriorityQueue { private E[] elements; private int size; // ... @SuppressWarnings("unchecked") public HeapPriorityQueue() { elements = (E[]) new Comparable[10]; size = 0; } // ... public void add(E value) { // resize if needed if (size + 1 >= elements.length) { elements = Arrays.copyOf(elements, 2 * elements.length); } int index = size + 1; elements[index] = value; // put in last slot // "bubble up" until in order again boolean done = false; while (!done && hasParent(index)) { int daddy = parent(index); if (elements[daddy].compareTo(elements[index]) > 0) { swap(elements, index, daddy); index = daddy; } else { done = true; } } size++; } // ... public void clear() { // TODO: implement this method } // ... public boolean isEmpty() { // TODO: implement this method return true; } // ... public E peek() { // bug: assumes that queue is not empty return elements[1]; } // ... public E remove() { // TODO: implement this method E result = elements[1]; elements[1] = elements[size]; elements[size] = null; size--; // bubble down until order is restored boolean done = false; int index = 1; while (!done && hasLeftChild(index)) { int left = leftChild(index); int kiddo = left; if (hasRightChild(index)) { int right = rightChild(index); if (elements[right].compareTo(elements[left]) < 0) { kiddo = right; } } if (elements[index].compareTo(elements[kiddo]) > 0) { swap(elements, index, kiddo); index = kiddo; } else { done = true; } } return result; } public int size() { return size; } public String toString() { // this is an incorrect implementation for debugging return Arrays.toString(elements); } // helpers for navigating indexes up/down the tree private int parent(int index) { return index / 2; } // Returns the index of the left child of the given index in the heap. private int leftChild(int index) { return index * 2; } // Returns the index of the right child of the given index in the heap. private int rightChild(int index) { return index * 2 + 1; } // Returns true if the given index has a parent in the heap (true for all elements above 1). private boolean hasParent(int index) { return index > 1; } // Returns true if the given index has a left child element in the heap. private boolean hasLeftChild(int index) { return leftChild(index) <= size; } // Returns true if the given index has a right child element in the heap. private boolean hasRightChild(int index) { return rightChild(index) <= size; } // Swaps the elements at the two given indexes in the array. private void swap(E[] a, int index1, int index2) { E temp = a[index1]; a[index1] = a[index2]; a[index2] = temp; } }