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 !

```java
// Find the maximum between two children
int leftChildIndex = 2 * currentIndex + 1;
int rightChildIndex = 2 * currentIndex + 2;
int maxIndex = leftChildIndex;

if (leftChildIndex >= list.size()) break; // The tree is a heap

if (rightChildIndex < list.size()) {
    if (list.get(maxIndex).compareTo(
        list.get(rightChildIndex)) < 0) {
        maxIndex = rightChildIndex; // compare two children
    }
}

// Swap if the current node is less than the maximum
if (list.get(currentIndex).compareTo(
    list.get(maxIndex)) < 0) {
    E temp = list.get(maxIndex);
    list.set(maxIndex, list.get(currentIndex));
    list.set(currentIndex, temp); // swap with the larger child
    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();
}
```

### Explanation

This code is part of a heap sort implementation. It describes a method used to maintain the heap property of a binary heap stored in a list.

- **Lines 45-47**: Calculate the indices of the left and right children of a given node `currentIndex` in a complete binary tree. These indices are based on the formula for heap-based tree structures.
- **Lines 48-49**: Check if the `leftChildIndex` is beyond the size of the list, which indicates that the subtree rooted at `currentIndex` is already a valid heap.
- **Lines 50-53**: Determine if the right child exists and compare the values at `maxIndex` and `rightChildIndex`. If the right child value is greater, update `maxIndex`.
- **Lines 56-63**: Check if the value at `currentIndex` is less than the value at `maxIndex`. If so, swap them and update `currentIndex` to `maxIndex` to continue sifting down the heap. Otherwise, if no swap is needed, exit the loop since the tree satisfies the heap property.
- **Line 67**: Returns the removed object after maintaining the heap property.
- **Lines 72-75**: A method `getSize()` that returns the number
Transcribed Image Text:```java // Find the maximum between two children int leftChildIndex = 2 * currentIndex + 1; int rightChildIndex = 2 * currentIndex + 2; int maxIndex = leftChildIndex; if (leftChildIndex >= list.size()) break; // The tree is a heap if (rightChildIndex < list.size()) { if (list.get(maxIndex).compareTo( list.get(rightChildIndex)) < 0) { maxIndex = rightChildIndex; // compare two children } } // Swap if the current node is less than the maximum if (list.get(currentIndex).compareTo( list.get(maxIndex)) < 0) { E temp = list.get(maxIndex); list.set(maxIndex, list.get(currentIndex)); list.set(currentIndex, temp); // swap with the larger child 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(); } ``` ### Explanation This code is part of a heap sort implementation. It describes a method used to maintain the heap property of a binary heap stored in a list. - **Lines 45-47**: Calculate the indices of the left and right children of a given node `currentIndex` in a complete binary tree. These indices are based on the formula for heap-based tree structures. - **Lines 48-49**: Check if the `leftChildIndex` is beyond the size of the list, which indicates that the subtree rooted at `currentIndex` is already a valid heap. - **Lines 50-53**: Determine if the right child exists and compare the values at `maxIndex` and `rightChildIndex`. If the right child value is greater, update `maxIndex`. - **Lines 56-63**: Check if the value at `currentIndex` is less than the value at `maxIndex`. If so, swap them and update `currentIndex` to `maxIndex` to continue sifting down the heap. Otherwise, if no swap is needed, exit the loop since the tree satisfies the heap property. - **Line 67**: Returns the removed object after maintaining the heap property. - **Lines 72-75**: A method `getSize()` that returns the number
```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
Transcribed Image Text:```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
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
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