- Every item has a priority associated with it.
- An element with high priority is dequeued before an element with low priority.
- If two elements have the same priority, they are served according to their order in the queue.
A Binary Heap is a Binary Tree with the following properties:
- It is a Complete Tree. This property of Binary Heap makes them suitable to be stored in an array.
- A Binary Heap is either Min Heap or Max Heap.
- In a Min Binary Heap, the key at the root must be minimum among all keys present in Binary Heap. The same property must be recursively true for all nodes in Binary Tree.
- Similarly, in a Max Binary Heap, the key at the root must be maximum among all keys present in Binary Heap. The same property must be recursively true for all nodes in Binary Tree.
Operation on Binary Heap
- insert(p): Inserts a new element with priority p.
- extractMax(): Extracts an element with maximum priority.
- remove(i): Removes an element pointed by an iterator i.
- getMax(): Returns an element with maximum priority.
- changePriority(i, p): Changes the priority of an element pointed by i to p.
Example of A Binary Max Heap
- Suppose below is the given Binary Heap that follows all the properties of Binary Max Heap.
- Now a node with value 32 need to be insert in the above heap: To insert an element, attach the new element to any leaf. For Example A node with priority 32 can be added to the leaf of the node 11. But this violates the heap property. To maintain the heap property, shift up the new node 32.
- Shift Up Operation get node with 32 at the correct position: Swap the incorrectly placed node with its parent until the heap property is satisfied. For Example: As node 11 is less than node 32 so, swap node 11 and node 32. Then, swap node 14 and node 32. And at last, swap node 31 and node 32.
- ExtractMax: The maximum value is stored at the root of the tree. But the root of the tree cannot be directly removed. First, it is replaced with any one of the leaves and then removed. For Example: To remove Node 45, it is first replaced with node 11. But this violates the heap property, so move the replaced node down. For that, use shift down operation.
- ShiftDown operation: Swap the incorrectly placed node with a larger child until the heap property is satisfied. For Example: Node 11 is swapped with node 32 then, with node 31 and in last it is swapped with node 14.
- ChangePriority: Let the changed element shift up or down depending on whether its priority decreased or increased. For Example: Change the priority of nodes 11 to 35, due to this change the node has to shift up the node in order to maintain the heap property.
- Remove: To remove an element, change its priority to a value larger than the current maximum, then shift it up, and then extract it using extract max. Find the current maximum using getMax.
- GetMax: The max value is stored at the root of the tree. To getmax, just return the value at the root of the tree.
Array Representation of Binary Heap
Since the heap is maintained in form of a complete binary tree, because of this fact the heap can be represented in the form of an array. To keep the tree complete and shallow, while inserting a new element insert it in the leftmost vacant position in the last level i.e., at the end of our array. Similarly, while extracting maximum replace the root with the last leaf at the last level i.e., the last element of the array. Below is the illustration of the same:
Below is the program to implement Priority Queue using Binary Heap:
Priority Queue : 45 31 14 13 20 7 11 12 7
Node with maximum priority : 45
Priority queue after extracting maximum : 31 20 14 13 7 7 11 12
Priority queue after priority change : 49 20 31 13 7 7 11 12
Priority queue after removing the element : 49 20 31 12 7 7 11
Time Complexity: The time complexity of all the operation is O(log N) except for GetMax() which has time complexity of O(1).
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Why is Binary Heap Preferred over BST for Priority Queue?
- How to implement stack using priority queue or heap?
- Heap and Priority Queue using heapq module in Python
- Priority Queue using Queue and Heapdict module in Python
- Difference between Circular Queue and Priority Queue
- Heap Sort for decreasing order using min heap
- Priority Queue using Linked List
- Priority Queue using doubly linked list
- Implementation of Non-Preemptive Shortest Job First using Priority Queue
- Huffman Coding using Priority Queue
- Find the K closest points to origin using Priority Queue
- Convert min Heap to max Heap
- Priority Queue | Set 1 (Introduction)
- Applications of Priority Queue
- STL Priority Queue for Structure or Class
- Does STL priority queue allow duplicate values?
- Priority queue of pairs in C++ (Ordered by first)
- Priority Queue in Python
- Double ended priority queue
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.