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:

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

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:

  1. Write a main method that will accept 5 numbers, and put them in a min-heap
  2. 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
Transcribed Image Text: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
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()) {
Transcribed Image Text: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()) {
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 6 images

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education