优先队列的 java 代码实现

代码

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,

猜你喜欢

转载自blog.csdn.net/one_of_a_kind/article/details/75799076