File size: 4,022 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
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;
}
@SuppressWarnings("unchecked")
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;
}
}
@Override
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++;
}
@Override
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]);
}
@Override
public boolean contains(int keyIndex) {
// If the key index has a priority then it exists.
if (pq[keyIndex] != -1){
return true;
}
else{
return false;
}
}
@Override
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);
}
}
@Override
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];
}
@Override
public Key peekKey() {
// Returns the key with highest priority
if (peek() > -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;
}
} |