-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval_heap.js
273 lines (244 loc) · 6.52 KB
/
interval_heap.js
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
/**
* An implementation of an implicit binary interval heap.
*/
const DEFAULT_CAPACITY = 64;
class IntervalHeap {
constructor(capacity) {
this.data = new Array(capacity);
this.keys = new Array(capacity);
this.capacity = capacity || DEFAULT_CAPACITY;
this.size = 0;
}
offer(key, value) {
// If move room is needed, double array size
if (this.size >= this.capacity) {
this.capacity *= 2;
this.data.length = this.capacity;
this.keys.length = this.capacity;
}
// Insert new value at the end
this.size++;
this.data[this.size - 1] = value;
this.keys[this.size - 1] = key;
this.siftInsertedValueUp();
}
removeMin() {
if (this.size === 0)
throw new IllegalStateException();
this.size--;
this.data[0] = this.data[this.size];
this.keys[0] = this.keys[this.size];
this.data[this.size] = null;
this.siftDownMin(0);
}
replaceMin(key, value) {
if (this.size === 0)
throw new IllegalStateException();
this.data[0] = value;
this.keys[0] = key;
if (this.size > 1) {
// Swap with pair if necessary
if (this.keys[1] < key)
this.swap(0, 1);
this.siftDownMin(0);
}
}
removeMax() {
if (this.size === 0)
throw new IllegalStateException();
else if (this.size === 1) {
this.removeMin();
return;
}
this.size--;
this.data[1] = this.data[this.size];
this.keys[1] = this.keys[this.size];
this.data[this.size] = null;
this.siftDownMax(1);
}
replaceMax(key, value) {
if (this.size === 0)
throw new IllegalStateException();
else if (this.size === 1) {
this.replaceMin(key, value);
return;
}
this.data[1] = value;
this.keys[1] = key;
// Swap with pair if necessary
if (key < this.keys[0])
this.swap(0, 1);
this.siftDownMax(1);
}
getMin() {
if (this.size === 0)
throw new IllegalStateException();
return this.data[0];
}
getMax() {
if (this.size === 0)
throw new IllegalStateException();
else if (this.size === 1)
return this.data[0];
return this.data[1];
}
getMinKey() {
if (this.size === 0)
throw new IllegalStateException();
return this.keys[0];
}
getMaxKey() {
if (this.size === 0)
throw new IllegalStateException();
else if (this.size === 1)
return this.keys[0];
return this.keys[1];
}
swap(x, y) {
var yData = this.data[y];
var yDist = this.keys[y];
this.data[y] = this.data[x];
this.keys[y] = this.keys[x];
this.data[x] = yData;
this.keys[x] = yDist;
return y;
}
/**
* Min-side (u % 2 === 0):
* - leftchild: 2u + 2
* - rightchild: 2u + 4
* - parent: (x/2-1)&~1
*
* Max-side (u % 2 === 1):
* - leftchild: 2u + 1
* - rightchild: 2u + 3
* - parent: (x/2-1)|1
*/
siftInsertedValueUp() {
var u = this.size - 1;
if (u === 0)
; // Do nothing if it's the only element!
else if (u === 1) {
// If it is the second element, just sort it with it's pair
if (this.keys[u] < this.keys[u - 1]) { // If less than it's pair
this.swap(u, u - 1); // Swap with it's pair
}
} else if (u % 2 === 1) {
// Already paired. Ensure pair is ordered right
var p = (u / 2 - 1) | 1; // The larger value of the parent pair
if (this.keys[u] < this.keys[u - 1]) { // If less than it's pair
u = this.swap(u, u - 1); // Swap with it's pair
if (this.keys[u] < this.keys[p - 1]) { // If smaller than smaller parent pair
// Swap into min-heap side
u = this.swap(u, p - 1);
this.siftUpMin(u);
}
} else {
if (this.keys[u] > this.keys[p]) { // If larger that larger parent pair
// Swap into max-heap side
u = this.swap(u, p);
this.siftUpMax(u);
}
}
} else {
// Inserted in the lower-value slot without a partner
var p = (u / 2 - 1) | 1; // The larger value of the parent pair
if (this.keys[u] > this.keys[p]) { // If larger that larger parent pair
// Swap into max-heap side
u = this.swap(u, p);
this.siftUpMax(u);
} else if (this.keys[u] < this.keys[p - 1]) { // If smaller than smaller parent pair
// Swap into min-heap side
u = this.swap(u, p - 1);
this.siftUpMin(u);
}
}
}
siftUpMin(c) {
// Min-side parent: (x/2-1)&~1
for (var p = (c / 2 - 1) & ~1; p >= 0 && this.keys[c] < this.keys[p]; c = p, p = (c / 2 - 1) & ~1) {
this.swap(c, p);
}
}
siftUpMax(c) {
// Max-side parent: (x/2-1)|1
for (var p = (c / 2 - 1) | 1; p >= 0 && this.keys[c] > this.keys[p]; c = p, p = (c / 2 - 1) | 1) {
this.swap(c, p);
}
}
siftDownMin(p) {
for (var c = p * 2 + 2; c < this.size; p = c, c = p * 2 + 2) {
if (c + 2 < this.size && this.keys[c + 2] < this.keys[c])
c += 2;
if (this.keys[c] < this.keys[p]) {
this.swap(p, c);
// Swap with pair if necessary
if (c + 1 < this.size && this.keys[c + 1] < this.keys[c])
this.swap(c, c + 1);
} else {
break;
}
}
}
siftDownMax(p) {
for (var c = p * 2 + 1; c <= this.size; p = c, c = p * 2 + 1) {
if (c === this.size) {
// If the left child only has half a pair
if (this.keys[c - 1] > this.keys[p])
this.swap(p, c - 1);
break;
} else if (c + 2 === this.size) {
// If there is only room for a right child lower pair
if (this.keys[c + 1] > this.keys[c]) {
if (this.keys[c + 1] > this.keys[p])
this.swap(p, c + 1);
break;
}
} else if (c + 2 < this.size) {
// If there is room for a right child upper pair
if (this.keys[c + 2] > this.keys[c])
c += 2;
}
if (this.keys[c] > this.keys[p]) {
this.swap(p, c);
// Swap with pair if necessary
if (this.keys[c - 1] > this.keys[c])
this.swap(c, c - 1);
} else {
break;
}
}
}
toString() {
// java.text.DecimalFormat twoPlaces = new java.text.DecimalFormat("0.00");
// StringBuffer str = new StringBuffer(IntervalHeap.class.getCanonicalName());
// str.append(", this.size: ").append(this.size()).append(" this.capacity: ").append(this.capacity());
// var i = 0, p = 2;
// while (i < this.size()) {
// var x = 0;
// str.append("\t");
// while ((i + x) < this.size() && x < p) {
// str.append(twoPlaces.format(this.keys[i + x])).append(", ");
// x++;
// }
// str.append("\n");
// i += x;
// p *= 2;
// }
// return str.toString();
}
validateHeap() {
// Validate left-right
for (var i = 0; i < this.size - 1; i += 2) {
if (this.keys[i] > this.keys[i + 1]) return false;
}
// Validate within parent interval
for (var i = 2; i < this.size; i++) {
var maxParent = this.keys[(i / 2 - 1) | 1];
var minParent = this.keys[(i / 2 - 1) & ~1];
if (this.keys[i] > maxParent || this.keys[i] < minParent) return false;
}
return true;
}
}
module.exports = IntervalHeap;