-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapPQ.java
More file actions
346 lines (278 loc) · 7.76 KB
/
HeapPQ.java
File metadata and controls
346 lines (278 loc) · 7.76 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package cs2321;
import java.util.Comparator;
import net.datastructures.*;
/**
* A Adaptable PriorityQueue based on an heap.
*
* Course: CS2321 Section ALL
* Assignment: #3
* @author Giulio Vario
*/
public class HeapPQ<K,V> implements AdaptablePriorityQueue<K,V> {
ArrayList<Entry<K,V>> heapPQ;
Comparator<K> comp;
public HeapPQ() {
heapPQ = new ArrayList<Entry<K,V>>();
comp = new DefaultComparator<K>();
}
public HeapPQ(Comparator<K> c) {
heapPQ = new ArrayList<Entry<K,V>>();
comp = c;
}
/**
* The entry should be bubbled up to its appropriate position
* @param int move the entry at index j higher if necessary, to restore the heap property
*/
public void upheap(int j){
if(j == 0) { //base case, if we are at the root we are done
return;
}
int p = parent(j);//grab the parent
if(comp.compare(heapPQ.get(j).getKey(), heapPQ.get(p).getKey()) <= 0) { //if the child is less than the parent, swap
swap(j,p);
}
upheap(p);//go again with the parent
}
/**
* The entry should be bubbled down to its appropriate position
* @param int move the entry at index j lower if necessary, to restore the heap property
*/
public void downheap(int j){
if(!(hasLeft(j))) { //if the node does not have a left child we are done
return;
}
int s = left(j);//grab the left child
if(hasRight(j) && comp.compare(heapPQ.get(s).getKey(), heapPQ.get(right(j)).getKey()) >= 0) { //if the left child is greater than the right, the right is the new smallest
s = right(j);
}
if(comp.compare(heapPQ.get(s).getKey(), heapPQ.get(j).getKey()) <= 0) { //if the smallest child is smaller than the parent, swap
swap(s, j);
}
downheap(s); //downheap again with the new smallest
}
/**
* Returns the size of the PQ
*
* @return size of PQ
*/
@TimeComplexity ("O(1)")
@Override
public int size() {
/*
* TCJ
* The method just returns an int variable, which is a primitive operation. This is constant time.
*/
return heapPQ.size(); //get the size of the heap
}
/**
* Checks if the PQ is empty
*
* @return true if PQ is empty, otherwise false
*/
@TimeComplexity ("O(1)")
@Override
public boolean isEmpty() {
/*
* TCJ
* The method returns a comparison, which is a primitive operation. This is constant time
*/
return heapPQ.isEmpty(); //check if the heap is empty
}
/**
* Inserts a new entry in the heap and keeps heap order
*
* @param K new key
* @param V new value
* @return entry inserted
*/
@TimeComplexity ("O(log n)")
@Override
public Entry<K, V> insert(K key, V value) throws IllegalArgumentException {
/*
* TCJ
* Moves log n steps down the tree before inserting and then performs a log n operation
*/
boolean checked = checkKey(key);
PQEntry<K,V> insertedEntry = new PQEntry<K,V>(key,value, heapPQ.size()); //create a new entry
heapPQ.addLast(insertedEntry); //put entry at the end of the list
upheap(heapPQ.size() - 1); //restore heap order
return insertedEntry; //return the new entry
}
protected boolean checkKey(K key) throws IllegalArgumentException{
try {
return (comp.compare(key, key) == 0);
} catch(ClassCastException e) {
throw new IllegalArgumentException();
}
}
/**
* Finds the minimum key in the heap
*
* @return minimum entry
*/
@TimeComplexity ("O(1)")
@Override
public Entry<K, V> min() {
/*
* TCJ
* Accesses the array, which is a constant time operation
*/
if(heapPQ.isEmpty()) { //check if the heap is empty
return null;
}
return heapPQ.get(0); //get the root node
}
/**
* Finds the minimum key and removes it
*
* @return the removed key
*/
@TimeComplexity ("O(log n)")
@Override
public Entry<K, V> removeMin() {
/*
* TCJ
* Removes entry by stepping down the tree logn steps and restores the heap
*/
if(heapPQ.isEmpty()) { //check if the heap is empty
return null;
}
PQEntry<K,V> removedVal = (PQEntry<K, V>) heapPQ.get(0); //get the minimum value
heapPQ.set(0, heapPQ.get(heapPQ.size() - 1)); //replace the minimum entry with the last entry
heapPQ.removeLast(); //get rid of the last node
downheap(0); //restore heap order
return removedVal; //return the removed entry
}
/**
* Removes selected entry from the heap and restores heap order
*
* @param entry to be removed
*/
@TimeComplexity ("O(log n)")
@Override
public void remove(Entry<K, V> entry) throws IllegalArgumentException {
/*
* TCJ
* Removes entry by stepping down the tree logn steps and restores the heap
*/
PQEntry<K,V> removedEntry = (PQEntry<K,V>) validateEntry(entry); //validate the entry and set it to a new entry variable
int i = removedEntry.getIndex();
if(i == heapPQ.size() - 1) { //if the entry to remove is at the end of the list
heapPQ.remove(heapPQ.size() - 1); //get rid of element at the end of the list
}
else {
swap(i, heapPQ.size() - 1); //swap the entry with the last entry
heapPQ.remove(heapPQ.size() - 1); //get rid of last entry
downheap(i);
}
}
/**
* Replaces key of the entry
*
* @param entry to replace key of
* @param K new key
*/
@TimeComplexity ("O(log n)")
@Override
public void replaceKey(Entry<K, V> entry, K key) throws IllegalArgumentException {
/*
* TCJ
* This changes a key in the entry, which is a constant time operation
*/
PQEntry<K,V> tempEntry = (PQEntry<K,V>) validateEntry(entry); //validate the entry
if(!(comp.compare(key, key) == 0)) { //validate the key
throw new IllegalArgumentException();
}
tempEntry.setKey(key); //set the new key
reHeap(tempEntry.getIndex()); //restore heap order
}
/**
* Replaces the value of entry
*
* @param value to replace in entry
* @param entry to replace value of
*/
@TimeComplexity ("O(1)")
@Override
public void replaceValue(Entry<K, V> entry, V value) throws IllegalArgumentException {
/*
* TCJ
* This changes a value in the entry, which is a constant time operation
*/
validateEntry(entry);
PQEntry<K,V> tempEntry = (PQEntry<K,V>) (entry); //validate the entry
tempEntry.setValue(value); //set the new value
}
public PQEntry<K,V> validateEntry(Entry<K,V> entry) {
if(!(entry instanceof PQEntry)) { //check if entry is actually an entry
throw new IllegalArgumentException();
}
PQEntry<K,V> tempEntry = (PQEntry<K,V>) (entry);
if(tempEntry.getIndex() > heapPQ.size() -1) { //check if entry is in the list
throw new IllegalArgumentException();
}
return tempEntry;
}
public int parent(int i) {
return ((i-1)/2);
}
public int left(int i) {
return (2*i + 1);
}
public int right(int i) {
return(2*i + 2);
}
public boolean hasLeft(int i) {
return left(i) < heapPQ.size();
}
public boolean hasRight(int i) {
return right(i) < heapPQ.size();
}
public void swap(int i, int j) {
Entry<K,V> tempEntry = heapPQ.get(i);
//swap the index of each entry
((PQEntry<K,V>) heapPQ.get(i)).setIndex(j);
((PQEntry<K,V>) heapPQ.get(j)).setIndex(i);
//swap each entry
heapPQ.set(i, heapPQ.get(j));
heapPQ.set(j, tempEntry);
}
public void reHeap(int i) {
if(i >0 && comp.compare(heapPQ.get(i).getKey(), heapPQ.get(parent(i)).getKey()) < 0) { //if new key is smaller than its parent, upheap, else downheap
upheap(i);
}
else {
downheap(i);
}
}
static class PQEntry<K,V> implements Entry<K,V>{
private K k;
private V v;
private int i;
public PQEntry(K key, V value, int index) {
k = key;
v = value;
i = index;
}
@Override
public K getKey() {
return k;
}
@Override
public V getValue() {
return v;
}
public void setKey(K key) {
k = key;
}
public void setValue(V value) {
v = value;
}
public int getIndex() {
return i;
}
public void setIndex(int i) {
this.i = i;
}
}
}