@jyeontu/structure-jyeontu
Version:
javascript实现各种数据结构:堆、优先队列、字典树、LFU缓存……
31 lines (29 loc) • 700 B
JavaScript
/**
* 优先队列
* @param Array array 用于初始化的数组
* @param Function compareFun 自定义比较函数
* @returns {PriorityQueue}
* */
const { Heap } = require("../Heap/index");
class PriorityQueue {
constructor(array = [], compareFun = null) {
this.oHeap = new Heap(array, compareFun || this.compareFun);
}
get queue() {
return this.oHeap.queue;
}
compareFun(a, b) {
if (a == undefined || b == undefined) return false;
return a.value < b.value;
}
front() {
return this.oHeap.front();
}
pop() {
return this.oHeap.pop();
}
push() {
this.oHeap.push();
}
}
exports.PriorityQueue = PriorityQueue;