Question
java

The following code is an implementation of a HeapPriorityQueue. You are to implement the methods commented with: // TODO: TIP
// TODO: complete implementation private int parent int pos) // TODO: complete implementation private int leftChild ( int pos
0 0
Add a comment Improve this question Transcribed image text
Answer #1

package HeapPriorityQueue;

import java.util.PriorityQueue;


public class HeapPriorityQueue extends PriorityQueue {
  
private final static int DEFAULT_SIZE=10000;
private Comparable[] storage;
private int currentSize;
  
public HeapPriorityQueue(){
this(DEFAULT_SIZE);
}
  
  
public HeapPriorityQueue(int size){
storage = new Comparable[size+1];
currentSize = 0;
}
  
public void insert(Comparable k)throws HeapFullException{
if(currentSize == storage.length)
throw new HeapFullException();
storage[currentSize]= k;
currentSize++;
bubbleUp(currentSize);
}
  
public void bubbleUp(int index){
//Not req
}
public void swapElement(int pos1,int pos2){
//Not req
}
  
public boolean hasLeft(int pos)
{
return (leftChild(pos)<= currentSize);
}
  
  
public boolean hasRight(int pos)
{
return (rightChild(pos)<= currentSize);
}
  
private int parent(int pos){
// Parent of any heap array is index/2 if index is not root element other wise root has no parent
return (pos/2);
}
  
private int leftChild(int pos){
return pos*2;// Left Child in array is always in order
}
  
private int rightChild(int pos){
return pos*2+1;
}
}

package HeapPriorityQueue;
public class HeapFullException extends Exception {
public HeapFullException()
{
super("Heap is full.");
}
}

Add a comment
Know the answer?
Add Answer to:
java The following code is an implementation of a HeapPriorityQueue. You are to implement the methods...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • add/ remove any methods to the program. please post new code and output Min Heap: public...

    add/ remove any methods to the program. please post new code and output Min Heap: public class MinHeap { private int[] Heap; private int size; private int maxsize; private static final int FRONT = 1; public MinHeap(int maxsize) { this.maxsize = maxsize; this.size = 0; Heap = new int[this.maxsize + 1]; Heap[0] = Integer.MIN_VALUE; } private int parent(int pos) { return pos / 2; } private int leftChild(int pos) { return (2 * pos); } private int rightChild(int pos) {...

  • Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an...

    Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an array of elements as a parameter and uses that array as the heap rather than creating a new array. Of course, the array passed in is probably not in proper heap ordering, so you must rearrange it until it is. There's a neat trick for achieving this: If you just "bubble down" all of the non-leaf nodes of the heap, starting from the last...

  • import java.util.Scanner; import class17.HeapPriorityQueue; import class17.PriorityQueue; /*************** * Homework D * * * Remove any initial...

    import java.util.Scanner; import class17.HeapPriorityQueue; import class17.PriorityQueue; /*************** * Homework D * * * Remove any initial package declaration that might be added to your file in * case you edit it in eclipse. * * The goal of the homework is to create two ArrayList based implementations of * a Priority Queue as explained in Section 9.2 (in 9.2.4 and 9.2.5) of the * textbook. * * These are to be made by completing the classes PQunsorted and PQsorted as...

  • Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an...

    Write a second constructor that could be added to the HeapPriorityQueue class. This constructor accepts an array of elements as a parameter and uses that array as the heap rather than creating a new array. Of course, the array passed in is probably not in proper heap ordering, so you must rearrange it until it is. There's a neat trick for achieving this: If you just "bubble down" all of the non-leaf nodes of the heap, starting from the last...

  • Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the...

    Can Anyone help me to convert Below code to C++! Thanks For example, in C++, the function headers would be the following: class MaxHeap { vector<int> data; public: MaxHeap() { // ... } int size() { // ... } int maxLookup() { // ... } void extractMax() { // ... } void insert(int data) { // ... } void remove(int index) { // ... } }; ======================== import java.util.Arrays; import java.util.Scanner; public class MaxHeap { Integer[] a; int size; //...

  • I need some help with some homework questions. How would I implement the to-do's? ----------------------------------------------------------------------------------------------------------------------------------------------------------- AbstractArrayHea

    I need some help with some homework questions. How would I implement the to-do's? ----------------------------------------------------------------------------------------------------------------------------------------------------------- AbstractArrayHeap.Java package structures; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.NoSuchElementException; public abstract class AbstractArrayHeap<P, V> {   protected final ArrayList<Entry<P, V>> heap;   protected final Comparator<P> comparator; protected AbstractArrayHeap(Comparator<P> comparator) {     if (comparator == null) {       throw new NullPointerException();     }     this.comparator = comparator;     heap = new ArrayList<Entry<P, V>>();   } public final AbstractArrayHeap<P, V> add(P priority, V value) {     if (priority == null || value...

  • a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a...

    a) Create a class MinHeap implementation using an Array. Find the kth smallest value in a collection of n values, where 0 < k < n. Write a program that uses a minheap method to find the kth smallest value in a collection of n values. Use the MinHeap class defined in part a. public final class MinHeap<T extends Comparable<? super T>>              implements MinHeapInterface<T> {    private T[] heap;      // Array of heap entries; ignore heap[0]    private int...

  • I am not passing some of the code when i run the testing . PriorityQueue public...

    I am not passing some of the code when i run the testing . PriorityQueue public class PriorityQueue<T extends Comparable<T>> implements IPriorityQueue<T> {    private Vector<T> theVector = new Vector<>(0, 1);    private int size = 0;    @Override    public void insert(T element) {        theVector.pushBack(element);        size++;        bubbleUp(); }    @Override    public T peekTop() throws java.util.NoSuchElementException {        if (isEmpty()) {            throw new java.util.NoSuchElementException();        }       ...

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

  • // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap...

    // CMPS 390 // MinHeap.java // Complete 4 methods: getMin, add, removeMin, reheap public class MinHeap { private Comparable a heap; // array of heap entries private static final int DEFAULT MAX SIZE = 100; private int lastIndex; // index of last entry public MinHeap() { heap = new Comparable (DEFAULT_MAX_SIZE]; lastIndex = 0; } // end default constructor public MinHeap (int maxSize) { heap = new Comparable (maxSize); lastIndex = 0; } // end constructor public MinHeap (Comparable[] entries)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT