-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.js
More file actions
39 lines (34 loc) · 822 Bytes
/
Copy pathPriorityQueue.js
File metadata and controls
39 lines (34 loc) · 822 Bytes
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
class PriorityQueue {
constructor(compare, array) {
this.compare = compare;
this.queue = [];
this.length = 0;
if (array) {
for (const val of array) {
this.push(val);
}
}
}
push(val) {
// const i = this.queue.findIndex((v) => this.compare(val, v));
const i = this.queue.findIndex((v) => this.compare(v, val));
if (-1 == i) {
this.queue.push(val);
}
else {
this.queue.splice(i, 0, val);
}
this.length++;
}
top() {
return this.queue[this.queue.length - 1];
}
pop() {
this.length--;
return this.queue.pop();
}
isEmpty() {
return this.length == 0;
}
}
exports.PriorityQueue = PriorityQueue;