JS实现最小堆
class MinHeap {
constructor() {
this.heap = [];
}
// 获取堆的大小
size() {
return this.heap.length;
}
// 检查堆是否为空
isEmpty() {
return this.size() === 0;
}
// 插入新值
push(val) {
this.heap.push(val);
this._bubbleUp(this.size() - 1);
}
// 删除并返回堆顶元素
pop() {
if (this.size() === 1) return this.heap.pop();
const top = this.heap[0];
this.heap[0] = this.heap.pop();
this._bubbleDown(0);
return top;
}
// 获取堆顶元素
peek() {
return this.heap[0];
}
// 上浮操作
_bubbleUp(index) {
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.heap[index] >= this.heap[parentIndex]) break;
[this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]];
index = parentIndex;
}
}
// 下沉操作
_bubbleDown(index) {
const length = this.size();
while (true) {
const leftIndex = 2 * index + 1;
const rightIndex = 2 * index + 2;
let nextIndex = index;
if (leftIndex < length && this.heap[leftIndex] < this.heap[nextIndex]) {
nextIndex = leftIndex;
}
if (rightIndex < length && this.heap[rightIndex] < this.heap[nextIndex]) {
nextIndex = rightIndex;
}
if (nextIndex === index) break;
[this.heap[index], this.heap[nextIndex]] = [this.heap[nextIndex], this.heap[index]];
index = nextIndex;
}
}
}
JS实现最大堆
class MaxHeap {
constructor() {
this.heap = [];
}
// 获取堆的大小
size() {
return this.heap.length;
}
// 检查堆是否为空
isEmpty() {
return this.size() === 0;
}
// 插入新值
push(val) {
this.heap.push(val);
this._bubbleUp(this.size() - 1);
}
// 删除并返回堆顶元素
pop() {
if (this.size() === 1) return this.heap.pop();
const top = this.heap[0];
this.heap[0] = this.heap.pop();
this._bubbleDown(0);
return top;
}
// 获取堆顶元素
peek() {
return this.heap[0];
}
// 上浮操作
_bubbleUp(index) {
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.heap[index] <= this.heap[parentIndex]) break;
[this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]];
index = parentIndex;
}
}
// 下沉操作
_bubbleDown(index) {
const length = this.size();
while (true) {
const leftIndex = 2 * index + 1;
const rightIndex = 2 * index + 2;
let nextIndex = index;
if (leftIndex < length && this.heap[leftIndex] > this.heap[nextIndex]) {
nextIndex = leftIndex;
}
if (rightIndex < length && this.heap[rightIndex] > this.heap[nextIndex]) {
nextIndex = rightIndex;
}
if (nextIndex === index) break;
[this.heap[index], this.heap[nextIndex]] = [this.heap[nextIndex], this.heap[index]];
index = nextIndex;
}
}
}
示例用法
// 使用最小堆
const minHeap = new MinHeap();
minHeap.push(5);
minHeap.push(3);
minHeap.push(8);
console.log(minHeap.pop()); // 输出 3
console.log(minHeap.peek()); // 输出 5
// 使用最大堆
const maxHeap = new MaxHeap();
maxHeap.push(5);
maxHeap.push(3);
maxHeap.push(8);
console.log(maxHeap.pop()); // 输出 8
console.log(maxHeap.peek()); // 输出 5
JS实现通用堆
class Heap {
constructor(compare) {
this.compare = compare;
this.heap = [];
}
// 获取堆的大小
size() {
return this.heap.length;
}
// 检查堆是否为空
isEmpty() {
return this.size() === 0;
}
// 插入新值
push(val) {
this.heap.push(val);
this._bubbleUp(this.size() - 1);
}
// 删除并返回堆顶元素
pop() {
if (this.size() === 1) return this.heap.pop();
const top = this.heap[0];
this.heap[0] = this.heap.pop();
this._bubbleDown(0);
return top;
}
// 获取堆顶元素
peek() {
return this.heap[0];
}
// 上浮操作
_bubbleUp(index) {
while (index > 0) {
const parentIndex = Math.floor((index - 1) / 2);
if (this.compare(this.heap[index], this.heap[parentIndex]) >= 0) break;
[this.heap[index], this.heap[parentIndex]] = [this.heap[parentIndex], this.heap[index]];
index = parentIndex;
}
}
// 下沉操作
_bubbleDown(index) {
const length = this.size();
while (true) {
const leftIndex = 2 * index + 1;
const rightIndex = 2 * index + 2;
let nextIndex = index;
if (leftIndex < length && this.compare(this.heap[leftIndex], this.heap[nextIndex]) < 0) {
nextIndex = leftIndex;
}
if (rightIndex < length && this.compare(this.heap[rightIndex], this.heap[nextIndex]) < 0) {
nextIndex = rightIndex;
}
if (nextIndex === index) break;
[this.heap[index], this.heap[nextIndex]] = [this.heap[nextIndex], this.heap[index]];
index = nextIndex;
}
}
}
// 最小堆的比较函数
const minHeapCompare = (a, b) => a - b;
// 最大堆的比较函数
const maxHeapCompare = (a, b) => b - a;
// 示例用法
const minHeap = new Heap(minHeapCompare);
minHeap.push(5);
minHeap.push(3);
minHeap.push(8);
console.log(minHeap.pop()); // 输出 3
console.log(minHeap.peek()); // 输出 5
const maxHeap = new Heap(maxHeapCompare);
maxHeap.push(5);
maxHeap.push(3);
maxHeap.push(8);
console.log(maxHeap.pop()); // 输出 8
console.log(maxHeap.peek()); // 输出 5
借助通用堆实现优先队列
class PriorityQueue {
constructor(compare) {
this.heap = new Heap(compare);
}
// 插入新元素
enqueue(val) {
this.heap.push(val);
}
// 删除并返回优先级最高的元素
dequeue() {
return this.heap.pop();
}
// 返回优先级最高的元素
peek() {
return this.heap.peek();
}
// 返回优先队列的大小
size() {
return this.heap.size();
}
// 检查优先队列是否为空
isEmpty() {
return this.heap.isEmpty();
}
}
/ 最小堆的比较函数
const minHeapCompare = (a, b) => a - b;
// 最大堆的比较函数
const maxHeapCompare = (a, b) => b - a;
// 创建最小优先队列
const minPriorityQueue = new PriorityQueue(minHeapCompare);
minPriorityQueue.enqueue(5);
minPriorityQueue.enqueue(3);
minPriorityQueue.enqueue(8);
console.log(minPriorityQueue.dequeue()); // 输出 3
console.log(minPriorityQueue.peek()); // 输出 5
// 创建最大优先队列
const maxPriorityQueue = new PriorityQueue(maxHeapCompare);
maxPriorityQueue.enqueue(5);
maxPriorityQueue.enqueue(3);
maxPriorityQueue.enqueue(8);
console.log(maxPriorityQueue.dequeue()); // 输出 8
console.log(maxPriorityQueue.peek()); // 输出 5