Question
Consider the following Java code snippet:
import java.util.Collections; import java.util.PriorityQueue; public class HeapQuestion2 { public static void main(String[] args) { PriorityQueue maxHeap = new PriorityQueue(Collections.reverseOrder()); maxHeap.add(10); maxHeap.add(5); maxHeap.add(20); maxHeap.add(3); System.out.println(maxHeap.poll()); System.out.println(maxHeap.poll()); } } What will be the output of this program?Solution
By passing Collections.reverseOrder() to the PriorityQueue constructor, we create a max-heap, where the largest element has the highest priority (is at the root). 1. maxHeap.add(10); maxHeap.add(5); maxHeap.add(20); maxHeap.add(3); After these additions, the heap contains {20, 10, 5, 3} (conceptually, 20 is at the root). 2. System.out.println(maxHeap.poll()); poll() removes and returns the largest element. So, 20 is removed and printed. The heap now contains {10, 5, 3}. 3. System.out.println(maxHeap.poll()); poll() removes and returns the largest element again. So, 10 is removed and printed. The heap now contains {5, 3}. Therefore, the output is 20, 10.
दिए गए प्रत्येक प्रश्न में एक हिंदी का वाक्य दिया गया �...
दिए गए वाक्य का उचित हिंदी अनुवाद चुने
India allows vegetable o...
हमें बैंक में खाता खोलने के लिए न्यूनतम धनराशि जमा करनी प�...
Inflation शब्द का हिंदी में अर्थ कौन सा शब्द नहीं होगा?
नीचे अंग्रेज़ी के वाक्य दिए गए है उनके सही हिन्दी अनुव...
निम्नलिखित में से बाध्य मितव्ययिता शब्द का वित्तीय श�...
दिए गए विकल्पों में से वह वाक्य चुने जो प्रश्न में दिए ...
निम्नलिखित में से विशेष कार्य अधिकारी शब्द का वित्�...
निम्नलिखित में से कौन सा ‘ conduct' शब्द का शब्दावली में सही...
दृष्टिबंधक –