Priority Queues and Heaps
Analyzing properties of binary max-heaps.
Kevin Lin, with thanks to many others.
1
Removing the Max
Draw the binary max-heap after removing the max value.
2
Q
6
4
9
5
8
7
3
1
2
Q1: Draw the binary max-heap after removing the max value.
Removing the Max
Draw the binary max-heap after removing the max value.
3
A
6
4
9
5
8
7
3
1
2
Removing the Max
Draw the binary max-heap after removing the max value.
4
A
6
4
1
5
8
7
3
9
2
Removing the Max
Draw the binary max-heap after removing the max value.
5
A
6
4
1
5
8
7
3
2
Removing the Max
Draw the binary max-heap after removing the max value.
6
A
6
4
8
5
1
7
3
2
Removing the Max
Draw the binary max-heap after removing the max value.
7
A
6
4
8
5
7
1
3
2
Third-Largest Value
If the second-largest value in a max-heap is the root’s left child, select all nodes where the third-largest value could be found. Assume all values are distinct.
8
Q
H
I
D
E
F
C
G
Q1: If the second-largest value in a max-heap is the root’s left child, select all nodes where the third-largest value could be found. Assume all values are distinct.
Third-Largest Value
If the second-largest value in a max-heap is the root’s left child, select all nodes where the third-largest value could be found. Assume all values are distinct.
9
A
H
I
D
E
F
C
G
Decreasing Order
If 9 distinct values are inserted into this max-heap in decreasing order, which nodes could represent the following?
Largest value
Median value
Smallest value
10
Q
H
I
D
E
F
C
G
B
A
?: Draw the result of inserting 3, 2, 1 into an empty max-heap.
If 9 distinct values are inserted into this max-heap in decreasing order, which nodes could represent the following?
Q1: Largest value
Q2: Median value
Q3: Smallest value
Decreasing Order
If 9 distinct values are inserted into this max-heap in decreasing order, which nodes could represent the following?
Largest value
Median value
Smallest value
11
A
H
I
D
E
F
C
G
B
A
Unknown Order
If 9 distinct values are inserted into this max-heap in an unknown order, which nodes could represent the following?
Largest value
Median value
Smallest value
12
Q
H
I
D
E
F
C
G
B
A
If 9 distinct values are inserted into this max-heap in an unknown order, which nodes could represent the following?
Q1: Largest value
Q2: Median value
Q3: Smallest value
Unknown Order
If 9 distinct values are inserted into this max-heap in an unknown order, which nodes could represent the following?
Largest value
Median value
Smallest value
13
A
H
I
D
E
F
C
G
B
A
Increasing Order
If distinct values are inserted into this max-heap in increasing order, which nodes could represent the following?
Largest value
Median value
Smallest value
14
Q
H
I
D
E
F
C
G
B
A
?: Draw the result of inserting 1, 2, 3 into an empty max-heap.
If distinct values are inserted into this max-heap in increasing order, which nodes could represent the following?
Q1: Largest value
?: Median value
?: Smallest value
Increasing Order
If distinct values are inserted into this max-heap in increasing order, which nodes could represent the following?
Largest value
A, since the max is always the root.
Median value
G, since F “unseats” E and G “unseats” F.
Smallest value
H, since B “unseats” A, …
15
A
H
I
D
E
F
C
G
B
A