forked from yshshrm/Algorithms-And-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMinPriorityQueue.java
More file actions
142 lines (118 loc) · 3.89 KB
/
Copy pathMinPriorityQueue.java
File metadata and controls
142 lines (118 loc) · 3.89 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
import java.util.Comparator;
import java.util.NoSuchElementException;
public class MinPriorityQueue<V> {
private static final int DEFAULT_SIZE = 8;
private V[] elements;
private int elementsCount;
private Comparator<V> comparator;
public MinPriorityQueue() {
this(DEFAULT_SIZE);
}
public MinPriorityQueue(int length) {
elements = (V[]) new Object[length + 1];
elementsCount = 0;
}
public MinPriorityQueue(Comparator<V> comparator) {
this(DEFAULT_SIZE, comparator);
}
public MinPriorityQueue(int length, Comparator<V> comparator) {
this(length);
this.comparator = comparator;
}
public MinPriorityQueue(V[] elements) {
this(elements.length);
elementsCount = elements.length;
for (int i = 0; i < elementsCount; i++) {
this.elements[i + 1] = elements[i];
}
for (int sinkElementIndex = elementsCount / 2; sinkElementIndex >= 1; sinkElementIndex--) {
sink(sinkElementIndex);
}
assert isMinHeap();
}
public void enqueue(V value) {
if (elementsCount >= elements.length - 1) {
resize(2 * elements.length);
}
elements[++elementsCount] = value;
swim(elementsCount);
assert isMinHeap();
}
public V dequeue() {
if (isEmpty()) {
throw new NoSuchElementException();
}
V result = elements[1];
swap(1, elementsCount--);
sink(1);
elements[elementsCount + 1] = null;
assert isMinHeap();
return result;
}
public V peek() {
if (isEmpty()) {
throw new NoSuchElementException();
}
return elements[1];
}
public int size() {
return elementsCount;
}
public boolean isEmpty() {
return elementsCount == 0;
}
private void swim(int elementIndex) {
while (elementIndex > 1 && greater(elementIndex / 2, elementIndex)) {
swap(elementIndex, elementIndex / 2);
elementIndex /= 2;
}
}
private void sink(int elementIndex) {
while (2 * elementIndex <= elementsCount) {
int smallerChildIndex = 2 * elementIndex;
if (smallerChildIndex < elementsCount && greater(smallerChildIndex, smallerChildIndex + 1)) {
smallerChildIndex++;
}
if (!greater(elementIndex, smallerChildIndex)) {
break;
}
swap(elementIndex, smallerChildIndex);
elementIndex = smallerChildIndex;
}
}
private void resize(int newLength) {
V[] newElements = (V[]) new Object[newLength];
for (int i = 1; i <= elementsCount; i++) {
newElements[i] = elements[i];
}
elements = newElements;
}
private boolean greater(int i, int j) {
if (comparator == null) {
return ((Comparable<V>) elements[i]).compareTo(elements[j]) > 0;
} else {
return comparator.compare(elements[i], elements[j]) > 0;
}
}
private void swap(int i, int j) {
V swap = elements[i];
elements[i] = elements[j];
elements[j] = swap;
}
private boolean isMinHeap() {
return isMinHeap(1);
}
private boolean isMinHeap(int parentIndex) {
if (parentIndex > elementsCount) {
return true;
}
int leftChildIndex = 2 * parentIndex;
int rightChildIndex = 2 * parentIndex + 1;
boolean leftChildLessThanParent = leftChildIndex <= elementsCount && greater(parentIndex, leftChildIndex);
boolean rightChildLessThanParent = rightChildIndex <= elementsCount && greater(parentIndex, rightChildIndex);
if (leftChildLessThanParent || rightChildLessThanParent) {
return false;
}
return isMinHeap(leftChildIndex) && isMinHeap(rightChildIndex);
}
}