Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

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

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions

Question

How is the combined ratio defined? What does it measure?

Answered: 1 week ago

Question

What advantages does this tactic offer that other tactics do not?

Answered: 1 week ago

Question

Proficiency with Microsoft Word, Excel, PowerPoint

Answered: 1 week ago

Question

Experience with SharePoint and/or Microsoft Project desirable

Answered: 1 week ago