Define what a binary heap is
Compare and contrast min and max heaps
Implement basic methods on heaps
Understand where heaps are used in the real world and what other data structures can be constructed from heaps
Very similar to a binary search tree, but with some different rules!
In a MaxBinaryHeap, parent nodes are always larger than child nodes. In a MinBinaryHeap, parent nodes are always smaller than child nodes
41
33
39
12
27
18
33
41
18
39
27
12
Value of parent is always greater than children
No Implied Ordering Between Siblings
Binary Heaps are used to implement Priority Queues, which are very commonly used data structures
They are also used quite a bit, with graph traversal algorithms
We'll come back to this!
41
33
39
12
27
18
A LIST/ARRAY
REPRESENTING A HEAP
REPRESENTING A HEAP
Parent
L Child
R Child
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
REPRESENTING A HEAP
Parent
L Child
R Child
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
REPRESENTING A HEAP
Parent
L Child
R Child
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
REPRESENTING A HEAP
Parent
L Child
R Child
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
REPRESENTING A HEAP
Parent
L Child
R Child
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
For any index of an array n...
The left child is stored at 2n + 1
The right child is at 2n + 2
WHAT IF WE HAVE A CHILD NODE AND WANT TO FIND ITS PARENT?
Parent
Child
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
WHAT IF WE HAVE A CHILD NODE AND WANT TO FIND ITS PARENT?
Parent
Child
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
For any child node at index n...
Its parent is at index (n-1)/2
floored
Class Name:
MaxBinaryHeap
Properties:
values = []
41
33
39
12
27
18
55
41
33
39
12
27
18
55
[41,39,33,18,27,12,55]
41
55
39
12
27
18
33
[41,39,33,18,27,12,55]
[41,39,55,18,27,12,33]
55
41
39
12
27
18
33
[41,39,55,18,27,12,33]
[55,39,41,18,27,12,33]
55
41
39
12
27
18
33
41
33
39
12
27
18
The procedure for deleting the root from the heap (effectively extracting the maximum element in a max-heap or the minimum element in a min-heap) and restoring the properties is called down-heap (also known as bubble-down, percolate-down, sift-down, trickle down, heapify-down, cascade-down, and extract-min/max).
41
33
39
12
27
18
[41,39,33,18,27,12]
12
33
39
41
27
18
[12,39,33,18,27]
REMOVED!
[41,39,33,18,27,12]
12
33
39
27
18
[12,39,33,18,27]
[39,12,33,18,27]
39
33
12
27
18
[39,12,33,18,27]
[39,27,33,18,12]
39
33
27
12
18
[39,27,33,18,12]
(also called extractMax)
BUILDING A
PRIORITY QUEUE
WHAT IS A
PRIORITY QUEUE?
A data structure where each element has a priority. Elements with higher priorities are served before elements with lower priorities.
A NAIVE VERSION
priority: 1
priority: 2
priority: 3
priority: 4
priority: 5
Use a list to store all elements
Iterate over the entire thing to find the highest priority element.
NEXT TO
GET HELP
NEXT TO
GET HELP
Class Name:
PriorityQueue
Properties:
values = []
THE SAME AS BEFORE
Class Name:
Node
Properties:
val
priority
BUT ALSO...
val: "walk dog"
priority: 2
val: "pay bill"
priority: 1
val: "go out"
priority: 3
Val doesn't matter.
Heap is constructed using Priority
OUR PRIORITY QUEUE
Converting an array into a MaxBinaryHeap
We can sort an array in O(n log n) time and O(1) space by making it a heap!
12
23
18
44
51
29
Same idea, min values go upwards
Insertion - O(log N)
Removal - O(log N)
Search - O(N)
Suppose we wanted to insert 200
200
For 16 Elements....4 comparisons
REMEMBER THIS DEPRESSING TREE?
NOT POSSIBLE WITH HEAPS!
Binary Heaps are very useful data structures for sorting, and implementing other data structures like priority queues
Binary Heaps are either MaxBinaryHeaps or MinBinaryHeaps with parents either being smaller or larger than their children
With just a little bit of math, we can represent heaps using arrays!