优先队列的 java 代码实现

本文介绍了一种使用Java实现的二叉堆数据结构,包括插入、查找最小元素、删除最小元素等基本操作,并提供了完整的代码示例。通过具体实例展示了如何创建二叉堆、进行插入操作以及删除最小元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码

public class BinaryHeap<Type extends Comparable< ? super Type>> {
    private static final int DEFAULT_CAPACITY = 5;

    private int currentSize;
    private Type [] array;

    //将空穴下沉
    private void percolateDown(int hole) {
        int left = hole * 2, right = left + 1, min = left;

        //如果不存在左孩子直接结束
        if ( left > currentSize ) 
            return;
        //初始化min为左孩子,如果存在右孩子而且右孩子的值比左孩子小,min为右孩子
        if ( right <= currentSize && array[left].compareTo(array[right]) > 0 )
            min = right;

        //如果空穴的值比某个孩子的值小,就交换他们,并递归将空穴下沉
        if ( array[hole].compareTo(array[min]) > 0 ) {
            Type t = array[hole];
            array[hole] = array[min];
            array[min] = t;
            percolateDown(min);
        }
    }

    //建堆
    private void buildHeap() {
        for ( int i = currentSize / 2; i > 0; i--)
            percolateDown(i);
    }

    //扩展
    private void enlargeArray( int newSize) {
        Type [] old = array;

        array = (Type []) new Comparable[newSize];
        int i = 0;
        for ( Type items : old ) 
            array[i++] = items;
    }

    //构造方法
    public BinaryHeap( Type [] items) {
        currentSize = items.length;
        array = (Type []) new Comparable[ (currentSize + 2) * 11 / 10 ];

        int i = 1;
        for( Type item : items ) 
            array[i++] = item;
        buildHeap();

        print();
    }

    //插入
    public void insert(Type x) {
        //扩展
        if(currentSize == array.length - 1)
            enlargeArray(array.length * 2);

        //新建一个空穴,每次将空穴与其父比较,如果比其父小,就上浮,直到位置合适
        int hole = ++currentSize;
        for ( ; hole > 1 && x.compareTo(array[ hole / 2 ]) < 0; hole /= 2)
            array[hole] = array[hole/2];

        array[hole] = x;
        print();
    }

    //返回最小值
    public Type findMin() {
        return array[1];
    }

    //弹出最小值
    public Type deleteMin() throws Exception {
        if ( isEmpty()) 
            throw new Exception();

        Type min = findMin();

        //将最后一个值放置到堆首,然后下沉
        array[1] = array[currentSize--];
        percolateDown(1);

        print();

        return min;
    }

    public boolean isEmpty() {
        return currentSize == 0;
    }

    public void makeEmpty() {
        currentSize = 0;
    }

    public void print() {
        for ( int i = 1; i <= currentSize; i++) 
            System.out.print(" " + array[i] + ",");
        System.out.println();

    }

    //测试
    public static void main( String [] args) throws Exception {
        Integer [] array= {6};
        BinaryHeap<Integer> binaryHeap = new BinaryHeap<> (array);

        binaryHeap.insert(0);
        binaryHeap.insert(10);
        binaryHeap.insert(100);
        binaryHeap.insert(15);
        binaryHeap.insert(5);

        binaryHeap.deleteMin();
        binaryHeap.deleteMin();
    }
}

输出

 6,
 0, 6,
 0, 6, 10,
 0, 6, 10, 100,
 0, 6, 10, 100, 15,
 0, 6, 5, 100, 15, 10,
 5, 6, 10, 100, 15,
 6, 15, 10, 100,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CHOOOU

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

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

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

打赏作者

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

抵扣说明:

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

余额充值