package exam; class IndexMinPQ> implements IIndexPQ{ private int n; private int[] pq; private int[] qp; private Key[] keys; private void swim(int priority) { if (priority == 0) return; while (priority> 0 && larger((priority-1)/2, priority)){ exch(priority, (priority-1)/2); priority = (priority-1)/2; } } private boolean larger(int priorityI, int priorityJ) { return keys[qp[priorityI]].compareTo(this.keys[qp[priorityJ]]) > 0; } private void sink(int priority) { while(2*priority + 1 < n){ // j is left subnode. int j = 2*priority + 1; // Set j to largest subnode. if(j+1 -1){ return keys[peek()]; } else{ return null; } } @Override public int peek() { // Returns key index with highest priority if (isEmpty()) { throw new IllegalArgumentException("Priority queue is empty"); } return qp[0]; } @Override public int poll() { // Returns key index with highest priority and removes it from the PQ. int keyIndex = peek(); delete(keyIndex); return keyIndex; } @Override public int size() { return n; } @Override public boolean isEmpty() { return n==0; } }