Revise Heap in Listing 23.9, using a generic parameter and a Comparator for comparing objects. Define a
Question:
Revise Heap in Listing 23.9, using a generic parameter and a Comparator for comparing objects. Define a new constructor with a Comparator as its argument as follows:Heap(Comparator comparator)
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: 72% (11 reviews)
Program Plan Define the Heap class as done in the Listing but dont extend the Generic to comparable Create a new property for comparing here C which belongs to Comparator class Include this property i...View the full answer
Answered By
Aysha Ali
my name is ayesha ali. i have done my matriculation in science topics with a+ . then i got admission in the field of computer science and technology in punjab college, lahore. i have passed my final examination of college with a+ also. after that, i got admission in the biggest university of pakistan which is university of the punjab. i am studying business and information technology in my university. i always stand first in my class. i am very brilliant client. my experts always appreciate my work. my projects are very popular in my university because i always complete my work with extreme devotion. i have a great knowledge about all major science topics. science topics always remain my favorite topics. i am also a home expert. i teach many clients at my home ranging from pre-school level to university level. my clients always show excellent result. i am expert in writing essays, reports, speeches, researches and all type of projects. i also have a vast knowledge about business, marketing, cost accounting and finance. i am also expert in making presentations on powerpoint and microsoft word. if you need any sort of help in any topic, please dont hesitate to consult with me. i will provide you the best work at a very reasonable price. i am quality oriented and i have 5 year experience in the following field.
matriculation in science topics; inter in computer science; bachelors in business and information technology
_embed src=http://www.clocklink.com/clocks/0018-orange.swf?timezone=usa_albany& width=200 height=200 wmode=transparent type=application/x-shockwave-flash_
4.40+
11+ Reviews
14+ 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
-
Revise BST in Listing 25.5, using a generic parameter and a Comparator for comparing objects. Define a new constructor with a Comparator as its argument as follows:BST(Comparator comparator) Listing...
-
Revise MyPriorityQueue in Listing 24.9, using a generic parameter for comparing objects. Define a new constructor with a Comparator as its argument as follows:PriorityQueue(Comparator comparator)...
-
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...
-
Lamonda Corp. uses a job order cost system. On April 1, the accounts had the following balances: The following transactions occurred during April: (a) Purchased materials on account at a cost of...
-
Show that the open interval a, b and the closed interval a, b are both convex sets of R with the natural order (example 1.20). The hybrid intervals a, b and a, b are also convex. Show that intervals...
-
Solve the systems in Problems 3140 graphically and indicate whether each solution region is bounded or unbounded. Find the coordinates of each corner point. 2x + 3y = 24 x + 3y 15 = 4 y
-
True or False: If \(\operatorname{IRR}(\mathrm{A})>\operatorname{IRR}(\mathrm{B})\), then \(\operatorname{ERR}(\mathrm{A})>\operatorname{ERR}(\mathrm{B})\).
-
Selected comparative financial statements of Korbin Company follow. Required 1. Compute each years current ratio. (Round ratio amounts to one decimal.) 2. Express the income statement data in common-...
-
Explain the advantages of the relational model. Conduct some light research on the Web and describe how it differs from other data models. Explain some alternatives to the relational model. Please...
-
The following are some selected quotes from senior executives: CEO, Worthington Industries (a high- technology steel company): We try to find the best technology, stay ahead of the competition, and...
-
Write a program that displays a heap graphically, as shown in Figure 23.10. The program lets you insert and delete an element from the heap. Exercise23_10 78 56 34 43 4 1 15 2 23 Enter a key: 78...
-
Implement the following sort method using a heap. public static > void sort(E[] list)
-
Two accountants, Yuan Tsui and Sergio Aragon, are arguing about the merits of presenting an income statement in the multiple-step versus the single-step format. The discussion involves the following...
-
Your client, George, wants to know when he can deduct the part of the points related to home improvement. When can George deduct these points?
-
In a retail business with daily cash transactions, the management implements a change fund to facilitate smooth operations. What purpose does the change fund serve?
-
You are the CFO of Harvard Manufacturing Co. and two project managers proposed their projects to you: Manager A: A 5-year project with initial investment (Year 0) of -$100,000. Year 1 projected...
-
An entity reports net income before income taxes this year of $300,000. The enacted tax rate is 30%. The entity has reported a $40,000 gain on an installment sale that will not be taxed for two...
-
How accounting estimates and policies might significantly impact the reported financial position and performance of Sidewalk 6. (provide specific examples and assumptions)
-
Bujold Company has the following internal controls over cash payments. Identify the control activity that is applicable to each of the following: 1. Company cheques are pre-numbered. 2. Blank cheques...
-
The nitrogen atoms in N2 participate in multiple bonding, whereas those in hydrazine, N2H4, do not. (a) Draw Lewis structures for both molecules. (b) What is the hybridization of the nitrogen atoms...
-
Four channels, two with a bit rate of 200 kbps and two with a bit rate of 150 kbps, are to be multiplexed using multiple-slot TDM with no synchronization bits. Answer the following questions: a. What...
-
Distinguish between multilevel TDM, multiple-slot TDM, and pulse-stuffed TDM.
-
Ten sources, six with a bit rate of 200 kbps and four with a bit rate of 400 kbps, are to be combined using multilevel TDM with no synchronizing bits. Answer the following questions about the final...
-
Using two-way Set-Associative mapping method, design two-way mapping for cache memory from main memory. Explain why you need each condition and show all intermediate calculation procedures. You must...
-
18) Under the proration approach, the sum of the amounts shown in the subsidiary ledgers will not match the amounts shown in the general ledger because no adjustments from budgeted to actual...
-
Simpson Auto Body Repair purchased $20,000 of Machinery. The company paid $8,000 in cash at the time of the purchase and signed a promissory note for the remainder to be paid in four monthly...
Study smarter with the SolutionInn App