import java.util.*; /** The HeapPriorityQueue implements the PriorityQueue interface by building a heap in an ArrayList.

The heap is structured so that the "smallest" item is at the top. */ public class HeapPriorityQueue implements PriorityQueue { // Data Fields /** The ArrayList to hold the data */ private ArrayList theData = new ArrayList(); /** Compare two items using their natural ordering. pre: Both left and right implement Comparable. @param left One item @param right The other item @return Negative int if left less than right, 0 if left equals right, positive int if left > right @throws ClassCastException if items are not Comparable */ protected int compare(Object left, Object right) { Comparable comparable = (Comparable) left; return comparable.compareTo(right); } /** Insert an item into the priority queue. pre: The item to be inserted implements Comparable and the ArrayList theData is in heap order. post: The item is in the priority queue and theData is is heap order. @param item The Object to be inserted */ public void insert(Object item) { // Add the item to the heap. theData.add(item); // child is newly inserted item. int child = theData.size() - 1; int parent = (child - 1) / 2; // Find childs parent. // Reheap while (parent >= 0 && compare(theData.get(parent), theData.get(child)) > 0) { swap(parent, child); child = parent; parent = (child - 1) / 2; } } /** Remove an item from the priority queue pre: The ArrayList theData is in heap order. post: The ArrayList theData is in heap order. @return The item with the smallest priority value

size() == 1) { theData. return result. if (leftChild >= theData.set(0.theData. if (theData. parent = minChild.theData.out.get(parent). // Move smaller child up heap if necessary.size()&& compare(theData. // See whether rightChild is smaller. int minChild = leftChild.EmptyQueueException if the queue is empty */ public Object removeMin() { if (isEmpty()) { System. } /* Remove the last item from the ArrayList and place it into the first position.get(rightChild)) > 0) { minChild = rightChild.size() . return null.println("Empty Priority Queue"). if (rightChild < theData. } int rightChild = leftChild + 1.get(leftChild). if (compare(theData. while (true) { int leftChild = 2 * parent + 1.remove(0). } // Save the top of the heap. } } return result. minChild). // Assume leftChild is smaller. */ theData.1)). } // assert: minChild is the index of the smaller child. // If only one item then remove it. theData.size()) { break. int parent = 0. } else { // Heap property is restored. Object result = theData.remove(theData.get(0). break. // The parent starts at the top. } .@throws queue.get(minChild)) > 0) { swap(parent. // Out of heap.

out. System. theData. theData. int j) { Object temp = theData.size(). theData.print (theData.set(i. } public static void main (String args[]) { int HEAPSIZE = 10.get(i) + " "). i<theData. } public void print() { for (int i = 0. ./** Peek at the first item in the queue.println("Empty Priority Queue").get(0).size() == 0.println().get(i).set(j. } /** Swap the items in theData[i] and theData[j] @param i .out. temp). i++) System. return null. } /** Return true if the queue is empty @return true if empty */ public boolean isEmpty() { return theData. } return theData. @post The priority queue remains unchanged @return The item with the smallest priority value @throws EmptyQueueException If the queue is empty */ public Object peek() { if (isEmpty()) { System.Index of the other iterm */ private void swap(int i.get(j)).size(). } /** Return the size of the priority queue @return The size of the queue */ public int getSize() { return theData.out.Index of one of the items @param j . @pre ArrayList theData is in heap order.

print().println(myHeap.Random random = new Random(). myHeap. i++) myHeap. System. } } .nextInt(100))).print().insert(new Integer(random. i< HEAPSIZE.out. for (int i = 0.removeMin()). HeapPriorityQueue myHeap = new HeapPriorityQueue(). myHeap.

Sign up to vote on this title
UsefulNot useful