Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the methods for Heap.java. HeapAPI has info on how to implement the methods in heap. Dont modify heapapi it just provides the info. //HEAPAPI.java

Implement the methods for Heap.java. HeapAPI has info on how to implement the methods in heap. Dont modify heapapi it just provides the info.

//HEAPAPI.java do not modify

class HeapException extends Exception { /** * Creates a new instance of HeapException without detail * message. */ public HeapException() { }

/** * Constructs an instance of HeapException with the specified * detail message. * @param msg the detail message. */ public HeapException(String msg) { super(msg); } }

/** * Describes the basic operations of a heap * @author Duncan * @param * @since 99-99-9999 */ public interface HeapAPI> { /** * Determine whether the Heap is empty. * @return this method returns true if the heap is empty; * otherwise, it returns false if the heap contains at least one item. */ boolean isEmpty();

/** * Inserts an item into the Heap. * @param item the value to be inserted. */ void insert(E item);

/** * An exception is generated if this method is invoked * by an empty heap. The maximum/minimum value is removed * from the heap if the heap is not empty and its effective * size is reduced by 1. * @return the maximum (in the case of a maxheap) or the * minimum (in the case of a minheap) on the heap. * @throws HeapException when the heap is empty */ E remove() throws HeapException;

/** * An exception is generated if this method is invoked * by an empty heap * @return the maximum (in the case of a maxheap) or the * minimum (in the case of a minheap) on the heap. * @throws HeapException when the heap is empty */ E peek() throws HeapException;

/** * Gives the size of this heap * @return the size of the heap; the effective size of the * heap. */ int size(); }

public class Heap> implements HeapAPI { /** * A complete tree stored in an array list representing this binary heap */ private ArrayList tree; /** * A comparator lambda function that compares two elements of this heap when * rebuilding it; cmp.compare(x,y) gives 1. negative when x less than y 2. * positive when x greater than y 3. 0 when x equal y */ private Comparator cmp; /** * Constructs an empty heap using the compareTo method of its data type as the * comparator */ public Heap() { tree = new ArrayList; } /** * A parameterized constructor that uses an externally defined comparator * * @param fn * - a trichotomous integer value comparator function */ public Heap(Comparator fn) { this.fn = fn; tree = new ArrayList(); } public boolean isEmpty() { return tree.isEmpty(); } public void insert(E obj) { // implement this method } public E remove() throws HeapException { // implement this method } public E peek() throws HeapException { // implement this method } public int size() { return tree.size; } /** * Swaps a parent and child elements of this heap at the specified indices * * @param place * an index of the child element on this heap * @param parent * an index of the parent element on this heap */ private void swap(int place, int parent) { } /** * Rebuilds the heap to ensure that the heap property of the tree is preserved. * * @param root * the root index of the subtree to be rebuilt * @param eSize * the size of this tree */ private void rebuild(int root, int eSize) { // implement this method } }

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

Database And Expert Systems Applications 31st International Conference Dexa 2020 Bratislava Slovakia September 14 17 2020 Proceedings Part 1 Lncs 12391

Authors: Sven Hartmann ,Josef Kung ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

303059002X, 978-3030590024

More Books

Students also viewed these Databases questions

Question

=+applying to all or most employers and employees?

Answered: 1 week ago

Question

=+associated with political parties and if so, which ones? Are

Answered: 1 week ago