-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityQueue.java
41 lines (32 loc) · 908 Bytes
/
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
package nonlinear;
import java.util.NoSuchElementException;
public class PriorityQueue {
private final BinaryHeap heap;
public PriorityQueue(int capacity) {
heap = new BinaryHeap(capacity);
}
public PriorityQueue(int capacity, boolean isMinPriority) {
heap = new BinaryHeap(capacity, isMinPriority);
}
public void enqueue(int element) {
heap.insert(element);
}
public int dequeue() {
if (!isEmpty()) {
return heap.extractMinOrMax();
}
throw new NoSuchElementException("Priority queue is empty");
}
public int peek() {
if (!isEmpty()) {
return heap.getMinOrMax();
}
throw new NoSuchElementException("Priority queue is empty");
}
public int size() {
return heap.getSize();
}
public boolean isEmpty() {
return heap.isEmpty();
}
}