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 h;

public PriorityQueue() {

h = new Heap();

}

public PriorityQueue (Comparable super T> comparator){

h = new Heap((Comparator super T>) comparator);

}

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 items; //array of heap items

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 ) item1).compareTo(item2);

}

else

{

return comparator.compare(item1, item2);

}

}

}

Java code thanks

package Lab12; import java.util.Comparator; public class PriorityQueue { private Heap h; public

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 class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!