Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Convert the given Java source code for MaxBinaryHeap and HeapSort to a Min - Binary Heap and Decreasing Heap Sort by changing the direction of
Convert the given Java source code for MaxBinaryHeap and HeapSort to a MinBinary Heap and Decreasing Heap Sort by changing the direction of two comparing operators in the percolateDown method replace by Execute the test case available in the main method of the given java file:
public static void mainString args
Comparable array new Comparable;
heapSortarray;
forint i ; i array.length;i
System.out.printlnarrayi;
Starter Code:
public class BinaryHeap
private Comparable data;
private int size ;
public BinaryHeapint capacity
data new Comparablecapacity;
public BinaryHeapComparable data
this.data data;
size data.length ;
forint i size; i ;i
percolateDowni;
public int size
return size;
private void percolateDownint rootheapify
int leftChild root ;
int rightChild leftChild ;
int max; max of left,, right, and parent nodes.
comparing parent with left child
ifleftChild size && dataleftChildcompareTodataroot
max leftChild;
else
max root;
comparing the max with the right child
if rightChild size && datarightChildcompareTodatamax
max rightChild;
ifmax rootif not the base case!
Comparable temp dataroot;swapping max w root
dataroot datamax;
datamax temp;
percolateDownmax;
public Comparable extractMax
ifsize
throw new IllegalStateException;
Comparable rv data;
data datasize;swap root w last leaf
datasize rv;
size;
percolateDown;heapify the root
return rv;
public static BinaryHeap buildHeapComparable values
return new BinaryHeapvalues;
public static void heapSortComparable values
BinaryHeap heap buildHeapvalues;
forint i ; i values.length ;i
heap.extractMax;
public static void mainString args
Comparable array new Comparable;
heapSortarray;
forint i ; i array.length;i
System.out.printlnarrayi;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started