Lecture 4: Heaps and Priority Queues
Dr Zhouxiang Fei
Objectives
Learn, understand and implements the following data structures and concepts:
● Priority Queues
● Heap
● Heap Operations
● Heap Sorting
● Complexity of Heap Operations
Priority Queues
A data structure where elements are assigned priorities, and elements with higher
priority are dequeued first.
Examples:
1. Hospital patient management
2. Emergency room triage
3. Air traffic control
Priority Queue
With a set S of elements, each associated with a key,
we have the following main operations:
1. insert element x into set S
2. return element of S with largest (or
smallest) key and remove it from S
Heap
• An implementation of a priority queue
• An array, visualised as a complete binary tree
• Max Heap Property: The key of a node is ≥ than the keys
of its children
• Min Heap Property: The key of a node is ≤ than the keys
of its children
Example of Max Heap Example of Min Heap
Heap
• An implementation of a priority queue
• An array, visualised as a complete binary tree
• Max Heap Property: The key of a node is ≥ than the keys
of its children
• Min Heap Property: The key of a node is ≤ than the keys
of its children
Complete binary tree Complete binary tree Incomplete binary tree Incomplete binary tree
Heap as a Tree
Case 1
• root of tree: first element in the array, corresponding to i = 1
• parent(i) = floor(i/2): returns index of node's parent
• left(i) = 2i: returns index of node's left child
• right(i) = 2i + 1: returns index of node's right child
Heap as a Tree
Case 2
• root of tree: first element in the array, corresponding to i = 0
• parent(i) = floor[(i - 1)/2]: returns index of node's parent
• left(i) = 2i + 1: returns index of node's left child
• right(i) = 2i + 2: returns index of node's right child
Heap as a Tree
Case 2
• root of tree: first element in the array, corresponding to i = 0
• parent(i) = floor[(i - 1)/2]: returns index of node's parent
• left(i) = 2i + 1: returns index of node's left child
• right(i) = 2i + 2: returns index of node's right child
Heap as a Tree
Case 2
• root of tree: first element in the array, corresponding to i = 0
• parent(i) = floor[(i - 1)/2]: returns index of node's parent
• left(i) = 2i + 1: returns index of node's left child
• right(i) = 2i + 2: returns index of node's right child
Height of a Heap
Height of a heap with n nodes is ~log(n) (discuss in detail in seminar)
Insertion in a Heap
Insert. Add node at end, then swim it up.
In a max Heap, if a key becomes larger than its parent's key:
1. Exchange key in child with key in parent
2. Repeat until heap order restored
Insertion in a Heap
Insert. Add node at end, then swim it up.
In a max Heap, if a key becomes larger than its parent's key:
1. Exchange key in child with key in parent
2. Repeat until heap order restored
Complexity of Insertion in Heap
The complexity of Heap insertion is log(n).
Removal in a Heap
Remove the minimum at root. Exchange root with node at end, then sink it down.
In a min Heap, if a key is smaller than its parent's key:
1. Exchange key in parent with key in smaller child
2. Repeat until heap order restored
Removal in a Heap
Remove the minimum at root. Exchange root with node at end, then sink it down.
In a min Heap, if a key is smaller than its parent's key:
1. Exchange key in parent with key in smaller child
2. Repeat until heap order restored
Removal in a Heap
Remove the minimum at root. Exchange root with node at end, then sink it down.
In a min Heap, if a key is smaller than its parent's key:
1. Exchange key in parent with key in smaller child
2. Repeat until heap order restored
Complexity of Removal in Heap
The complexity of Heap removal is log(n).
Heap-Sort
Take maximum as example:
1. Heap construction: build a max-heap with all n keys
2. Sortdown: repeatedly remove the maximum key
Heap-Sort
Take maximum as example:
1. Heap construction: build a max-heap with all n keys
2. Sortdown: repeatedly remove the maximum key
Heap-Sort
Take maximum as example:
1. Heap construction: build a max-heap with all n keys
2. Sortdown: repeatedly remove the maximum key
Heap-Sort
Take maximum as example:
1. Heap construction: build a max-heap with all n keys
2. Sortdown: repeatedly remove the maximum key
Complexity of Heap-Sort
The heap-sort algorithm sorts a list S of n comparable elements in O(n log n) time.
O(n log n) running time of heap-sort is much better than the O(n2 ) running time
for selection-sort and insertion-sort.
Conclusion
We learned about the following data structures, applications, implementations:
Priority Queues
Heap
Heap Operations
Heap Sorting
Complexity of Heap Operations