-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPriorityQueue.java
292 lines (265 loc) · 8.32 KB
/
PriorityQueue.java
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
package com.queues;
import java.util.Comparator;
/**
* In computer science, a priority queue is an abstract data type which is like
* a regular queue or stack data structure, but where additionally each element
* has a "priority" associated with it. In a priority queue, an element with
* high priority is served before an element with low priority. If two elements
* have the same priority, they are served according to their order in the
* queue.
*
* {@link https://en.wikipedia.org/wiki/Priority_queue}
*
* The elements of the priority queue are ordered according to their natural
* ordering, or by a Comparator provided at queue construction time, depending
* on which constructor is used. A priority queue does not permit null elements.
* A priority queue relying on natural ordering also does not permit insertion
* of non-comparable objects (doing so may result in ClassCastException).
*
* {@link http://docs.oracle.com/javase/8/docs/api/java/util/PriorityQueue.html}
*
* @author STEPHANE MIGUEL KAKANAKOU (Skakanakou@gmail.com)
*
* @param <Item>
*/
public class PriorityQueue<Item extends Comparable<Item>> {
/**
* Contains the content of the PriorityQueue. For simplicity reason, the
* contents array is 1 based array. So when a parent Item is at index k, its
* children are at index 2*k and 2*k + 1.
*/
private Item[] contents;
/*** Contains the size of the PriorityQueue. */
private int pqSize;
/***
* Store the provided Comparator for the construction of the PriorityQueue.
*/
private Comparator<Item> comparator;
/*** Contains the capacity of the PriorityQueue. */
private int capacity;
/*** Contains the initial capacity of an empty PriorityQueue. */
private static final int INITIAL_CAPACITY = 16;
/**
* Constructs an empty priorityQueue that use the natural order and with an
* initial capacity of 16.
*/
@SuppressWarnings("unchecked")
public PriorityQueue() {
pqSize = 0;
contents = (Item[]) new Comparable[INITIAL_CAPACITY];
comparator = null;
capacity = INITIAL_CAPACITY;
}
/**
* Constructs an empty priorityQueue that use the given Comparator and with
* an initial capacity of 16.
*
* @param com
* The Comparator that the priorityQueue will use
*/
@SuppressWarnings("unchecked")
public PriorityQueue(Comparator<Item> com) {
pqSize = 0;
contents = (Item[]) new Comparable[INITIAL_CAPACITY];
comparator = com;
capacity = INITIAL_CAPACITY;
}
/**
* Constructs a priorityQueue that use the natural order and fill him with
* the given items.
*
* @param items
* array of Items to put in the priorityQueue.
*
* @exception java.lang.NullPointerException
* if items is null
*/
@SuppressWarnings("unchecked")
public PriorityQueue(Item[] items) {
if (items == null) {
throw new NullPointerException("Items is null");
}
pqSize = items.length;
capacity = ((pqSize + 1) / INITIAL_CAPACITY) * INITIAL_CAPACITY;
if ((pqSize + 1) % INITIAL_CAPACITY != 0) {
capacity += INITIAL_CAPACITY;
}
contents = (Item[]) new Comparable[capacity];
for (int i = 0; i < pqSize; i++) {
contents[i + 1] = items[i];
}
heapify();
}
/**
* Constructs a priorityQueue that use the given Comparator and fill him with
* the given items.
*
* @param items
* array of Items to put in the priorityQueue.
*
* @exception java.lang.NullPointerException
* if items is null
*/
@SuppressWarnings("unchecked")
public PriorityQueue(Item[] items, Comparator<Item> com) {
if (items == null) {
throw new NullPointerException("Items is null");
}
pqSize = items.length;
capacity = ((pqSize + 1) / INITIAL_CAPACITY) * INITIAL_CAPACITY;
if ((pqSize + 1) % INITIAL_CAPACITY != 0) {
capacity += INITIAL_CAPACITY;
}
contents = (Item[]) new Comparable[capacity];
for (int i = 0; i < pqSize; i++) {
contents[i + 1] = items[i];
}
comparator = com;
heapify();
}
/**
* Add the specified element in the priorityQueue.
*
* @param item
* the element to add
*
* @exception java.lang.NullPointerException
* if item is null
*/
public void add(Item item) {
if(item == null)
throw new NullPointerException("Null Item");
if (pqSize+1 >= capacity) {
resize(2 * capacity);
}
contents[++pqSize] = item;
swim(pqSize);
}
/**
* Retrieves and removes the item at the top of this priorityQueue.
*
* @return the top of this priorityQueue
*
* @exception java.lang.IllegalStateException
* if the priorityQueue is empty
*/
public Item poll() {
if (isEmpty()) {
throw new IllegalStateException("The PriorityQueue is empty");
}
if ((capacity > INITIAL_CAPACITY) && (pqSize <= capacity / 4)) {
this.resize(this.capacity / 2);
}
Item top = contents[1];
exchKeys(1, pqSize--);
sink(1);
contents[pqSize + 1] = null;
return top;
}
/**
* Retrieves but not removes the item at the top of this priorityQueue.
*
* @return the top of this priorityQueue
*
* @exception java.lang.IllegalStateException
* if the priorityQueue is empty
*/
public Item peek() {
if (isEmpty())
throw new IllegalStateException("The queue is empty");
return contents[1];
}
/**
* Tests if this priorityQueue is empty.
*
* @return true if the priorityQueue is empty and false if not.
*/
public boolean isEmpty() {
if (pqSize == 0)
return true;
return false;
}
/**
* Removes all of the elements from this priorityQueue.
*/
@SuppressWarnings("unchecked")
public void clear() {
pqSize = 0;
contents = (Item[]) new Comparable[INITIAL_CAPACITY];
capacity = INITIAL_CAPACITY;
}
/**
* Returns the number of elements in this priorityQueue.
*
* @return size the number of elements in this priorityQueue.
*/
public int size() {
return pqSize;
}
/*
* Convert the array contents into a into a Heap data structure.
*/
private void heapify() {
for (int k = pqSize / 2; k >= 1; k--) {
sink(k);
}
}
/*
* Restore the Heap logic when a parent is greater than one or both its children.
*/
private void sink(int k) {
int j;
while (k <= pqSize / 2) {
j = 2 * k;
if ((j + 1 <= pqSize) && isLess(j + 1, j, comparator))
j++;
if (isLess(k, j, comparator))
break;
exchKeys(k, j);
k = j;
}
}
/*
* Restore the Heap logic when a child is greater than his parent.
*/
private void swim(int k) {
while ((k > 1) && isLess(k, k / 2, comparator)) {
exchKeys(k, k / 2);
k = k / 2;
}
}
/*
* check is A[i] is less than A[j] ?
*/
private boolean isLess(int k, int j, Comparator<Item> com) {
int compareValue;
if (com == null)
compareValue = contents[k].compareTo(contents[j]);
else
compareValue = com.compare(contents[k], contents[j]);
if (compareValue < 0)
return true;
return false;
}
/*
* Swap the item at positions i and j.
*/
private void exchKeys(int i, int j) {
Item item = contents[i];
contents[i] = contents[j];
contents[j] = item;
}
/*
* Resize the array.
*/
@SuppressWarnings("unchecked")
private void resize(int newCapacity) {
Item[] newContents = (Item[]) new Comparable[newCapacity];
for (int i = 1; i <= pqSize; i++) {
newContents[i] = contents[i];
}
contents = newContents;
newContents = null;
this.capacity = newCapacity;
}
}