Java Code JDK 11
Please Turn my min-heap into a max -heap. Thank you!
public MinHeap(ArrayList<T> data) {
// Initialize the backing array
backingArray = (T[]) new Comparable[2 * data.size() + 1];
// Copy the data to the backing array
int i = 1;
for (T value : data)
backingArray[i++] = value;
size = data.size();
// Heapify the backing array
for (int parentIndex = size / 2; parentIndex >= 1; parentIndex--)
heapify(parentIndex);
}
/**
* A recursive helper method that will heapify the backing array so that
* it will become a min heap.
*
* @param parentIndex Parent index being processed
*/
private void heapify(int parentIndex) {
// Stop if we end up in an empty slot
if (backingArray[parentIndex] == null)
return;
int leftChildIndex = parentIndex * 2;
int rightChildIndex = parentIndex * 2 + 1;
// Find which is smaller between the parent and any of its 2 children
int smallerIndex = parentIndex;
if (leftChildIndex <= size && backingArray[leftChildIndex].compareTo(backingArray[smallerIndex]) < 0)
smallerIndex = leftChildIndex;
if (rightChildIndex <= size && backingArray[rightChildIndex].compareTo(backingArray[smallerIndex]) < 0)
smallerIndex = rightChildIndex;
// Swap the smaller value to the parent if there is smaller than the parent
if (smallerIndex != parentIndex) {
T value = backingArray[parentIndex];
backingArray[parentIndex] = backingArray[smallerIndex];
backingArray[smallerIndex] = value;
// Since there's a new parent, repeat the process on that parent
heapify(smallerIndex);
}
}
Step by stepSolved in 2 steps
- PP 12.4 The array implementation in this chapter keeps the top variablepointing to the next array position above the actual top of thestack. Rewrite the array implementation such that stack[top] isthe actual top of the stack. PP 12.5 There is a data structure called a drop-out stack that behaves likea stack in every respect except that if the stack size is n, whenthe n + 1 element is pushed, the first element is lost. Implement adrop-out stack using an array. (Hint: A circular array implementation would make sense.)arrow_forwardSort the array using insertion sort algorithm list ={18,57,8,89,7}arrow_forwardPython: Written without libraries method find_ansestors , that takes in an index i and returns all ancestors of node located at index i. What is the time complexity of your function? class ArrayBinaryTree: def __init__(self): self._heap = [] def find_ancestors(self, i: int): if(i==0): return;arrow_forward
- java follow directions on the picture dont use others answers please!! will leave you feedback!! Thank You!!arrow_forwardJava program In main method: Create 2-D Array, called m: the row size of array m is 15 ask user to input column size of array m fill all array elements as double numbers in the range of (7.0, 17.0) by using random object create 2-d array, called double [][]md = {{1,1,1},{2,0,1},{4,7,4}}; pass the above array m and md to call the following two methods MaxCol(m); MaxCol(md); System.out.println (“the average of array m is: ” + returnLast3RowAvg(m)); System.out.println (“the average of array md is: ” + returnLast3RowAvg(md)); Print out the largest column sum, and the average of the last 3 rows of array m and md. In MaxCol(double[][]array) method, find and print the largest column sum in the array. In returnLast3RowAvg(double[][]array) method, find the average of the elements in the last 3 rows in the array and return this average value.arrow_forwardWrite the full Java code for NaturalMergeSorter.javaarrow_forward
- JAVA The following code for InsertionSort is given to us by the textbook. Trace the code stepby step using the array[55, 22, 77, 99, 66, 33, 11]on a piece of paper or using a Word document. If the code has errors, correct it and make itwork.public static void insertionSort(double[] list) {for (int i = 1; i < list.length; i++) {/** insert list[i] into a sorted sublist list[0..i-1] so thatlist[0..i] is sorted. */double currentElement = list[i];int k;for (k = i - 1; k >= 0 && list[k] > currentElement; k--) {list[k + 1] = list[k];}// Insert the current element into list[k+1]list[k + 1] = currentElement;}}arrow_forwardCode to ::::implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializes an array-like data structure with the given length. Initially, each element equals 0. void set(index, val) sets the element at the given index to be equal to val. int snap() takes a snapshot of the array and returns the snap_id: the total number of times we called snap() minus 1. int get(index, snap_id) returns the value at the given index, at the time we took the snapshot with the given snap_id Example 1: Input: ["SnapshotArray","set","snap","set","get"] [[3],[0,5],[],[0,6],[0,0]] Output: [null,null,0,null,5] Explanation: SnapshotArray snapshotArr = new SnapshotArray(3); // set the length to be 3 snapshotArr.set(0,5); // Set array[0] = 5 snapshotArr.snap(); // Take a snapshot, return snap_id = 0 snapshotArr.set(0,6); snapshotArr.get(0,0); // Get the value of array[0] with snap_id = 0, return 5...arrow_forwardJava (Generic Types) - Zip Code and Populationarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education