Question: Please show me how to solve this and explain. ( CORRECT JAVA CODE ONLY last time it was wrong ) GEHeap.java import java.util.ArrayList; public class
Please show me how to solve this and explain. CORRECT JAVA CODE ONLY last time it was wrong
GEHeap.java
import java.util.ArrayList;
public class GEHeap
public static void mainString args
MaxHeap H new MaxHeap;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
Hinsert;
System.out.printlnHeap contents:";
Hprt;
System.out.println
Elements :;
ArrayList gte HGreaterOrEqual;
forInteger i : gte
System.out.printlni;
MaxHeap.java
import java.util.ArrayList;
Maxheap implementation; from C Shaffer,"Data Structures and Algorithm
Analysis in Java"
public class MaxHeap
private ArrayList Heap;
private int n; Number of things in heap
Constructor for initially empty heap
public MaxHeap
Heap new ArrayList;
n ;
Constructor supporting preloading of heap contents
public MaxHeapE h int num
Heap new ArrayListnum;
n num;
buildheap;
@return Current size of the heap
public int heapsize
return n;
public boolean isEmpty
return n ;
@return True if pos a leaf position, false otherwise
public boolean isLeafint pos
return pos n && pos n;
@return Position for left child of pos
public int leftchildint pos
assert pos n : "Position has no left child";
return pos ;
@return Position for right child of pos
public int rightchildint pos
assert pos n : "Position has no right child";
return pos ;
@return Position for parent
public int parentint pos
assert pos : "Position has no parent";
return pos ;
Insert val into heap
public void insertE val
Systemout.printlnInserting val;
prt;
Systemout.printlnSizen;
Heap.addval; Start at end of heap
int curr n;
n;
Now sift up until curr's parent's key curr's key
while curr
&& HeapgetcurrcompareToHeapgetparentcurr
swapcurr parentcurr;
curr parentcurr;
Heapify contents of Heap
public void buildheap
for int i n ; i ; i
siftdowni;
Put element in its correct place
private void siftdownint pos
assert pos && pos n : "Illegal heap position";
while isLeafpos
int j leftchildpos;
if j n && HeapgetjcompareToHeapgetj
j; j is now index of child with greater value
if HeapgetposcompareToHeapgetj
return;
swappos j;
pos j; Move down
Remove and return maximum value
public E removeMax
assert n : "Removing from empty heap";
swapn; Swap maximum with last value
if n Not on last element
siftdown; Put new heap root val in correct place
return Heap.getn;
Return but do not remove maximum value
public E peek
if n Not empty
return Heap.get;
else
return null;
Remove and return element at specified position
public E removeint pos
assert pos && pos n : "Illegal heap position";
if pos n
n; Last element, no work to be done
else
swapposn; Swap with last value
If we just swapped in a big value, push it up
while pos
&& HeapgetposcompareToHeapgetparentpos
swappos parentpos;
pos parentpos;
if n
siftdownpos; If it is little, push down
return Heap.getn;
private void swapint i int j
E temp Heap.getj;
Heap.setj Heap.geti;
Heap.seti temp;
public void prt
forint i; i
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
