mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-19 08:59:05 +08:00
24 lines
546 B
JavaScript
24 lines
546 B
JavaScript
import MinHeap from '../heap/MinHeap';
|
|
|
|
// It is the same as min heap except that when comparing to elements
|
|
// we take into account not element's value but rather its priority.
|
|
export default class PriorityQueue extends MinHeap {
|
|
constructor() {
|
|
super();
|
|
this.priorities = {};
|
|
}
|
|
|
|
add(item, priority = 0) {
|
|
this.priorities[item] = priority;
|
|
super.add(item);
|
|
}
|
|
|
|
compare(a, b) {
|
|
if (this.priorities[a] === this.priorities[b]) {
|
|
return 0;
|
|
}
|
|
|
|
return this.priorities[a] < this.priorities[b] ? -1 : 1;
|
|
}
|
|
}
|