Question: public class HeapPriorityQueue implements PriorityQueue { private Entry [] storage; //The Heap itself in array form private int tail; //Index of last element in the
![public class HeapPriorityQueue implements PriorityQueue { private Entry [] storage; //The](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f33229411d5_12066f33228d99d2.jpg)


public class HeapPriorityQueue
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
Entry
Entry
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 ) { 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 )
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
public class HeapPriorityQueueTest {
static String alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static void main( String [] args ) {
Random rng = new Random(12345); // do not change the seed
// A min-heap HeapPriorityQueue
System.out.println();System.out.println();
// removeMin for( int i = 0; i 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 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 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 e = pq.removeMax(); System.out.print ( "(" + e.key.toString() + "," + e.value.toString() + ":" + e.index + "), " ); } */ } }
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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
