package u6; import utils.*; import java.util.*; /** * Heap implementation of the PriorityQueue. * Stores elements of the key-element pair type in a Heap structure. * * @author V.Boutchkova */ public class HeapPriorityQueue implements PriorityQueue { private Heap container; public HeapPriorityQueue(Heap emptyHeap) { container = emptyHeap; } public HeapPriorityQueue(Comparator cmp) { container = new ArrayHeap(cmp); } /** * Inserts an element with priority key in this priority queue. * @param key the priority value; smaller keys represent greater priority. * @param element the element value; */ public void insert(Object key, Object element) { container.add(key, element); } /** * Extracts the element with greater priority, i.e. with minimal key. * @return the element with minimal key */ public synchronized Object extract() { Object result = container.readMinElement(); container.extractItem(); return result; } /** * Gets the number of entries in this priority queue. * @return the number of elements */ public int size() { return container.size(); } /** * Checks whether this priority queue is empty. * @return true iff this priority queue is empty */ public boolean isEmpty() { return container.size() == 0; } /** * Reads the minimal key in this priority queue, * without removing the element. */ public Object minKey() { return container.readMinKey(); } /** * Reads the element with minimal key. */ public Object minKeyElement() { return container.readMinElement(); } }