Question
package Lab12; import java.util.Comparator; public class PriorityQueue { private Heap h; public PriorityQueue() { h = new Heap (); } public PriorityQueue (Comparable super T>
package Lab12;
import java.util.Comparator;
public class PriorityQueue
private Heap
public PriorityQueue() {
h = new Heap
}
public PriorityQueue (Comparable super T> comparator){
h = new Heap
}
public boolean pqIsEmpty(){
return h.heapIsEmpty();
}
public void pqInsert(T newItem) throws PriorityQueueException {
try{
h.heapInsert(newItem);
}
catch (HeapException e) {
throw new PriorityQueueException(
"PQueueException: Problem inserting to Priority Queue");
}
}
public T pqDelete(){
return h.heapDelete();
}
}
package Lab12;
import java.util.ArrayList;
import java.util.Comparator;
public class Heap
private ArrayList
private Comparator super T> comparator;
public Heap() {
items = new ArrayList
} // end default constructor
public Heap(Comparatorcomparator){
items =new ArrayList
this.comparator = comparator;
} // end default constructor
// heap operations:
public boolean heapIsEmpty(){
return items.size()==0;
} // end heapIsEmpty
public void heapInsert(T newItem)
throws HeapException, ClassCastException {
if (!items.add(newItem)){
throw new HeapException("HeapException: heapInsert failed");
} else
{
int place = items.size()-1;
int parent = (place -1)/2;
while ((parent >=0) && (compareItems(items.get(place), items.get(parent)))
T temp=items.get(parent);
items.set(parent, items.get(place));
items.set(place, temp);
place = parent;
parent = (place - 1)/2;
} // end while
} // end else
} //end heapInsert
public T heapDelete (){
T rootItem = null;
int loc;
if (!heapIsEmpty()) {
rootItem = items.get(0);
loc = items.size()-1;
items.set(0, items.get(loc));
items.remove(loc);
heapRebuild(0);
} // end if
return rootItem;
} // end heapDelete
protected void heapRebuild(int root){
int child = 2* root +1;
if (child
int rightChild = child + 1;
if ((rightChild
child = rightChild;
}
if (compareItems(items.get(root), items.get(child))>0){
T temp = items.get(root);
items.set(root, items.get(child));
items.set(child, temp);
heapRebuild(child);
}
}
}
private int compareItems (T item1, T item2) {
if (comparator == null) {
return ((Comparable
}
else
{
return comparator.compare(item1, item2);
}
}
}
Java code thanks
Test Priority Queue. Instructions: . A) Goal: get experience with heap and priority queue.* B) Task: . 1) Implement the HeapException class and PriorityQueception class.* 2) Implement a class named TsiQueue, where you should: a. produce a sequence of 15 random integers, print this sequence preceding with "Unsorted" b. generate an empty priority queue c. insert the 15 integers into the queue d. show the sorted sequence of integers using the priority queue method. c) Procedure: 1. download the attached classes.* 2. Implement the mentioned classes." 3. Run the Test classStep 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