public class HeapPriorityQueue -K extends Comparable- V- implements Pr (1).docx
1. public class HeapPriorityQueue <K extends Comparable, V> implements PriorityQueue <K, V>
{
private Entry [] storage; //The Heap itself in array form
private int tail; //Index of last element in the heap
public HeapPriorityQueue() {this(100);}
public HeapPriorityQueue( int size ) {
storage = new Entry [ size ];
tail = -1;}
public int size() {return tail + 1;}
public Entry <K, V>insert( K key, V value) throws IllegalArgumentException {
if( tail == storage.length - 1 )
throw new IllegalArgumentException ( "Heap Overflow" );
Entry <K, V> e = new Entry <> ( key, value );
storage [ ++tail ] = e;
e.setIndex(tail);
upHeap ( tail );
return e;
public Entry <K, V> min() {
if( isEmpty() )
return null;
return storage [ 0 ];
}
public Entry <K, V> removeMin() {
if( isEmpty() )
return null;
Entry <K, V> ret = storage [ 0 ];
if( tail == 0 ) {
tail = -1;
storage [ 0 ] = null;
return ret;
}
private void upHeap( int location ) {
if( location == 0 ) return;
int parent = parent ( location );
if( storage [ parent ].key.compareTo ( storage [ location ].key ) > 0 ) {
swap ( location, parent );
upHeap ( parent );
}
}
private void downHeap( int location ) {
2. int left = (location * 2) + 1;
int right = (location * 2) + 2;
//Both children null or out of bound
if( left > tail ) return;
//left in right out;
if( left == tail ) {
if( storage [ location ].key.compareTo ( storage [ left ].key ) > 0 )
swap ( location, left );
return;
}
int toSwap = (storage [ left ].key.compareTo ( storage [ right ].key ) < 0) ?
left : right;
if( storage [ location ].key.compareTo ( storage [ toSwap ].key ) > 0 ) {
swap ( location, toSwap );
downHeap ( toSwap );
}
}
private int parent( int location ) {
return (location - 1) / 2;
}
private void swap( int location1, int location2 ) {
Entry <K, V> temp = storage [ location1 ];
storage [ location1 ] = storage [ location2 ];
storage [ location2 ] = temp;
storage[location1].index= location1;
storage[location2].index= location2;
}
public void print() {
for (Entry<K,V> e : storage)
System.out.println ( "(" + e.key.toString() + "," + e.value.toString() + ":" + e.index + "), " );
}
}
public class HeapPriorityQueueTest {
static String alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main( String [] args ) {
Random rng = new Random(12345); // do not change the seed
3. // A min-heap
HeapPriorityQueue <Integer, Character> pq = new HeapPriorityQueue <> ( 500 );
// Insert elements in heap
for( int i = 0; i < 100; i++ ) {
Entry <Integer, Character> e= pq.insert ( rng.nextInt ( 10000 ), alpha.charAt ( rng.nextInt ( 52 )
));
}
System.out.println();System.out.println();
// removeMin
for( int i = 0; i < 8; i++ ) {
Entry <Integer, Character> e = pq.removeMin();
System.out.print ( "(" + e.key.toString() + "," + e.value.toString() + ":" + e.index + "), " );
}
System.out.println();System.out.println();
// removeMax
/*
for( int i = 0; i < 8; i++ ) {
Entry <Integer, Character> e = pq.removeMax();
System.out.print ( "(" + e.key.toString() + "," + e.value.toString() + ":" + e.index + "), " );
}
*/
System.out.println();System.out.println();
// removeMin
for( int i = 0; i < 8; i++ ) {
Entry <Integer, Character> e = pq.removeMin();
System.out.print ( "(" + e.key.toString() + "," + e.value.toString() + ":" + e.index + "), " );
}
System.out.println();System.out.println();
// removeMax
/*
for( int i = 0; i < 8; i++ ) {
Entry <Integer, Character> e = pq.removeMax();
System.out.print ( "(" + e.key.toString() + "," + e.value.toString() + ":" + e.index + "), " );
}
*/
}
}
4. The objective of this programming assignment is to implement a double-ended priority queue;
this is a priority queue for which you can obtain both the max and the min element. This double-
ended queue will be implemented using two heaps: a min-heap and a max-heap. Half of the
elements are stored in one of the heaps and the other half is stored in the second heap. Each
element a of the min-heap is associated with one element of the max-heap b with a < b . If the
total number of elements is odd, then the extra element is contained in a 1-element buffer. The
min element removal Removing the min element is done as follows: 1. Check the element at the
root of the min-heap, if this one is greater than the element in the buffer (if any) then simply
return the element in the buffer 2. Else you remove and return the element at the root of the min-
heap. The associated element in the max-heap must now be re-processed. a. If the buffer is
empty, then remove the element to be re-processed from the maxheap and insert it in the buffer
b. If the buffer is not empty, then the element to be re-processed and the element in the buffer
must now form a new pair. c. If the element in the buffer is smaller than the element to be re-
processed, then you simply have to insert the element in the buffer into the min-heap. d. If the
element in the buffer is greater than the element to be re-processed, then i . these two elements
must exchange their values ii. the max-heap must be modified in order to re-establish the heap
condition. iii. the element in the buffer must be inserted in the min-heap. The max element
removal Removing the max element is done by following the same procedure as for the removal
of the min element as explained above, except that it now operates from the max-heap.
Instructions: To build your solution, start from the code provided that implements a regular min-
heap. The index attribute in the Entry class provides the current position of the element in the
array. The associate attribute is currently not used. There is also a test main function in the
HeapPriorityQueueTest class. Do not modify this class but you will uncomment the section that
calls the removeMax method that you have to implement. You must follow the algorithm
described above. For step 2 of the removal step, you will have to design an algorithm that allows
to re-establish the heap condition when a node at any position is mispositioned. Note that you
can assume that there will not be 2 keys with the same value in the two heaps.