Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Who can help me to write this JAVA? Write a Java program to implement priority q

ID: 3845184 • Letter: W

Question

Who can help me to write this JAVA?

Write a Java program to implement priority queue using min-heap as we discussed in class.

Heap size: 30 nodes with random-generated number between 1`100.

Show the state of the min-heap tree using an array after adding each node.

Show the state of the min-heap tree after removing each node.

Sample Log:

Add 30: [30]
Add 81: [30, 81]
Add 28: [28, 81, 30]
Add 25: [25, 28, 30, 81]
Add 86: [25, 28, 30, 81, 86]
Add 97: [25, 28, 30, 81, 86, 97]
Add 29: [25, 28, 29, 81, 86, 97, 30]

.

.Add 3: [2, 8, 3, 25, 12, 18, 15, 29, 41, 28, 25, 29, 72, 76, 21, 81, 95, 55, 44, 86, 41, 39, 92, 97, 55, 96, 84, 88, 80, 30]

Remove 2: [3, 8, 15, 25, 12, 18, 21, 29, 41, 28, 25, 29, 72, 76, 30, 81, 95, 55, 44, 86, 41, 39, 92, 97, 55, 96, 84, 88, 80] size: 29
Remove 3: [8, 12, 15, 25, 25, 18, 21, 29, 41, 28, 39, 29, 72, 76, 30, 81, 95, 55, 44, 86, 41, 80, 92, 97, 55, 96, 84, 88] size: 28
Remove 8: [12, 25, 15, 29, 25, 18, 21, 81, 41, 28, 39, 29, 72, 76, 30, 88, 95, 55, 44, 86, 41, 80, 92, 97, 55, 96, 84] size: 27

.
.

Remove 88: [92, 95, 97, 96] size: 4
Remove 92: [95, 96, 97] size: 3
Remove 95: [96, 97] size: 2
Remove 96: [97] size: 1
Remove 97: [] size: 0

Explanation / Answer

import java.util.PriorityQueue;

public class PriorityQueueMinHeap
{
   PriorityQueue<Integer> pq;

   public PriorityQueueMinHeap() {
       pq = new PriorityQueue<Integer>();
   }

   public void insert(int[] x) {
       for (int i = 0; i < x.length; i++) {
           pq.offer(x[i]);
       }
   }

   public int peek() {
       return pq.peek();
   }

   public int extractMin() {
       return pq.poll();
   }

   public int getSize() {
       return pq.size();
   }

   public void print() {
       System.out.println(pq);
   }

   public static void main(String[] args) {
       int[] arrA = {30, 81, 28, 25, 86, 97, 29,2, 8, 3, 25, 12, 18, 15, 29, 41, 28, 25, 29, 72, 76, 21, 81, 95, 55, 44, 86, 41, 39, 92, 97, 55, 96, 84, 88, 80, 30 };
       PriorityQueueMinHeap pq = new PriorityQueueMinHeap();
       pq.insert(arrA);
       pq.print();
      
       System.out.println("Priority Queue Size: " + pq.getSize());
   }
}