自定义实现优先级队列(堆)

1 篇文章 0 订阅

基于数组自定义实现一个最大堆,包括入堆,出堆,弹出堆顶元素,打印整个堆功能。

public class MyHeap {
    private int[] elem;
    private int usedSize=0;
    //  初始化一个k大小的数组
    public MyHeap(int k){
        this.elem = new int[k];
    }

    //入堆
    public boolean offer(int val){
        if(isFull()){
            throw new RuntimeException("堆为满");
        }
        this.elem[this.usedSize] = val;
        int child = this.usedSize;
        adjustUp(child);
        this.usedSize++;
        return true;
    }
    //出堆
    public int poll(){
        if (isEmpty()){
            throw new RuntimeException("堆为空");
        }
        int tmp = this.elem[0];
        swap(0,this.usedSize-1);
        this.usedSize--;
        adjustDown(0,this.usedSize);
        return tmp;
    }
    //弹出堆顶元素
    public int peek(){
        if (isEmpty()){
            throw new RuntimeException("堆为空");
        }
        return this.elem[0];
    }
    //打印堆中元素
    public String show(){
        String str = "";
        for (int x: this.elem) {
            str += x+" ";
        }
        return str;
    }
    //判断堆是否为空
    public boolean isEmpty(){
        return this.usedSize==0;
    }
    //判断队列是否为满
    public boolean isFull(){
        return this.usedSize==this.elem.length;
    }
    //向上调整
    private void adjustUp(int child){
        int parent = (child-1)/2;
        while (child>0){
            if (this.elem[child]>this.elem[parent]){
                swap(child,parent);
                child = parent;
                parent = (child-1)/2;
            }else{
                break;
            }
        }
    }
    //向下调整
    private void adjustDown(int parent,int len){
        int child = 2*parent+1;
        while (child<len){
            if (child+1<len && this.elem[child]<this.elem[child+1]){
                child++;
            }
            if (this.elem[child] > this.elem[parent]){
                swap(child,parent);
                parent = child;
                child = 2*parent+1;
            }else{
                break;
            }
        }
    }
    //交换
    private void swap(int child,int parent){
        int tmp = this.elem[child];
        this.elem[child] = this.elem[parent];
        this.elem[parent] = tmp;
    }
}

我们来看看测试:

public class TestDemo {
    public static void main(String[] args) {
        MyHeap heap = new MyHeap(7);
        heap.offer(1);
        heap.offer(55);
        heap.offer(16);
        heap.offer(34);
        heap.offer(78);
        heap.offer(36);
        heap.offer(7);
        System.out.println(heap.peek());
        System.out.println(heap.show());
        heap.poll();
        heap.poll();
        heap.poll();
        heap.poll();
        heap.poll();
        heap.poll();
        heap.poll();
        heap.poll();

    }
}

在这里插入图片描述
符合预期结果。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

宋丹尼尔

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

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

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

打赏作者

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

抵扣说明:

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

余额充值