0

我正在尝试创建一个使用二进制堆实现 PriorityQueue 接口的 HeapPriorityQueue 类。不使用 Java 集合的 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)"

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

4

1 回答 1

0
 public class HeapPriorityQueue<Integer, String> { 

不扩展/实现 PriorityQueue。这就是它失败的原因

PriorityQueue<Integer, String> queue = new HeapPriorityQueue<Integer, String>();

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

于 2013-09-07T10:42:04.723 回答