A Binary Heap is a common way to implement a Priority Queue.
This is because:
- It has easy access to the root node which can be the
min
ormax
depending on implementation - Heaps are usually implemented as an array, which guarantees cache friendliness
- It is easy to
insert
,search
anddelete
inside of cheaply, in worst case
Implementation
A min
binary heap is an array as follows:
0 | 1 | 2 | 3 | 4 | 5 |
---|---|---|---|---|---|
1 | 3 | 6 | 5 | 9 | 8 |