Datasets:
ArXiv:
License:
| package exam; | |
| class IndexMinPQ<Key extends Comparable<Key>> implements IIndexPQ<Key>{ | |
| 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<n && larger(j, j+1)) j++; | |
| if(!larger(priority, j)) break; | |
| exch(priority, j); | |
| priority = j; | |
| } | |
| } | |
| private void exch(int priorityI, int priorityJ) { | |
| int keyIndexI = qp[priorityI]; | |
| int keyIndexJ = qp[priorityJ]; | |
| pq[keyIndexI] = pq[keyIndexJ]; | |
| pq[keyIndexJ] = priorityI; | |
| qp[priorityI] = keyIndexJ; | |
| qp[priorityJ] = keyIndexI; | |
| } | |
| public IndexMinPQ(int maxN){ | |
| keys = (Key[]) new Comparable[maxN]; | |
| pq = new int[maxN]; | |
| qp = new int[maxN]; | |
| for(int i = 0; i<maxN; i++){ | |
| pq[i] = -1; | |
| qp[i] = -1; | |
| } | |
| } | |
| public void add(int keyIndex, Key key) { | |
| if (contains(keyIndex)) { | |
| throw new IllegalArgumentException("Index already in priority queue"); | |
| } | |
| int priority = n; | |
| qp[priority] = keyIndex; | |
| keys[keyIndex] = key; | |
| pq[keyIndex] = priority; | |
| swim(priority); | |
| n++; | |
| } | |
| public void changeKey(int keyIndex, Key key) { | |
| if (!contains(keyIndex)) { | |
| throw new IllegalArgumentException("Index not on priority queue"); | |
| } | |
| keys[keyIndex] = key; | |
| swim(pq[keyIndex]); | |
| sink(pq[keyIndex]); | |
| } | |
| public boolean contains(int keyIndex) { | |
| // If the key index has a priority then it exists. | |
| if (pq[keyIndex] != -1){ | |
| return true; | |
| } | |
| else{ | |
| return false; | |
| } | |
| } | |
| public void delete(int keyIndex) { | |
| if (!contains(keyIndex)) { | |
| throw new IllegalArgumentException("Index not on priority queue"); | |
| } | |
| n--; | |
| int priority = pq[keyIndex]; | |
| int priorityToDelete = n; | |
| exch(priority, priorityToDelete); | |
| keyIndex = qp[priorityToDelete]; | |
| keys[keyIndex] = null; | |
| qp[priorityToDelete] = -1; | |
| pq[keyIndex] = -1; | |
| if(priority != priorityToDelete){ | |
| swim(priority); | |
| sink(priority); | |
| } | |
| } | |
| public Key getKey(int keyIndex) { | |
| // Returns the key at the key index. | |
| if (!contains(keyIndex)) { | |
| throw new IllegalArgumentException("Index not on priority queue"); | |
| } | |
| return keys[keyIndex]; | |
| } | |
| public Key peekKey() { | |
| // Returns the key with highest priority | |
| if (peek() > -1){ | |
| return keys[peek()]; | |
| } | |
| else{ | |
| return null; | |
| } | |
| } | |
| public int peek() { | |
| // Returns key index with highest priority | |
| if (isEmpty()) { | |
| throw new IllegalArgumentException("Priority queue is empty"); | |
| } | |
| return qp[0]; | |
| } | |
| public int poll() { | |
| // Returns key index with highest priority and removes it from the PQ. | |
| int keyIndex = peek(); | |
| delete(keyIndex); | |
| return keyIndex; | |
| } | |
| public int size() { | |
| return n; | |
| } | |
| public boolean isEmpty() { | |
| return n==0; | |
| } | |
| } |