基于java堆的优先级队列实现

jei2mxaa  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(207)

我已经做了一个使用堆的优先级队列实现,但是它在某种程度上是有效的,但是我遇到了一个问题,如果我将优先级队列出列,它就不能正确地堆化。例如,如果我从0到9的数组中输入10个元素,因为这是一个maxheap实现,所以它应该总是返回最高的值,然后heapify来更正树,但是它没有。
当我出列时得到9,当我再次出列时没有得到8,而是得到0,因为它交换了上一个索引,但它没有修复树。我试过调试和单元测试,但我根本找不到问题所在。我想知道是否有新的眼睛可以看到我看不到的东西,提前谢谢!

import java.lang.reflect.Array;

public class HeapPriorityQueue<T extends Comparable<? super T>> implements PriorityQueue<T> {

    private Class<T> clazz;
    private int lastIndex, capacity;
    private T heap[];

    public HeapPriorityQueue(Class<T> clazz, int capacity) {
        this.clazz = clazz;
        this.capacity = capacity;
        this.heap = (T[]) Array.newInstance(clazz, capacity);
        this.lastIndex = -1;
    }

    @Override
    public void clear() {
        this.lastIndex = -1;
        this.heap = (T[]) Array.newInstance(clazz, capacity);
        System.out.println("The queue has been destroyed!");
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return lastIndex == -1;
    }

    @Override
    public boolean isFull() {
        // TODO Auto-generated method stub
        return lastIndex == capacity-1;
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return lastIndex;
    }

    @Override
    public void enqueue(T element) {
        if (!isFull()) {
            heap[++lastIndex] = element;
            shiftUp();

            String test = "";
            for (int i = 0; i < heap.length; i++) {
                test += "" + heap[i] + " ";
            }
            System.out.println(test);
        }
    }

    @Override
    public T dequeue() {
        if(isEmpty()) throw new QueueEmptyException();
        T rootValue = heap[0];
        swap(0, lastIndex);
        heap[lastIndex] = null;
        lastIndex--;
        shiftDown();
        String test = "";
        for (int i = 0; i < heap.length; i++) {
            test += "" + heap[i] + " ";
        }
        System.out.println(test);
        return rootValue;
    }

    @Override
    public T getFront() {
        if(isEmpty()) throw new QueueEmptyException();
        return heap[0];
    }

    private void shiftUp() {
        int index = lastIndex;
        int parentIndex = parent(index);
        while (parentIndex > -1 && heap[index].compareTo(heap[parentIndex]) > 0) {
            swap(index, parentIndex);
            index = parentIndex;
            parentIndex = parent(parentIndex);
        }
    }

    private void shiftDown() {
        int index = 0;

        while (index < lastIndex) {

            T maxValue = heap[index];
            int maxIndex = index;

            int leftIndex = left(index);
            if (leftIndex > 0 && maxValue.compareTo(heap[leftIndex]) > 0) {
                maxValue = heap[leftIndex];
                maxIndex = leftIndex;
            }

            int rightIndex = left(index);
            if (rightIndex > 0 && maxValue.compareTo(heap[rightIndex]) > 0) {
                maxValue = heap[rightIndex];
                maxIndex = rightIndex;
            }

            if (maxIndex == index) {
                break;
            }

            swap(maxIndex, index);
            index = maxIndex;
        }
    }

    private int parent(int index) {
        return index/2;
    }

    private int left(int index) {
        int leftChild = index * 2;
        return leftChild;
    }

    private int right(int index) {
        int rightChild = index * 2 + 1;
        return rightChild;
    }

    private void swap(int index1, int index2) {
        T temp = heap[index1];
        heap[index1] = heap[index2];
        heap[index2 ] = temp;
    }

    public static void main(String[] args) {
        Integer[] data = {1, 2, 5, 6, 7, 8, 9, 10, 15, 20};
        HeapPriorityQueue<Integer> pq = new HeapPriorityQueue<Integer>(Integer.class,10);
        for (Integer i : data) {
            pq.enqueue(i);
        }
        System.out.println(pq.dequeue());
        System.out.println(pq.dequeue());
    }

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题