forked from yshshrm/Algorithms-And-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMaxPriorityQueue.java
More file actions
142 lines (118 loc) · 3.88 KB
/
Copy pathMaxPriorityQueue.java
File metadata and controls
142 lines (118 loc) · 3.88 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 MaxPriorityQueue<V> {
private static final int DEFAULT_SIZE = 8;
private V[] elements;
private int elementsCount;
private Comparator<V> comparator;
public MaxPriorityQueue() {
this(DEFAULT_SIZE);
}
public MaxPriorityQueue(int length) {
elements = (V[]) new Object[length + 1];
elementsCount = 0;
}
public MaxPriorityQueue(Comparator<V> comparator) {
this(DEFAULT_SIZE, comparator);
}
public MaxPriorityQueue(int length, Comparator<V> comparator) {
this(length);
this.comparator = comparator;
}
public MaxPriorityQueue(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 isMaxHeap();
}
public void enqueue(V value) {
if (elementsCount >= elements.length - 1) {
resize(2 * elements.length);
}
elements[++elementsCount] = value;
swim(elementsCount);
assert isMaxHeap();
}
public V dequeue() {
if (isEmpty()) {
throw new NoSuchElementException();
}
V result = elements[1];
swap(1, elementsCount--);
sink(1);
elements[elementsCount + 1] = null;
assert isMaxHeap();
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 && less(elementIndex / 2, elementIndex)) {
swap(elementIndex, elementIndex / 2);
elementIndex /= 2;
}
}
private void sink(int elementIndex) {
while (2 * elementIndex <= elementsCount) {
int greaterChildIndex = 2 * elementIndex;
if (greaterChildIndex < elementsCount && less(greaterChildIndex, greaterChildIndex + 1)) {
greaterChildIndex++;
}
if (!less(elementIndex, greaterChildIndex)) {
break;
}
swap(elementIndex, greaterChildIndex);
elementIndex = greaterChildIndex;
}
}
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 less(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 isMaxHeap() {
return isMaxHeap(1);
}
private boolean isMaxHeap(int parentIndex) {
if (parentIndex > elementsCount) {
return true;
}
int leftChildIndex = 2 * parentIndex;
int rightChildIndex = 2 * parentIndex + 1;
boolean leftChildGreaterThanParent = leftChildIndex <= elementsCount && less(parentIndex, leftChildIndex);
boolean rightChildGreaterThanParent = rightChildIndex <= elementsCount && less(parentIndex, rightChildIndex);
if (leftChildGreaterThanParent || rightChildGreaterThanParent) {
return false;
}
return isMaxHeap(leftChildIndex) && isMaxHeap(rightChildIndex);
}
}