Professional Documents
Culture Documents
Note:
A max-heap has the same definition except that the
Key of every node is >= the keys of the children
Example of a Min-heap
5
8 20
15 11 30 27
33 16 18 12
Operations on Heaps
Applications of Heaps:
• A heap implements a priority queue, which is a queue
that orders entities not a on first-come first-serve basis,
but on a priority basis: the item of highest priority is at
the head, and the item of the lowest priority is at the tail
5
8 20 8 20
15 11 30 27 15 11 30 27
16 18 12 33 16 18 12
33
12 12
8 20 20
8
15 11 30 27 30
15 11 27
33 16 18 16 18
33
Restore Heap
15 11 30 27 15 11 30 27
33 16 18 33 16 18
8
11 20
15 12 30 27
left-filled
15 9 7 20
16 25 14 12 11 8
What are Heaps Useful for?
To implement priority queues
Priority queue = a queue where all elements
have a “priority” associated with them
Remove in a priority queue removes the
element with the smallest priority
insert
removeMin
Heap or Not a Heap?
Heap Properties
A heap T storing n keys has height h = log(n + 1),
which is O(log n)
5 6
15 9 7 20
16 25 14 12 11 8
Heap Insertion
Insert 6
Heap Insertion
Add key in next available position
Heap Insertion
Begin Unheap
Heap Insertion
Heap Insertion
Terminate unheap when
reach root
key child is greater than key parent
Heap Removal
Remove element
from priority queues?
removeMin( )
Heap Removal
Begin downheap
Heap Removal
Heap Removal
Heap Removal
Terminate downheap when
reach leaf level
key parent is greater than key child
Building a Heap
h=1
h=1
h=3
h=2
Complete Binary Trees
Note: definition in
book is incorrect
h=4
Heaps
Two key properties
Complete binary tree
Value at node
Smaller than or equal to values in subtrees
Example heap
XY X
XZ
Y Z
Heap & Non-heap Examples
5
5 8
5 45 6 45
5 2 2
6 22 6 22 6 22
25 8 45 25 8 45 25
Heaps Non-heaps
Heap Properties
Key operations
Insert ( X )
getSmallest ( )
Key applications
Heapsort
Priority queue
Heap Operations – Insert( X )
Algorithm
1. Add X to end of tree
2. While (X < parent)
Swap X with parent // X bubbles up tree
Complexity
# of swaps proportional to height of tree
O( log(n) )
Heap Insert Example
Insert ( 20 )
10 10 10
30 25 30 25 20 25
37 37 20 37 30
10 10 10 8
30 25 30 25 8 25 10 25
37 37 8 37 30 37 30
Complexity
# swaps proportional to height of tree
O( log(n) )
Heap GetSmallest Example
getSmallest ()
8 30 10
10 25 10 25 30 25
37 30 37 37
8 37 10 10
10 25 10 25 37 25 30 25
30 37 30 30 37
Algorithm
1. Create heap
2. Insert values in heap
3. Remove values from heap (in ascending order)
Complexity
O( nlog(n))
Heapsort Example
Input
11, 5, 13, 6, 1
View heap during insert, removal
As tree
As array
Heapsort – Insert Values
Heapsort – Remove Values
Heapsort – Insert in to Array 1
Input
11, 5, 13, 6, 1
Index = 0 1 2 3 4
Insert 11 11
Heapsort – Insert in to Array 2
Input
11, 5, 13, 6, 1
Index = 0 1 2 3 4
Insert 5 11 5
Swap 5 11
Heapsort – Insert in to Array 3
Input
11, 5, 13, 6, 1
Index = 0 1 2 3 4
Insert 13 5 11 13
Heapsort – Insert in to Array 4
Input
11, 5, 13, 6, 1
Index = 0 1 2 3 4
Insert 6 5 11 13 6
Swap 5 6 13 11
…
Heapsort – Remove from Array 1
Input
11, 5, 13, 6, 1
Index = 0 1 2 3 4
Remove root 1 5 13 11 6
Replace 6 5 13 11
Swap w/ child 5 6 13 11
Heapsort – Remove from Array 2
Input
11, 5, 13, 6, 1
Index = 0 1 2 3 4
Remove root 5 6 13 11
Replace 11 6 13
Swap w/ child 6 11 13
Heap Application – Priority Queue
Queue
Linear data structure Enqueue Dequeue
First-in First-out (FIFO)
Implement as array / linked list
Priority queue
Elements are assigned priority value
Higher priority elements are taken out first
Equal priority elements are taken out in FIFO order
Implement as heap
Priority Queue
Properties
Lower value = higher priority
Heap keeps highest priority items in front
Complexity
Enqueue (insert) = O( log(n) )
Dequeue (remove) = O( log(n) )
For any heap
Heap vs Binary Tree
Binary tree
Keeps values in sorted order
Find any value
O( log(n) ) for balanced tree
O( n ) for degenerate tree (worst case)
Heap
Keeps smaller values in front
Find minimum value
O( log(n) ) for any heap
Can also organize heap to find maximum value
Keep largest value in front