Answered step by step
Verified Expert Solution
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 print out the same
Part points: Implement Priority Queue using Heap structure. Download and complete the implementation of class PQHeap.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 above. Hint: Create the heap object as private data member in classfile PQHeap.java; Then use the methods in class Heap.java to implement the priority queue methods outlined in classfile PQHeap.java by applying the heap methods on the heap object. In other words, implement class PQHeap using methods in class Heap.java. It is recommended that you implement class PQHeap.java as a generic class, similar to class Heap.java. In addition, to implement all priority queue methods outlined in class PQHeap.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 PQHeap.java. Use the following menu options as shown below No menu, no points! Force the user to start with option to select the data type of the priority queue content. Then allow the user to exercise other menu options on the selected queue type. Enter Queue Type integer or string Enqueue Element Dequeue Element Check is Full Check isEmpty Print PQueue Size Display Front Element Print PQueue Elements 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 and above, we need to set the heap capacity to a certain size. For this assignment, let's all added variable int CAPACITY ; in class Heap.java for this purpose. Also, in class Heap.java, method list.size gives you the current size of the heap. Always redisplay the menu after an option other than option is fully exercised with blank lines before and after the menu. For option print the PQ content as shown below. Notice that value below is the parent node for values left child and right child; value is the parent node for values left child and right child; Nodes and have no child nodes on the heap
For option print the content as shown below. Notice that value below is the parent node for
values left child and right child; value is the parent node for values left child and right
child; Nodes and have no child nodes on the heap.
PQHEAP.JAVA
Generic code for class priorityqueueheap for Assignment
public class PQheap
Constructor method
PQheap;
Return true if priority queue is empty; otherwise return false
public boolean isempty
;
Return true if priority queue is full; otherwise return false
public boolean isfull;
Return dont 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 enqueueint value;
HEAP.JAVA
Class Heap.java
Textbook Listing Page
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 HeapE objects
for int i ; i objects.length; i
addobjectsi;
Add a new object into the heap
public void addE newObject
list.addnewObject; Append to the heap
int currentIndex list.size; The index of the last node
while currentIndex
int parentIndex currentIndex ;
Swap if the current object is greater than its parent
if listgetcurrentIndexcompareTo
list.getparentIndex
E temp list.getcurrentIndex;
list.setcurrentIndex list.getparentIndex;
list.setparentIndex temp;
else
break; the tree is a heap now
currentIndex parentIndex;
e
Remove the root from the heap
public E remove
if listsize return null;
E removedObject list.get;
list.set list.getlistsize;
list.removelistsize;
int currentIndex ;
while currentIndex list.size
int leftChildIndex currentIndex ;
int rightChildIndex currentIndex ;
Find the maximum between two children
if leftChildIndex list.size break; The tree is a heap
int maxIndex leftChildIndex;
if rightChildIndex list.size
if listgetmaxIndexcompareTo
list.getrightChildIndex
maxIndex rightChildIndex;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started