A priority queue.
The most common implementation of a priority queue is the binary heap.
Binary Heap
Week 7
A Binary Heap is a complete binary tree which satisfies the heap two fundamental properties:
A Binary Heap is a complete binary tree which satisfies the heap two fundamental properties:
A Binary Heap is a complete binary tree which satisfies the heap two fundamental properties:
which avoids a child node from having a value smaller than its parent node.
Binary Heaps
Insert (step 1)
Binary Heaps
Insert (step 2)
Binary Heaps
Insert (step 3)
Binary Heaps
What happens when we want to send the most important message?
Binary Heaps
What happens when we want to send the most important message?
ExtractMin (step 1)
Binary Heaps
What happens when we want to send the most important message?
ExtractMin (step 2)
Binary Heaps
What happens when we want to send the most important message?
ExtractMin (step 3)
Binary Heaps
What happens when we want to send the most important message?
ExtractMin (step 4)
Binary Heaps
Maybe when a message hasn't been sent for a while, we want to give it a smaller key and therefore a higher priority.
Binary Heaps
Maybe when a message hasn't been sent for a while, we want to give it a smaller key and therefore a higher priority.
Binary Heaps
Maybe when a message hasn't been sent for a while, we want to give it a smaller key and therefore a higher priority.
DecreaseKey (step 1)
Binary Heaps
Maybe when a message hasn't been sent for a while, we want to give it a smaller key and therefore a higher priority.
DecreaseKey (step 2)
O(1)
O(log n)
O(log n)
O(log n)
For any given element at position i, its:
Initialization and Structure
Inserting a Key
O(log n)
Getting the Minimum
O(1)
Extracting the Minimum
O(log n)
DecreaseKey
O(log n)