// Textbook fragment 08.13 /** * Realization of a priority queue by means of a heap. A complete * binary tree realized by means of an array list is used to * represent the heap. */ public class HeapPriorityQueue implements PriorityQueue { protected CompleteBinaryTree> heap; // underlying heap protected Comparator comp; // comparator for the keys /** Inner class for heap entries. */ protected static class MyEntry implements Entry { protected K key; protected V value; public MyEntry(K k, V v) { key = k; value = v; } public K getKey() { return key; } public V getValue() { return value; } public String toString() { return "(" + key + "," + value + ")"; } } /** Creates an empty heap with the default comparator */ public HeapPriorityQueue() { heap = new ArrayListCompleteBinaryTree>(); // use an array list comp = new DefaultComparator(); // use the default comparator } /** Creates an empty heap with the given comparator */ public HeapPriorityQueue(Comparator c) { heap = new ArrayListCompleteBinaryTree>(); comp = c; } /** Returns the size of the heap */ public int size() { return heap.size(); } /** Returns whether the heap is empty */ public boolean isEmpty() { return heap.size() == 0; }