JS实现最小堆和最大堆(借助堆实现优先队列)

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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值