用堆实现优先级队列--Java

来源:互联网 发布:华林证券软件 编辑:程序博客网 时间:2024/06/07 16:28

堆实现优先级队列!!堆好有用的呢

最大优先级队列包含四个操作: 

heapMaximum()返回队列中最大的元素; 

heapExtractMax()返回队列中最大的元素并从队列中删除此元素; 

heapIncreaseKey(int i, T key)增加队列中下标为i的元素的值为key; 

maxHeapInsert(T key)向队列中插入元素key。 
接下来分别介绍如何实现这四个操作。 


首先给出最大优先级队列实现类中的域及构造函数:


public class MaxPriorityQueue<T extends Comparable<T>> {    private T[] heap;    private int heapLength;    // 用于提供堆的操作    private MaxHeap<T> maxHeap = new MaxHeap<>();    public MaxPriorityQueue(T[] a, int heapLength) {        super();        maxHeap.buildHeap(a, heapLength);        this.heap = a;        this.heapLength = heapLength;    }}

(1)heapMaximum() 
返回队列中的最大元素是很简单的,因为最大优先队列是基于大根堆实现的

所以只需要返回数组的第一个元素即可。 


public T heapMaximum() {        return this.heap[0];    }


(2)heapExtractMax() 
不仅需要返回队列中最大的元素,还需要删除该元素,要做到这一点,首先保存数组的第一个元素,然后把数组的最后一个元素放到数组的第一个位置,堆的长度减1,对堆的第一个元素调用大根堆的heapify方法,使第一个元素满足大根堆的性质。 


 public T heapExtractMax() {        if (this.heapLength < 1) {            return null;        }        T max = heap[0];        heap[0] = heap[heapLength - 1];        heapLength--;        maxHeap.heapify(this.heap, 0, heapLength);        return max;    }


(3)heapIncreaseKey(int i, T key) 
把数组中下标为i的元素的值设为key,key必须大于等于该处原来的值,该结点的值发生变化后可能破坏大根堆的性质,所以需要上移该处的值,保持大根堆性质。 


 public void heapIncreaseKey(int i, T key) {        if (key.compareTo(this.heap[i]) < 0) {            try {                throw new Exception("the key is less than heap[i]");            } catch (Exception e) {                e.printStackTrace();                return;            }        }        this.heap[i] = key;        /**         * 向上移动heap[i]的位置;         * 移动的条件是heap[i]不是根节点,并且heap[i]比双亲结点大         */        while(i > 0 && heap[i].compareTo(this.heap[maxHeap.parent(i)]) > 0){            T temp = this.heap[i];            this.heap[i] = this.heap[maxHeap.parent(i)];            this.heap[maxHeap.parent(i)] = temp;            i = maxHeap.parent(i);        }    }


(4)maxHeapInsert(T key) 
向队列中插入元素key,首先,堆的长度增加1,然后把key放在堆的最后,对这个元素调用heapIncreaseKey(int i, T key)方法,使之满足堆的性质即可。


public void maxHeapInsert(T key) {        this.heapLength++;        // 如果保存堆的数组已经被填满        if (this.heapLength == this.heap.length) {            // 新建一个更大的数组,用于保存旧数组中的元素            @SuppressWarnings("unchecked")            T[] temp = (T[]) Array.newInstance(this.heap.getClass().getComponentType(),                    2 * this.heapLength);            // 把旧数组中的元素复制进新数组中            for(int i = 1; i < this.heapLength; i++){                temp[i] = this.heap[i];            }            this.heap = temp;        }        this.heap[heapLength] = key;        this.heapIncreaseKey(heapLength, key);    }


最大优先级队列和最小优先级队列的完整可以在下面的github地址处找到: 
https://github.com/l294265421/datastructure-common


0 0
原创粉丝点击