The heap presented in the text is also known as a max-heap, in which each node is
Question:
The heap presented in the text 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. Min-heaps are often used to implement priority queues. Revise the Heap class in Listing 23.9 to implement a min-heap.
Listing
Transcribed Image Text:
1 public class Heap
1 public class Heap> { private java.util.ArrayList list = new java.util.ArrayList<>(); 3 /** Create a default heap */ 4. public Heap() { 6 /** Create a heap from an array of objects * / public Heap (E[] objects) { for (int i = 0; i < objects.length; i++) add(objects[i]); 10 11 12 13 14 /** 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 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 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 31 currentIndex = parentIndex; 32 33 34 35 /** Remove the root from the heap */ public E remove() { if (list.size( == 0) return null; 36 37 38 E removedObject = list.get (0); list.set (0, list.get(list.size() - 1)); list.remove (1ist.size() - 1); 39 40 41 42 int currentIndex = 0; 43 while (currentIndex < list.size()) { 44 45 int leftChildIndex = 2 * currentIndex + 1; int rightChildIndex = 2 * currentIndex + 2; 46 47 // Find the maximum between two children if (leftChildIndex >= list.size()) break; // The tree is a heap int maxIndex = leftChildIndex; if (rightChildIndex < list.size()) { if (list.get(maxIndex).compareTo( 48 49 50 51 52 53 list.get(rightChildIndex)) < 0) { 54 maxIndex = rightChildIndex; 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 // 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); currentIndex - maxIndex; else break; // The tree is a heap 70 return removedObject; } 71 72 73 /** Get the number of nodes in the tree */ public int getSize() { return list.size(); 74 75 76 77
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 50% (10 reviews)
Program Plan Define a class called MinHeap Add a constructor with obje...View the full answer
Answered By
Emel Khan
I have the ability to effectively communicate and demonstrate concepts to students. Through my practical application of the subject required, I am able to provide real-world examples and clarify complex ideas. This helps students to better understand and retain the information, leading to improved performance and confidence in their abilities. Additionally, my hands-on approach allows for interactive lessons and personalized instruction, catering to the individual needs and learning styles of each student.
5.00+
2+ Reviews
10+ Question Solved
Related Book For
Introduction to Java Programming, Comprehensive Version
ISBN: 978-0133761313
10th Edition
Authors: Y. Daniel Liang
Question Posted:
Students also viewed these Computer science questions
-
Unicode value \u0000 is also known as ____________. a. Nil b. Null c. Nada d. Void
-
The procedure BUILD-MAX-HEAP in Section 6.3 can be implemented by repeatedly using MAX-HEAP-INSERT to insert the elements into the heap. Consider the following implementation: BUILD-MAX-HEAP'(A) 1...
-
We can build a heap by repeatedly calling MAX-HEAP-INSERT to insert the elements into the heap. Consider the following variation on the BUILD-MAX-HEAP procedure: BUILD-MAX-HEAP (A) 1 A.heap-size = 1...
-
Sandys Socks makes the worlds best socks. Information for the last eight months follows: Prepare a scatter graph by plotting Sandys data on a graph. Then draw a line that you believe best fits the...
-
If S = {x1, x2, . . . , xn} is affinely independent, every x aff S has a unique representation as an affine combination of the elements of S; that is, there are unique scalars a1, a2, . . . , an...
-
Solve the systems in Problems 2130 graphically and indicate whether each solution region is bounded or unbounded. Find the coordinates of each corner point. 2x + 3y 12 x 0 y 0
-
Consider the following cash flow profile and assume MARR is 10 percent/year. a. What does Descartes' rule of signs tell us about the IRR(s) of this project? b. What does Norstrom's criterion tell us...
-
At December 31, 2014, the trial balance of Roberto Company contained the following amounts before adjustment. Instructions (a) Based on the information given, which method of accounting for bad debts...
-
Define the relational model. What is a relational database management system (DBMS)?
-
Alexander Smith and his wife Allison are married and file a joint tax return for 2019. The Smiths live at 1234 Buena Vista Drive, Orlando, FL 32830. Alexander is a commuter airline pilot but took 6...
-
Implement the following sort method using a heap. public static > void sort(E[] list)
-
Write the following overloaded methods that check whether an array is ordered in ascending order or descending order. By default, the method checks ascending order. To check descending order, pass...
-
How many days will it take for $2075 to earn $124.29 interest at 8.25% p.a.?
-
Explain Briefly You've been engaged by an activist investor to provide strategic guidance on potential target companies. The investment fund has identified the energy transition as a key opportunity...
-
Make a list of relevant and reliable sources of information, such as business publications, journals, blogs, Twitter feeds, financial reports, and websites to help you keep up to date.
-
Calculate a land size adjustment based on table land size adjustment table, 2 acres land size, adjustment factor 1.26 ,value 396,500, what adjustment factor would you apply for sale 5 acres land,...
-
Bong Joon Ho is a license appraiser, who hire to find the Gross Rent Multiplier of Studio Condominium Property in Bacolod. The market value is estimated at 3,800,000 with annual rent of 360,000....
-
We examined post-mortem directives and reviewed comparisons between cremations and funeral burials, including the costs and benefits, and disadvantages of each. Moreover, it is important to know the...
-
Miramichi Company has the following internal controls over cash receipts. Identify the control activity that is applicable to each of the following: 1. The company conducts thorough background checks...
-
(a) Explain why the concentration of dissolved oxygen in freshwater is an important indicator of the quality of the water. (b) How is the solubility of oxygen in water affected by increasing...
-
Define the analog hierarchy used by telephone companies and list different levels of the hierarchy.
-
We need to use synchronous TDM and combine 20 digital sources, each of 100 Kbps. Each output slot carries 1 bit from each digital source, but one extra bit is added to each frame for synchronization....
-
In the analog hierarchy of Figure 6.9, find the overhead (extra bandwidth for guard band or control) in each hierarchy level (group, supergroup, master group, and jumbo group). Figure 6.9 48 kHz 12...
-
The forces in (Figure 1) act on a 1.1 kg object. Part A What is the value of a, the z-component of the object's acceleration? Express your answer with the appropriate units. Figure 3.0 N' 4.0 N y 3.0...
-
There is a performance gap between processors and main memory which cache memories aim to bridge. Task 1: Review issues that operating system designers face in the implementation of highly optimized...
-
Determine the power rating of the motor in an elevator system. The elevator (with a full load) weights 2200 kg and is required to move upward 3 m/sec at constant speed. The lifting mechanism is 82%...
Study smarter with the SolutionInn App