How can I modify an element of a PriorityQueue in Java?
A PriorityQueue is an abstract data type that is similar to a queue, and every element has some priority value associated with it.
It is used when the objects are supposed to be processed based on priority. The elements of the priority queue are ordered according to the natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used.
Operations on a Priority Queue:
A typical priority queue supports the following operations:
Insertion in a Priority Queue: When a new element is inserted in a priority queue, it moves to the empty slot from top to bottom and left to right. However, if the element is not in the correct place then it will be compared with the parent node. If the element is not in the correct order, the elements are swapped. The swapping process continues until all the elements are placed in the correct position.
Deletion in a Priority Queue: In a max heap, the maximum element is the root node. To remove the element which has maximum priority first, you remove the root node from the queue. This removal creates an empty slot, which will be filled with the new maximum element of the heap and the rest of the tree will get adjusted accordingly.
Peek in a Priority Queue: This operation helps to return the maximum element from Max Heap or the minimum element from Min Heap without deleting the node from the priority queue
Modifying the First element:
Here, we are modifying elements at the first position of the priority queue(in decreasing order). We modify the first element of the PriorityQueue by removing it and adding a new element as shown below:
Java
// Java code to modify first element of priority queue import java.util.*; class GeeksforGeeks { public static void main(String[] args) { // Creating priorityqueue in decreasing order // Here, priority is given first to larger numbers PriorityQueue<Integer> pq = new PriorityQueue<>( Collections.reverseOrder()); // Adding elements into priorityqueue pq.offer(1); pq.offer(5); pq.offer(7); pq.offer(3); // Removing first element and // modifying it to 6 System.out.println("Original first element: " + pq.poll()); // Priorityqueue becomes 5, 3, 1 // adding new element 6 // priorityqueue becomes 6, 5, 3, 1 pq.offer(6); // Printing modified first element System.out.println("Modified first element: " + pq.peek()); pq.offer(9); System.out.println( "Again, re-modified first element: " + pq.peek()); } }
Original first element: 7 Modified first element: 6 Again, re-modified first element: 9
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Modifying any element of a Custom Priority Queue:
For custom PriorityQueue, we first search and remove an element, make necessary modifications and reinsert it back.
Below is the implementation of the approach.
Java
// Java code to modify any element from priority queue import java.util.*; // Creating class Geek which stores name, // number of toffees of all Geeks class Geek { String name; int toffee; Geek(int toffee, String name) { this.name = name; this.toffee = toffee; } } // Sorting in ascending order of // number of toffees using comparator class sortBy implements Comparator<Geek> { public int compare(Geek g1, Geek g2) { if (g1.toffee > g2.toffee) { return 1; } else if (g1.toffee < g2.toffee) { return -1; } else { return 0; } } } class GeeksforGeeks { public static void main(String[] args) { // Creating a priorityqueue PriorityQueue<Geek> pq = new PriorityQueue<Geek>(new sortBy()); // Adding elements to priorityqueue Geek g1 = new Geek(2, "ram"); Geek g2 = new Geek(5, "shyam"); Geek g3 = new Geek(17, "ramesh"); pq.offer(g1); pq.offer(g2); pq.offer(g3); // Printing original priorityqueue System.out.println("Original PriorityQueue is: "); for (Geek g : pq) { System.out.println(g.name + " " + g.toffee); } // Checking if pq contains object g1 if (pq.contains(g1)) { // Remove geek g1 from priorityqueue pq.remove(g1); // Modify toffee of g1 from 2 to 20 g1.toffee = 20; // Again add g1 to priorityqueue pq.offer(g1); } // Printing modified priorityqueue System.out.println("Modified PriorityQueue is: "); for (Geek g : pq) { System.out.println(g.name + " " + g.toffee); } } }
Original PriorityQueue is: ram 2 shyam 5 ramesh 17 Modified PriorityQueue is: shyam 5 ramesh 17 ram 20
Time Complexity: O(N*logN)
Auxiliary Space: O(N)
Related Articles:
Please Login to comment...