Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   HeapPriorityQueue (http://www.velocityreviews.com/forums/t128747-heappriorityqueue.html)

gabbadd 11-17-2003 02:41 AM

HeapPriorityQueue
 
Hello Anyone who can assist in getting this code to work. Thanks.

package priorityq;

/**
* Defines an implementation of a Priority Queue using a heap
* represented with an array

*/

public class HeapPriorityQueue implements PriorityQueue
{
private final int SIZE = 1000;
private MyElement a[];
private int size;

/**
* Builds an array of MyElements of default size SIZE
*/

public HeapPriorityQueue()
{
// Creates array of MyElement
this.a = new MyElement[ SIZE ];
size = 0;
}

/**
* Builds an array of MyElements of default size size
*
* @param size
*/

public HeapPriorityQueue( int size )
{
// Creates array of MyElement
this.a = new MyElement[ size ];
this.size = 0;
}

// returns number of elements in the queue
public int size()
{

}

// returns true if the queue is empty
public boolean isEmpty()
{
return (size == 0);
}

// insert the pair (k,o)
public void enqueue( int key, Object obj ) throws QueueException
{

}

// extract object with maximum key
public Object maxElement() throws QueueException
{

}

// extract maximum key (without removing the element from the queue)
public int maxKey() throws QueueException
{

}

// Remove element with min key
public void remove() throws QueueException
{

}

private void heapify( int i, int j )
{
int sx, dx, k;

sx = 2 * i + 1;
dx = sx + 1;

if ( sx > j )
return;

if ( dx > j )
k = sx;
else
k = ( a[ sx ].getKey() > a[ dx ].getKey() ) ? sx : dx;

if ( a[ i ].getKey() < a[ k ].getKey() ) {
swap( i, k );
heapify( k, j );
}
}

private void swap( int i, int j )
{
MyElement tmp;

tmp = a[ i ];
a[ i ] = a[ j ];
a[ j ] = tmp;
}

private int father( int i )
{
return ( i - 1 ) / 2;
}

private void makeheap()
{
for ( int i = father( a.length - 1 ); i >= 0; i-- )
heapify( i, a.length - 1 );
}
}

Christophe Vanfleteren 11-17-2003 10:39 AM

Re: HeapPriorityQueue
 
gabbadd wrote:

> Hello Anyone who can assist in getting this code to work. Thanks.
>

<snip code>

It would probably help if you say what's not working, what it does now and
what you expect it to do.

http://www.catb.org/~esr/faqs/smart-questions.html

--
Regards,
Christophe Vanfleteren


All times are GMT. The time now is 05:59 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.