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.");
}
}
java The following code is an implementation of a HeapPriorityQueue. You are to implement the methods...
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 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 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 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 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; //...
//implement the binomial heap please import java.util.LinkedList; import java.util.NoSuchElementException; /** * Binomial Heap Implementation * * @author First Last * @since ${date} */ public class BinomialHeap<T extends Comparable<? super T>> implements binomialHeapInterface<T> { private static final int DEFAULT = 5; // default size for the binomial heap public Node<T>[] forest; private int n; private boolean isMaxHeap; /** * Node Class for nodes in Binomial Heap */ protected class Node<T> { private T value; private int degree; private LinkedList<Node<T>> children; /**...
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 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 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 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...