Java使用二进制堆创建HeapPriorityQueue来实现PriorityQueue

时间:2013-09-07 10:37:39

标签: java arrays string priority-queue binary-heap

我正在尝试创建一个使用二进制堆实现PriorityQueue接口的HeapPriorityQueue类。不使用Java Collections的PriorityQueue。

HeapPriorityQueue类将由此驱动程序类运行:

public class HeapPriorityQueueDriver {

    public static void main(String[] args) {
        PriorityQueue<Integer, String> queue = new HeapPriorityQueue<Integer, String>();
        queue.insert(0, "Zero");
        queue.insert(10, "Ten");
        queue.insert(1, "One");
        queue.insert(5, "Five");
        queue.insert(3, "Three");
        queue.insert(7, "Seven");
        queue.insert(9, "Nine");

        while(!queue.isEmpty()) {
            System.out.println(queue.extractMax());
        } // end while
    } // end main
}

程序的输出应该是。

(1,一种) (3,三个) (5,五) (7,七) (9,九)个 (10,十个)

这是我尝试过的课程:

HeapPriorityQueue(引用)

public class HeapPriorityQueue<Integer, String> {
    /**
     * Construct the binary heap.
     */
    public HeapPriorityQueue( )
    {
        currentSize = 0;
        array = new Comparable[ DEFAULT_CAPACITY + 1 ];
    }

    /**
     * Construct the binary heap from an array.
     * @param items the inital items in the binary heap.
     */
    public HeapPriorityQueue( Comparable [ ] K )
    {
        currentSize = K.length;
        array = new Comparable[ K.length + 1 ];

        for( int i = 0; i < K.length; i++ )
            array[ i + 1 ] = K[ i ];
        buildHeap( );    
    }

    /**
     * Remove the smallest item from the priority queue.
     * @return the smallest item.
     * @throws UnderflowException if empty.
     */
    public Comparable deleteMin( )
    {
        Comparable minK = findMin( );
        array[ 1 ] = array[ currentSize-- ];
        percolateDown( 1 );

        return minK;
    }

    private Comparable findMin() {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * Establish heap order property from an arbitrary
     * arrangement of items. Runs in linear time.
     */
    private void buildHeap( )
    {
        for( int i = currentSize / 2; i > 0; i-- )
            percolateDown( i );
    }

    /**
     * Returns size.
     * @return current size.
     */
    public int size( )
    {
        return currentSize;
    }

    /**
     * Make the priority queue logically empty.
     */
    public void makeEmpty( )
    {
        currentSize = 0;
    }

    private static final int DEFAULT_CAPACITY = 100;

    private int currentSize;      // Number of elements in heap
    private Comparable [ ] array; // The heap array

    /**
     * Internal method to percolate down in the heap.
     * @param hole the index at which the percolate begins.
     */
    private void percolateDown( int hole )
    {
        int child;
        Comparable tmp = array[ hole ];

        for( ; hole * 2 <= currentSize; hole = child )
        {
            child = hole * 2;
            if( child != currentSize &&
                    array[ child + 1 ].compareTo( array[ child ] ) < 0 )
                child++;
            if( array[ child ].compareTo( tmp ) < 0 )
                array[ hole ] = array[ child ];
            else
                break;
        }
        array[ hole ] = tmp;
    }

    /**
     * Internal method to extend array.
     */
    private void doubleArray( )
    {
        Comparable [ ] newArray;

        newArray = new Comparable[ array.length * 2 ];
        for( int i = 0; i < array.length; i++ )
            newArray[ i ] = array[ i ];
        array = newArray;
    }
}

控制台中的错误行:“线程中的异常”main“java.lang.Error:未解决的编译问题:     类型不匹配:无法从HeapPriorityQueue转换为PriorityQueue

at HeapPriorityQueueDriver.main(HeapPriorityQueueDriver.java:5)"

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

 public class HeapPriorityQueue<Integer, String> { 

不会扩展/实施PriorityQueue。这就是

失败的原因
PriorityQueue<Integer, String> queue = new HeapPriorityQueue<Integer, String>();

使其扩展/实现priorityQueue,或将驱动程序类中使用的类型更改为HeapPriorityQueue。