优先级队列不理解如何跟踪算法

问题描述 投票:0回答:1

这些是我收到的指示...

  1. 按照给定的顺序将以下n个对象插入到二进制最小堆中,您应该跟踪push方法。 5, 3, 9, 7, 2, 4, 6, 1, 8

  2. 应用 pop() 方法 3 次

我不明白如何从我的二叉树中弹出

这就是我所拥有的..

      9
    /  \
    8   6
   /\  /\
  7  4 5 1
  /
  3

这是我需要跟踪并弄清楚如何弹出的代码。

import java.util.Collection;
import java.util.NoSuchElementException;

/**
 * PriorityQueue class implemented via the binary heap.
 */
public class PriorityQueue<AnyType>
{

    private static int INITSIZE = 100;

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


    /**
     * Construct an empty PriorityQueue.
     */
    public PriorityQueue( )
    {
        currentSize = 0;
        array = (AnyType[]) new Object[ INITSIZE + 1 ];
    }


    /**
     * Compares lhs and rhs using compareTo
     */
    private int compare( AnyType lhs, AnyType rhs ) {
        return ((Comparable)lhs).compareTo( rhs );  
    }

    /**
     * Inserts an item to this PriorityQueue.
     * @param x any object.
     * @return true.
     */
    public boolean push( AnyType x )
    {
        if( currentSize + 1 == array.length )
            expandArray( );

            // Percolate up
        int hole = ++currentSize;
        array[ 0 ] = x;

        for( ; compare( x, array[ hole / 2 ] ) < 0; hole /= 2 )
            array[ hole ] = array[ hole / 2 ];
        array[ hole ] = x;

        return true;
    }

    /**
     *  isEmpty() indicates whether the heap is empty.
     *  @return true if the list is empty, false otherwise.
     **/

    public boolean isEmpty() {
        return currentSize == 0;
    }

    /**
     * Returns the number of items in this PriorityQueue.
     * @return the number of items in this PriorityQueue.
     */
    public int size( )
    {
        return currentSize;
    }

    /**
     * Make this PriorityQueue empty.
     */
    public void clear( )
    {
        currentSize = 0;
    }

    /**
     * Returns the smallest item in the priority queue.
     * @return the smallest item.
     * @throws NoSuchElementException if empty.
     */
    public AnyType element( )
    {
        if( isEmpty( ) )
            throw new NoSuchElementException( );
        return array[ 1 ];
    }

    /**
     * Removes the smallest item in the priority queue.
     * @return the smallest item.
     * @throws NoSuchElementException if empty.
     */
    public AnyType pop( )
    {
        AnyType minItem = element( );
        array[ 1 ] = array[ currentSize-- ];
        percolateDown( 1 );

        return minItem;
    }


    /**
     * 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 );
    }


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

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

    /**
     *  expandArray(): internal method to extend array.
     *  creates a new array with larger size (twice)
     */
    private void expandArray() {
        AnyType [ ] newArray;

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

    public static void main( String [ ] args )
    {
        PriorityQueue t = new PriorityQueue( );
        final int NUMS = 4000;
        final int GAP  =   37;

        System.out.println( "Checking... (no more output means success)" );
        int min = 1000000;
        for( int i = GAP; i != 0; i = ( i + GAP ) % NUMS ) {
            if (min > i)
                min = i;
            t.push( new Integer( i ) );
            if( ((Integer)(t.element( ))).intValue( ) != min )
                System.out.println( "Push error! "+i+"   "
                        +((Integer)(t.element( ))).intValue( ));
        }

        for( int i = 1; i < NUMS; i++ )
             if( ((Integer)(t.pop( ))).intValue( ) != i )
                 System.out.println( "Pop error!" );
    }
}
java priority-queue traversal
1个回答
0
投票

从堆中删除第一项的过程是:

1. Copy the first item from the array. That is the return value.
2. Take the lowest, right-most node (in this case, it would be the value 3), and place it in the first position of the array.
3. Reduce the number of items in the array by 1.
4. Sift the item down from the root to its proper place.

顺便说一句,你拥有的是最大堆。在最小堆中,最小的项位于根。

请参阅我的博客文章,更好的方法:堆,了解如何操作二进制堆的更详细讨论。

© www.soinside.com 2019 - 2024. All rights reserved.