You may use the Java Libraries for solving this problem. We recommend using java.util.* The star “ * ” acts as a wildcard and allows you to use the entire java.util library. The heap presented in the screenshot is also known as a max-heap, in which each node is greater than or equal to any of it's children. A min-heap is a heap in which each node is less than or equal to any of its children. Min-heaps are often used to implement priority queues. Revise the Heap class in the screenshot to implement a min-heap. (The max heap example begins on the screenshot that says "Listings 23.9" at the top). Use the following logic:
PLEASE READ THIS FIRST! _____________________
Please write in java. Add comments but make the comments short. No need for really long comments. Please keep code very neat and simple , dont add anything unneccesary in the code if you weren't instructed to do so. If youve answered this before please dont copy and paste from a previous question! (Rewrite it in another way)..... Also be sure to read the instructions below carefully. The two files that are attached are screenshots of the textbook's MAX-HEAP Example. You will be creating a mini-heap from that example. But the Instructions below explains everything you'll be doing. NO THIS is not a homework assignment, it's practice work. Please type the code out so that I can copy and paste it into my IDE and see the output for my self but also still provide a screenshot of your output!
INSTRUCTIONS FOR QUESTION BELOW!!_____________________________
You may use the Java Libraries for solving this problem. We recommend using java.util.* The star “ * ” acts as a wildcard and allows you to use the entire java.util library.
The heap presented in the screenshot is also known as a max-heap, in which each node is greater than or equal to any of it's children. A min-heap is a heap in which each node is less than or equal to any of its children. Min-heaps are often used to implement priority queues. Revise the Heap class in the screenshot to implement a min-heap. (The max heap example begins on the screenshot that says "Listings 23.9" at the top).
Use the following logic:
- Write a main method that will accept 5 numbers, and put them in a min-heap
- Remove them from the min heap, printing one at a time as they are removed, to show that the list is sorted
Hint: The screenshotted textbook example is for a MAX heap; you must alter that example to reflect a MIN heap – think about it !

![```java
LISTING 23.9 Heap.java
1 public class Heap<E extends Comparable<E>> {
2 private java.util.ArrayList<E> list = new java.util.ArrayList<E>();
3
4 /** Create a default heap */
5 public Heap() {
6 }
7
8 /** Create a heap from an array of objects */
9 public Heap(E[] objects) {
10 for (int i = 0; i < objects.length; i++)
11 add(objects[i]);
12 }
13
14 /** Add a new object into the heap */
15 public void add(E newObject) {
16 list.add(newObject); // Append to the heap
17 int currentIndex = list.size() - 1; // The index of the last node
18
19 while (currentIndex > 0) {
20 int parentIndex = (currentIndex - 1) / 2;
21 // Swap if the current object is greater than its parent
22 if (list.get(currentIndex).compareTo(
23 list.get(parentIndex)) > 0) {
24 E temp = list.get(currentIndex);
25 list.set(currentIndex, list.get(parentIndex));
26 list.set(parentIndex, temp);
27 }
28 else
29 break; // The tree is a heap now
30
31 currentIndex = parentIndex;
32 }
33 }
34
35 /** Remove the root from the heap */
36 public E remove() {
37 if (list.size() == 0) return null;
38
39 E removedObject = list.get(0);
40 list.set(0, list.get(list.size() - 1));
41 list.remove(list.size() - 1);
42
43 int currentIndex = 0;
44 while (currentIndex < list.size()) {
```
**Explanation of Key Code Segments:**
- **Lines 1-2**: Declaration of the class `Heap` with a generic type `<E>` that extends `Comparable<E>`. An `ArrayList<E>` is used to store heap elements.
- **Line 5**: Default constructor for the heap.
- **Lines 9-12**: Constructor to create a heap from an array of objects. It iterates through the array and adds each element to the heap.
- **Lines](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F0b9c1552-116f-4e2a-bbd1-47a03e858b24%2Fb74c9063-4656-45d5-9055-9d67b1354a65%2Fn23pzor_processed.png&w=3840&q=75)

Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 6 images









