A typical Priority Queue requires following operations to be efficient.
- Get Top Priority Element (Get minimum or maximum)
- Insert an element
- Remove top priority element
- Decrease Key
A Binary Heap supports above operations with following time complexities:
- Finding minimum and maximum are not naturally O(1), but can be easily implemented in O(1) by keeping an extra pointer to minimum or maximum and updating the pointer with insertion and deletion if required. With deletion we can update by finding inorder predecessor or successor.
- Inserting an element is naturally O(Logn)
- Removing maximum or minimum are also O(Logn)
- Decrease key can be done in O(Logn) by doing a deletion followed by insertion. See this for details.
So why is Binary Heap Preferred for Priority Queue?
- Since Binary Heap is implemented using arrays, there is always better locality of reference and operations are more cache friendly.
- Although operations are of same time complexity, constants in Binary Search Tree are higher.
- We can build a Binary Heap in O(n) time. Self Balancing BSTs require O(nLogn) time to construct.
- Binary Heap doesn’t require extra space for pointers.
- Binary Heap is easier to implement.
- There are variations of Binary Heap like Fibonacci Heap that can support insert and decrease-key in Θ(1) time
Is Binary Heap always better?
Although Binary Heap is for Priority Queue, BSTs have their own advantages and the list of advantages is in-fact bigger compared to binary heap.
- Searching an element in self-balancing BST is O(Logn) which is O(n) in Binary Heap.
- We can print all elements of BST in sorted order in O(n) time, but Binary Heap requires O(nLogn) time.
- Floor and ceil can be found in O(Logn) time.
- K’th largest/smallest element be found in O(Logn) time by augmenting tree with an additional field.
This article is contributed by Vivek Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
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.
- Priority Queue using Binary Heap
- 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
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- Convert min Heap to max Heap
- Heap Sort for decreasing order using min heap
- Priority Queue in C++ Standard Template Library (STL)
- Priority Queue | Set 1 (Introduction)
- Applications of Priority Queue
- Priority Queue using Linked List
- Priority Queue using doubly linked list
- Priority queue of pairs in C++ (Ordered by first)
- Priority Queue in Python
- Double ended priority queue
- 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