【问题标题】:Java creating a HeapPriorityQueue using binary heap to implement PriorityQueueJava使用二叉堆创建HeapPriorityQueue来实现PriorityQueue
【发布时间】:2013-09-07 10:37:38
【问题描述】:

我正在尝试创建一个使用二进制堆实现 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、三) (五、五) (7、七) (九,九) (10,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)"

谁能帮我解决这个问题?

【问题讨论】:

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


    【解决方案1】:
     public class HeapPriorityQueue<Integer, String> { 
    

    不扩展/实现 PriorityQueue。这就是它在

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

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

    【讨论】:

    • 老实说我不知道​​怎么做的先生
    • 阅读java继承,它应该更清楚。请记住,如果您实现一个接口,则需要实现该接口中定义的所有方法。
    • 公共类 yourClass 实现 yourInfterface { /* ... */ }
    猜你喜欢
    • 2013-08-16
    • 2018-05-16
    • 1970-01-01
    • 2013-11-30
    • 2010-10-19
    • 1970-01-01
    • 2011-08-02
    • 2011-09-25
    • 2011-11-28
    相关资源
    最近更新 更多