class MinHeap {
/**
*
* @param {Array<Number>} data
*/
constructor() {
this.data = [null];
}
/**
* 返回堆大小
* @returns Number
*/
get size() {
return this.data.length - 1;
}
/**
* 返回队列中最小值
*/
get min() {
return this.data[1];
}
/**
* 交换两个位置的值
* @param {Number} i
* @param {Number} j
*/
_swap(i, j) {
[this.data[i], this.data[j]] = [this.data[j], this.data[i]];
}
/**
* 先把要插入的元素添加到堆底的最后,然后让其上浮到正确位置
* @param {any} node
*/
insert(node) {
this.data.push(node);
this.
JavaScript实现最小堆
最新推荐文章于 2024-04-18 02:02:06 发布