Java使用小堆模拟实现简单的优先队列

今天简单的使用堆模拟实现一下优先队列。

关于优先队列和堆的相关知识可以去:
优先队列PriorityQueue中的方法和使用细节
堆 超详细的带图总结

代码实现:

package PriorityQueue;

//小堆模拟实现优先队列
// 堆的定义:要求每个位置都比它的两个孩子(如果存在的话)要小

public class MyPriorityQueue {
    // 元素类型使用 long 类型
    // 暂时不考虑扩容的情况
    private final long[] array = new long[1000];
    private int size;       // 元素的个数

    public MyPriorityQueue() {
        //空的优先级队列
        size = 0;
    }

    // 查看优先级队列中最小值
    // O(1)
    public long peek() {
        if (size <= 0) {
            throw new RuntimeException("该堆为空!!!");
        }
        return array[0];
    }

    // 把 e 添加到堆中(优先级队列)
    // 同时维护好最小堆的性质
    // 最坏情况:从叶子 -> 根 时间复杂度即堆的深度
    // O(log(n))
    public void offer(long e) {
        array[size++] = e;

        int index = size - 1;
        // index == 0 说明是根,不需要调整了
        while (index > 0) {
            // 求双亲的下标
            int pIdx = (index - 1) / 2;
            // 比较要调整的位置和双亲的大小
            if (array[pIdx] <= array[index]) {
                break;
            }
            swap(array, pIdx, index);
            index = pIdx;
        }
    }

    // 删除堆顶元素
    // 同时维护好最小堆的性质
    // O(log(n))
    public long poll() {
        if (size <= 0) {
            throw new RuntimeException("该堆为空!!!");
        }
        long e = array[0];
        size--;
        array[0] = array[size];
        adjustDown(0);
        return e;
    }

    public void adjustDown(int index) {
        //2 * index + 1 < size 说明不是叶子节点
        while (2 * index + 1 < size) {
            int minIdx = 2 * index + 1;
            int rightIdx = minIdx + 1;
            if (rightIdx < size && array[minIdx] > array[rightIdx]) {
                minIdx = rightIdx;
            }
            if (array[minIdx] >= array[index]) {
                return;
            }
            swap(array, minIdx, index);
            index = minIdx;
        }
    }

    public void swap(long[] array, int i, int j) {
        long tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    //验证
    public static void main(String[] args) {
        MyPriorityQueue q = new MyPriorityQueue();

        q.offer(3);
        q.offer(9);
        q.offer(7);
        q.offer(2);
        q.offer(6);
        q.offer(8);
        q.offer(5);
        q.offer(4);
        q.offer(3);

        System.out.println(q.poll());
        q.offer(1);
        System.out.println(q.poll());
        q.offer(1);
        q.offer(0);
        System.out.println(q.poll());
        System.out.println(q.poll());
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木木是木木

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值