The missing codes are for the functions down there
Can i get help with the missing code"insert code here" space : java language
The missing codes are for the functions down there
code :
import java.util.Comparator;
import java.util.NoSuchElementException;
public class PriorityQueue
{
/* attributes */
private int currentSize; // Number of elements in heap
private PriorityQueueNode [ ] PQ; // The priority queue array
private static final int DEFAULT_CAPACITY = 2>>14;
private Comparator<? super PriorityQueueNode> cmp; // object to compare to instances of PriorityQueueNode
/* default constructor */
public PriorityQueue( )
{
currentSize = 0;
cmp = null;
PQ = new PriorityQueueNode [DEFAULT_CAPACITY + 1];
}
/* getter methods for attributes */
public int getSize( )
{
return currentSize;
}
public boolean add( PriorityQueueNode x )
{
// insert code here
}
public PriorityQueueNode remove( )
{
// insert code here
}
private void percolateDown( int hole )
{
// insert code here
}
public void display() //
{
// insert code here
}
/* auxiliary methods: you may use the below methods to help you write your code, if necessary */
private int compare( PriorityQueueNode lhs, PriorityQueueNode rhs )
{
if(cmp == null)
{
return ((Comparable)lhs).compareTo(rhs);
}
else
{
return cmp.compare(lhs, rhs);
}
}
public void swapReferences(PriorityQueueNode [] a, int p, int q)
{
PriorityQueueNode tmp = a[p];
a[p] = a[q];
a[q] = tmp;
}
private void doubleArray( )
{
PriorityQueueNode [ ] newArray;
newArray = new PriorityQueueNode[ PQ.length * 2 ];
for( int i = 0; i < PQ.length; i++ )
newArray[ i ] = PQ[ i ];
PQ = newArray;
}
public PriorityQueueNode element( )
{
if(isEmpty())
{
throw new NoSuchElementException();
}
return PQ[1];
}
public void clear( )
{
currentSize = 0;
}
public boolean isEmpty()
{
return currentSize==0;
}
}
Step by step
Solved in 2 steps