Question: Implement a min - heap class in Java. ( a ) The keys are of type int. ( b ) Your class shall be named

Implement a min-heap class in Java.
(a) The keys are of type int.
(b) Your class shall be named as follows:
ca.ucalgary.cpsc331.MinHeap
(c) Your class shall implement the following Java interface:
package ca.ucalgary.cpsc331;
public interface PriorityQueue {
boolean empty();
boolean full();
void insert(int key);
int extractMin();
int min();
}
(d) The min-heap has a capacity of 31.
(e) Your implementation shall check for underflow and overflow of
the heap, and throws
appropriate exceptions when such situations arise.
1(f) The class shall provide a constructor that does not take any
arguments. This constructor
initializes the heap to an empty one.
(g) The class shall override the toString() method. Your
implementation of
toString() shall return a String in the following format:
The string consists of multiple lines, each terminated by a
newline character. No
line shall contain leading or trailing whitespaces.
The first line shall look like this:
size =12
The size of the heap is the number of cells that are actually
occupied. (Of course,
your binary heap may have a different size.)
Subsequent lines list the keys stored in the binary heap, from
low indices to high
indices. Elements belonging to the same height shall belong to the
same line. Adjacent elements are separated by a single blank space.
For example, the following
lines list the elements of a min-heap.
2
107
1220158
3321294018
Hint: Note that Java array indices start from zero rather than
one

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!