Question: / / 1 . Implement a Heap class based on my code ArrayBinaryTree.java / / 2 . Test the insert and delete methods of your
Implement a Heap class based on my code ArrayBinaryTree.java
Test the insert and delete methods of your Heap class using the examples on my slide PQ&Heap.pdf
The output should be:
Test insert
Before insert
Inserting a new node
After insert
null null null null
Test removeMin
Before removeMin
Removing the minimum
After removeMin
null
Note
DO NOT DELETE ANY COMMENT!!!
Modify the file name to "ArrayHeap.java" before compiling it
public class ArrayHeap extends ArrayBinaryTree
private int size;
private int height;
@SuppressWarningsunchecked
public ArrayHeap
super;
this.size ;
public ArrayHeapE root
superroot;
this.size ;
public ArrayHeapE array
superarray;
this.size array.length;
public void expandArray
@SuppressWarningsunchecked
E newArray E new Comparablethisarray.length ;
forint i; i this.array.length; i
newArrayi arrayi;
this.array newArray;
public int height
return intMathceilMathlogthissize Math.log;
public void insertE element
if thissize this.array.length
this.expandArray;
this.arraythissize element;
this.size;
this.upHeap;
public void upHeap
parentElement.compareTocurrentElement means
parentElement currentElement
int currentRank this.size ;
whilecurrentRank
int parentRank getParentRankcurrentRank;
E parentElement getElementparentRank;
E currentElement getElementcurrentRank;
if parentRank && parentElement.compareTocurrentElement
E temp currentElement;
this.arraycurrentRank parentElement;
this.arrayparentRank temp;
currentRank parentRank;
public void removeMin
ifthissize
System.out.printlnThe heap is empty!";
return;
this.array this.arraysize ;
this.arraysize null;
this.size;
this.downHeap;
public void downHeap
COMPLETE THIS BLOCK
public static void mainString args
System.out.println Test insert;
Integer array ;
ArrayHeap myHeap new ArrayHeaparray;
System.out.printlnBefore insert;
myHeap.display;
System.out.printlnInserting a new node ;
myHeap.insert;
System.out.printlnAfter insert;
myHeap.display;
System.out.println Test removeMin;
Integer array;
ArrayHeap myHeap new ArrayHeaparray;
System.out.printlnBefore removeMin;
myHeapdisplay;
System.out.printlnRemoving the minimum ;
myHeapremoveMin;
System.out.printlnAfter removeMin;
myHeapdisplay;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
