CS-2001
Data Structures
Fall 2021
Binary Heap
Heap and Priority Queue
2
Motivation
• With queues the order may be summarized by first in, first
out
• Some tasks may be more important or timely than others
– Higher priority
• Priority queues
– Enqueue objects based on priority
– Dequeue that object which has highest priority
If two objects have same priority, they are served as [usually]
FIFO order.
3
Applications Of Priority Queue
• Store packets on network
routers in order of urgency
• Ordering CPU jobs
• Hospital emergency-room
admission processing
• Dijkstra algorithm, Prim’s
algorithm for MST, and A*
algorithms in AI use priority
queue to complete their
operations
The priority of processes in
Windows may be set in the
Windows Task Manager
4
Priority Queue – ADT
• insert (i.e., enqueue)
– Specification of a priority level (0-high, 1,2.. Low)
• deleteMin (i.e., dequeue)
– Returns the current “highest priority” element in the queue
Element with the minimum priority level
– Deletes that element from the queue
• Performance goal is to make the run time of each
operation as close to O(1) as possible
5
Priority Queue – ADT
4
10
19
5 3
13 8
11 22
insert()
deleteMin()
3 Dequeues the next element
with the highest priority
6
Simple Implementations
• Unordered linked list
– Insert – O(1) step 5 2 10 … 3
– deleteMin – O(n) steps
• Ordered linked list
– insert – O(n) steps 2 3 5 … 10
– deleteMin – O(1) step
• Balanced binary tree,
– insert – O(log2 n) steps
– deleteMin in how many steps?
Find min – O(log2 n) steps
Delete – O(log2 n) steps
Can we build a data structure better suited to store and retrieve priorities
7
Binary Heap
8
Binary Heap
• A binary heap is a binary tree with two properties
– Structure property
– Heap-order property
9
Binary Heap – Structure Property
• A binary heap is (almost) complete binary tree
– Each level (except possibly the bottom most level) is
completely filled
– The bottom most level may be partially filled (from left to
right)
N=10
Every level
(except last)
saturated
10
Binary Heap – Heap-Order Property
• Min-Heap property
– Key associated with the root is less than or equal to the keys
associated with either of the sub-trees (if any)
– Both of the sub-trees (if any) are also binary min-heaps
• Properties of min-heap
– A single node is a min-heap
– Minimum key always at root
– For every node X, key(parent(X)) ≤ key(X)
– No relationship between nodes with similar key
11
Binary Heap – Heap-Order Property
• Max-Heap property
– Maximum key at the root
– For every node X, key(parent(X)) ≥ key(X)
• Insert and deleteMin must maintain heap-order property
12
Heap-Order Property – Example
• Min-Heap
Minimum
element
No
relationship
13
Heap Operations – insert
• Insert new element into the heap at the next available slot
(“hole”)
– Maintaining (almost) complete binary tree
• Percolate the element up the heap while heap-order
property not satisfied
14
Heap Insert – Example
• Insert 14
1 hole
4
15
Heap Insert – Example
• Insert 14
(1)
14 vs. 31
14
14 hole
16
Heap Insert – Example
• Insert 14
(2)
14 vs. 21 14
14
17
Heap Insert – Example
• Insert 14
(3)
14 vs. 13
14
Path of percolating
up
Heap order property
Structure property
18
Heap Insert – Example
• Insert 25
19
Heap Insert – Example
• Percolate 25 up into its appropriate location
– The resulting heap is still a complete tree
20
Heap Operation – deleteMin
• Minimum element is always at the root
– Return the element at the root and delete it
• Heap decreases by one in size
• Move last element of the tree into hole at root
• Percolate down while heap-order property is not satisfied
Make this
position
empty
21
deleteMin – Example
Copy 31 temporarily
here and move it down
Make
this Is 31 > min(14,16)?
position - Yes - swap 31 with min(14,16)
empty
22
deleteMin – Example
31
Is 31 > min(19,21)?
- Yes - swap 31 with min(19,21)
23
deleteMin – Example
31
31
Is 31 > min(19,21)? Is 31 > min(65,26)?
- Yes - swap 31 with min(19,21) - Yes - swap 31 with min(65,26)
24
deleteMin – Example
31
25
deleteMin – Example
• deleteMin will dequeue element 12 from the top
26
deleteMin – Example
• Copy the last entry in the heap to the root
27
deleteMin – Example
• Percolate 36 down swapping it with the smallest of its
children
– Halt when both children are larger
28
Array-Based Implementation of Binary Tree
Left child(i) = at position 2i
N=10 Right child(i) = at position 2i
+ 1 i / 2
Parent(i) = at position
Array representation:
i
2i +
1
i / 2 2i
29
Array-Based Implementation Of Binary Heap
• Consider the following heap, both as a tree and in its array
representation
30
Array-Based Implementation – insert
• Inserting 26 requires no changes
i / 2
31
Array-Based Implementation – insert
• Inserting 8 requires a few percolations
– Swap 8 and 23
i / 2
32
Array-Based Implementation – insert
• Swap 8 and 12
33
Array-Based Implementation – insert
• At this point, 8 is greater than its parent, so we are
finished
34
Delete Min
35
Array-Based Implementation – deleteMin
• Removing the top require copy of the last element to the
top
36
Array-Based Implementation – deleteMin
• Percolate down
– Compare Node 1 with its children: Nodes 2 and 3
– Swap 23 and 6
Left child(i) = at position 2i
Right child(i) = at position 2i
+ 1
37
Array-Based Implementation – deleteMin
• Compare Node 2 with its children: Nodes 4 and 5
– Swap 23 and 9
Left child(i) = at position 2i
Right child(i) = at position 2i
+ 1
38
Array-Based Implementation – deleteMin
• Compare Node 4 with its children: Nodes 8 and 9
– Swap 23 and 10
Left child(i) = at position 2i
Right child(i) = at position 2i
+ 1
39
Array-Based Implementation – deleteMin
• The children of Node 8 are beyond the end of the array:
– Stop
Left child(i) = at position 2i
Right child(i) = at position 2i
+ 1
40
Runtime Analysis
• insert operation
– Worst case: Inserting an element less than the root
O(log2 n)
– Best case: Inserting an element greater than any other
element
O(1)
– Average case: O(1)
• deleteMin operation
– Replacing the top element is O(1)
– Percolate down the top object is O(log2 n)
– We copy something that is already in the lowest depth
It will likely be moved back to the lowest depth
41
Building a Heap
• What if all N elements are all available upfront?
– Construct heap from initial set or array of N items
• Solution 1 (insert method)
– Perform N inserts
• Solution 2 (BuildHeap method)
– Randomly populate initial heap with structure property
– Perform a percolate-down(also known as Heapify) on each
internal node to take care of heap order property
42
BuildHeap Example
• Input priority levels
– { 150, 80, 40, 30, 10, 70, 110, 100, 20, 90, 60, 50, 120, 140,
130 }
Leaves are all
valid heaps
(implicitly)
• Arbitrarily assign elements to heap nodes So, let us look at each
• internal node,
Structure property satisfied
from bottom to top,
• Heap order property violated
and fix if necessary
• Leaves are all valid heaps (implicit)
43
BuildHeap Example
Nothing
to do
44
BuildHeap Example
Swap with
left child
45
BuildHeap Example
Nothing
to do
46
BuildHeap Example
Swap
with
right
child
47
BuildHeap Example
Nothing
to do
48
BuildHeap Example
Swap with right
child and then
with 60
49
BuildHeap Example
50
BuildHeap Example
Final Heap
51
Any Question So Far?
52