Question: In Java: The given Heap class in this problem (MaxHeap) is also known as a max-heap, in which each node is greater than or equal

In Java:

The given Heap class in this problem (MaxHeap) is also known as a max-heap, in which each node is greater than or equal to any of its children. A min-heap is a Heap in which each node is less than or equal to any of its children. Revise the following Heap class to implement a min-heap (MinHeap). Min-heaps are often used to implement priority queues.

The given Heap class in this problem (MaxHeap) is also known as a max-heap, in which each node is greater than or equal to any of its children. A min-heap is a Heap in which each node is less than or equal to any of its children. Revise the following Heap class to implement a min-heap (MinHeap). Min-heaps are often used to implement priority queues.

**************************************

The main and MaxHeap are given and need no change.

**************************************

import java.util.*;

import java.lang.*;

import java.io.*;

class MaxHeap> {

private java.util.ArrayList list = new java.util.ArrayList<>();

/** Create a default heap */

public MaxHeap() {

}

/** Create a heap from an array of objects */

public MaxHeap(E[] objects) {

for (int i = 0; i < objects.length; i++)

add(objects[i]);

}

/** Add a new object into the heap */

public void add(E newObject) {

list.add(newObject); // Append to the heap

int currentIndex = list.size() - 1; // The index of the last node

while (currentIndex > 0) {

int parentIndex = (currentIndex - 1) / 2;

// Swap if the current object is greater than its parent

if (list.get(currentIndex).compareTo(

list.get(parentIndex)) > 0) {

E temp = list.get(currentIndex);

list.set(currentIndex, list.get(parentIndex));

list.set(parentIndex, temp);

}

else

break; // the tree is a heap now

currentIndex = parentIndex;

}

}

/** Remove the root from the heap */

public E remove() {

// Enter Code Here

/** Get the number of nodes in the tree */

public int getSize() {

return list.size();

}

}

class DriverMain{

public static void main(String args[]){

MinHeap minHeap = new MinHeap();

Scanner input = new Scanner(System.in);

String str = input.nextLine();

input.close();

int[] list = Arrays.stream(str.substring(0, str.length()).split("\\s"))

.map(String::trim).mapToInt(Integer::parseInt).toArray();

// Add elements to the min heap

for (int i = 0; i < list.length; i++)

minHeap.add(list[i]);

// Remove elements from the min heap

for (int i = 0; i < list.length; i++)

System.out.print(minHeap.remove() +" ");

}

}

******************************************************

Here's where to edit the code.

*****************************************************

public class MinHeap> {

private java.util.ArrayList list = new java.util.ArrayList<>();

/** Create a default heap */

public MinHeap() {

}

/** Create a heap from an array of objects */

public MinHeap(E[] objects) {

for (int i = 0; i < objects.length; i++)

add(objects[i]);

}

/** Add a new object into the heap */

public void add(E newObject) {

//write your code here

}

/** Remove the root from the heap */

public E remove() {

//write your code here

}

/** Get the number of nodes in the tree */

public int getSize() {

return list.size();

}

Please also provide comments with the code. Thanks!

Expected Output:

output 6 8 2 0 1 0 1 2 6 8

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 Databases Questions!