Sei sulla pagina 1di 18

Last updated on Sep 8, 2013 7:13 AM

Copyright 2013 Kevin Wayne


http: //www. cs. pri nceton. edu/~wayne/kl ei nberg-tardos
DATA STRUCTURES II
!
heapify demo
ROBERT SEDGEWICK | KEVIN WAYNE
F O U R T H E D I T I O N
Algorithms
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
8 12 9 7 22 3 26 14 11 15 22
1 2 3 4 5 6 7 8 9 10 11
5
10 11
9
22 3 26 7
14 11 15 22
12
8
8 9
4 7 6
3 2
1
we assume array entries are indexed 1 to N
array in arbitrary order
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
1-node heaps
8 12 9 7 22 3 26 14 11 15 22
9
22 3 26 7
14 11 15 22
12
8
8 9 10 11
10 11 8 9
6 7
7 6
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 5
5
8 12 9 7 22 3 26 14 11 15 22
5
9
22 3 26 7
14 11 15 22
12
8
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 5
5
10
9
22
3 26 7
14 11
15
22
12
8
8 12 9 7 15 3 26 14 11 22 22
5 10
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 5
9
22
3 26 7
14 11
15
22
12
8
8 12 9 7 15 3 26 14 11 22 22
3-node heap
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 4
9
22
3 26 7
14 11
15
22
12
8
8 12 9 7 15 3 26 14 11 22 22
4
4
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 4
9
22
3 26 7
14 11
15
22
12
8
8 12 9 7 15 3 26 14 11 22 22
3-node heap
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 3
9
22
3 26 7
14 11
15
22
12
8
8 12 9 7 15 3 26 14 11 22 22
3
3
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 3
9
22
3
26 7
14 11
15
22
12
8
8 12 3 7 15 9 26 14 11 22 22
3 6
6
3
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 3
9
22
3
26 7
14 11
15
22
12
8
8 12 3 7 15 26 26 14 11 22 22
3-node heap
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 2
9
22
3
26 7
14 11
15
22
12
8
8 12 3 7 15 9 26 14 11 22 22
2
2
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
9
22
3
26
7
14 11
15
22
12
8
8 7 3 12 15 9 26 14 11 22 22
4
2
2 4
sink 2
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
9
22
3
26
7
14
11 15
22 12
8
8 7 3 11 15 9 26 14 12 22 22
9
4
2
2 4 9
sink 2
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 2
9
22
3
26
7
14
11 15
22 12
8
8 7 3 11 15 9 26 14 12 22 22
7-node heap
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 1
9
22
3
26
7
14
11 15
22 12
8
8 7 3 11 15 9 26 14 12 22 22
1
1
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
sink 1
9
22
3
26
7
14
11 15
22 12
8
3 7 8 11 15 9 26 14 12 22 22
1 3
3
1
Heapify. For each element in reverse-array order, sink it down.
Heapify demo
9
22
3
26
7
14
11 15
22 12
8
3 7 8 11 15 9 26 14 12 22 22
11-node heap
array is heap ordered

Potrebbero piacerti anche