Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import java.util.NoSuchElementException; * An implementation of a heap of integers. public class Heap { // Main method to test this class public static void main(String[]

image text in transcribed
image text in transcribed
image text in transcribed
import java.util.NoSuchElementException; * An implementation of a heap of integers. public class Heap { // Main method to test this class public static void main(String[] args) { } private int[heap; // Array where element are stored private int size; // Number of elements in the heap // Constructor creates an empty heap wit capacity 10 public Heap() { this(10); } // Constructor - creates an empty heap wit the // specified capacity public Heap(int capacity) { heap = new int[capacity]; size = 0; } * Adds the given element to the heap. @param e element to add *throws IllegalStateException if the heap is full public void add(int e) { if (size == heap.length) throw new IllegalStateException("heap full"); // WRITE ME! } // Given a parent, returns the index of the smaller child, // or -1 if there are no children. // This is a private method used by remove() private int smaller (int iParent) { // WRITE ME! return 0; // replace this line } /** Removes and returns the smallest element in the heap. (which is the element at the top of the heap). @return smallest element * @throws NoSuchelementException if heap is empty public int remove() { if (size == 0) throw new NoSuchelementException ("empty heap"); // WRITE ME! return 0; // replace this line } Returns true if the given array meets the heap property * that each parent is

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

More Books

Students also viewed these Databases questions