Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

make sure option 7 print out the same Part 2 ( 8 0 points ) : Implement Priority Queue using Heap structure. Download and complete

make sure option 7 print out the same
Part 2(80 points): Implement Priority Queue using Heap structure. Download and complete the implementation of class PQ_Heap.java, provided with this assignment. The only data member we need for this class is a heap object created from class Heap.java in part 1 above. Hint: Create the heap object as private data member in class/file PQ_Heap.java; Then use the methods in class Heap.java to implement the priority queue methods outlined in class/file PQ_Heap.java by applying the heap methods on the heap object. In other words, implement class PQ_Heap using methods in class Heap.java. It is recommended that you implement class PQ_Heap.java as a generic class, similar to class Heap.java. In addition, to implement all priority queue methods outlined in class PQ_Heap.java, you may need to include additional methods to the generic class Heap.java. Next, create a new test file named TestPQH.java to test class PQ_Heap.java. Use the following menu options as shown below (No menu, no points!). Force the user to start with option 0 to select the data type of the priority queue content. Then allow the user to exercise other menu options on the selected queue type. 0. Enter Queue Type (integer or string)1. Enqueue Element 2. Dequeue Element 3. Check is Full 4. Check is_Empty 5. Print PQueue Size 6. Display Front Element 7. Print PQueue Elements 8. Exit program Enter option number: Note: The author uses ArrayList to implement the heap, so we cannot have a static size for the ArrayList unless to impose a certain capacity for the heap. In order to implement options 3 and 4 above, we need to set the heap capacity to a certain size. For this assignment, let's all added variable int CAPACITY =100; in class Heap.java for this purpose. Also, in class Heap.java, method list.size () gives you the current size of the heap. Always re-display the menu after an option (other than option 8) is fully exercised with blank lines before and after the menu. For option 7, print the PQ content as shown below. Notice that value 99 below is the parent node for values 66(left child) and 44(right child); value 66 is the parent node for values 33(left child) and 22(right child); Nodes 44,33, and 22 have no child nodes on the heap
For option 7, print the PQ content as shown below. Notice that value 99 below is the parent node for
values 66(left child) and 44(right child); value 66 is the parent node for values 33(left child) and 22(right
child); Nodes 44,33, and 22 have no child nodes on the heap.
PQ_HEAP.JAVA
// Generic code for class priority_queue_heap for Assignment 5
public class PQ_heap
{
// Constructor method
PQ_heap(){};
// Return true if priority queue is empty; otherwise return false
public boolean is_empty(){
};
// Return true if priority queue is full; otherwise return false
public boolean is_full(){};
// Return (don't remove) the front element from the priority queue
// Precondition: priority queue is not empty.
public int front(){};
// return number of elements in the queue
public int size(){};
// Remove the largest value from this priority queue and return it.
// Precondition: priority queue is not empty.
public int dequeue(){};
// Inserts the 'value' into the priority queue.
// Precondition: priority queue is not full
public void enqueue(int value){};
}
HEAP.JAVA
// Class Heap.java
// Textbook - Listing 23.9, Page 878
public class Heap>{
private java.util.ArrayList list = new java.util.ArrayList();
/** Create a default heap */
public Heap(){
}
/** Create a heap from an array of objects */
public Heap(E[] objects){
for (int i =0; i objects.length; i++)
add(objects[i]);
}
/** Add a new object into the heap */
public void add(E newObject){
list.add(newObject); // Append to the heap
int currentIndex = list.size()-1; // The index of the last node
while (currentIndex >0){
int parentIndex =(currentIndex -1)/2;
// Swap if the current object is greater than its parent
if (list.get(currentIndex).compareTo(
list.get(parentIndex))>0){
E temp = list.get(currentIndex);
list.set(currentIndex, list.get(parentIndex));
list.set(parentIndex, temp);
}
else
break; // the tree is a heap now
currentIndex = parentIndex;
}e
}
/** Remove the root from the heap */
public E remove(){
if (list.size()==0) return null;
E removedObject = list.get(0);
list.set(0, list.get(list.size()-1));
list.remove(list.size()-1);
int currentIndex =0;
while (currentIndex list.size()){
int leftChildIndex =2* currentIndex +1;
int rightChildIndex =2* currentIndex +2;
// Find the maximum between two children
if (leftChildIndex >= list.size()) break; // The tree is a heap
int maxIndex = leftChildIndex;
if (rightChildIndex list.size()){
if (list.get(maxIndex).compareTo(
list.get(rightChildIndex))0){
maxIndex = rightChildIndex;
}
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions