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 !
![45
46
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
64
65
66
68
69
70
71
72
73
74
75
76
77 }
int leftChildIndex 2 currentIndex + 1;
int rightChildIndex = 2* currentIndex + 2;
}
// Find the maximum between two children
if (leftChildIndex>-list.size())
int maxIndex = leftChildIndex:
if (rightChildIndex
list.size()) {
if (list.get(max Index).compareTo(
list.get(rightChildIndex)) < 0) {
maxIndex - rightChildIndex;
}
// Swap if the current node is less than the maximum
if (list.get(currentIndex).compareTo(
break; // The tree is a heap
list.get(max Index)) < 0) {
E temp = list.get(maxIndex);
list.set(maxIndex, list.get(currentIndex));
list.set(currentIndex, temp):
currentIndex = maxIndex:
}
else
break; // The tree is a heap
return removedobject;
}
/** Get the number of nodes in the tree "/
public int getSize() {
return list.size():
23.6 Heap Sort 879
compare two children
swap with the larger child](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F0b9c1552-116f-4e2a-bbd1-47a03e858b24%2Fb74c9063-4656-45d5-9055-9d67b1354a65%2F93o0846_processed.png&w=3840&q=75)
![internal heap representation
no-arg constructor
constructor
add a new object
append the object
swap with parent
heap now
remove the root
empty heap
root
new root
remove the last
adjust the tree
LISTING 23.9 Heap.java
1 public class Heap<E extends Comparable<E>> {
2 private java.util.ArrayList<E> list = new java.util.ArrayListo():
3
4
5
6
7
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/** Create a default heap */
public Heap() {
/* Create a heap from an array of objects */
public Heap (E[] objects) {
for (int i = 0; i <objects.length; i++)
add(objects[1]);
}
/** 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(current Index) .compareTo(
list.get(parent Index)) > 0) {
E temp = list.get(currentIndex):
list.set(current Index, 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() {
if (list.size() == 0) return null;
E renovedObject - list.get(0);
list.set(0, list.get(list.size()-1));
list.remove(list.size()-1):
int current Index - 0:
while (currentIndex < list.size()) {](/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)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 6 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)