Professional Documents
Culture Documents
Heaps
Heaps
Heaps
Heap
24 31 19 68 6 31 19 68
21 16 21 16
24 31 19 68 24 31 19
65 26 32 65 26 32
Violates heap structural property Violates heap structural property
MaxHeap and non-MaxHeap examples
Violates MaxHeap
68 68
property 65 < 67
65 46 65 46
24 32 23 25 67 32 23 25
70
Not a Heap Not a Heap 30
50 40
21 16
24 31 19 38
19 18 10
15 20 25
2 5 15
Violates heap structural property Violates heap structural property
Array Representation of a Binary Heap
A heap is a dynamic data structure that is represented and
manipulated more efficiently using an array.
Since a heap is a complete binary tree, its node values can be stored
in an array, without any gaps, in a breadth-first order, where:
Value(node i+1) array[ i ], for i > 0
13
21 16
24 31 19 68
13 21 16 24 31 19 68 65 26 32
0 1 2 3 4 5 6 7 8 9
65 26 32
22 17
19 22 14 15
18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11
Notice:
The left child of index i is at index 2*i+1
The right child of index i is at index 2*i+2
Example: the children of node 3 (19) are 7 (18) and 8 (14)
Mapping into an array
26 24 20 18 17 19 13 12 14 11
0 1 2 3 4 5 6 7 8 9
24 20
18 17 19 13
8 8 10 10
10 8 8 5
1 2 3
10 10 12
8 5 12 5 10 5
12 8 8
4
Other children are not affected
12 12 14
10 5 14 5 12 5
8 14 8 10 8 10
24 31 19 68 24 31 19 68
65 26 32 65 26 32 18
Percolate up
13 13
18 16 21 16
Percolate up
24 21 19 68 24 18 19 68
65 26 32 31 65 26 32 31
A sample heap
22 17
19 22 14 15
18 14 21 3 9 11
Removal is easy:
When we remove from a heap, we always remove the
node with the largest key.
Hence, removal is quite easy and has index 0 of the
heap array.
maxNode = heapArray[0];
But tree is then not complete:
But once root is gone, tree is not complete and we
must fill this cell.
Now this becomes interesting… 19
Removal of “maxNode”
Trickle-down:
Then trickle this last node down until it is below a larger
node and above a smaller one.
11/2/2016 20
Removal (Delete)
100
30 60 50 70
82 51
63 70 37 10
43 27 53 34
30 Last node added
Trickle down swapping node w/larger child until node >= children
As we trickle down, the nodes swap out always swapping the larger node
with the node we are trickling down (so as to maintain the larger nodes above…)
70 51
63 53 37 10
43 27 30 34
24 21 23 68 24 21 23 68
65 26 32 31 65 26 32
Replace by value of last node Percolate down
18 18
Percolate down
21 19 31 19
24 31 23 68 24 21 23 68
65 26 32 65 26 32
Removing and replacing the root
0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25
16 4 7 1 12 19
16
4 7
1 12 19
16 16
4 7 4 19
swap
12 19 1 12 7
1
16 19
swap
12 19 12 16
swap
4 7 1 4 7
1
EXAMPLE OF HEAP SORT
Take out biggest
19
12 16
Move the last element
to the root
1 4 7
Sorted:
Array A
12 16 1 4 7 19
7
swap
HEAPIFY()
12 16
1 4
Sorted:
Array A
7 12 16 1 4 19
16
12 7
1 4
Sorted:
Array A
16 12 7 1 4 19
Take out biggest
16
Move the last element
to the root
12 7
1 4
Sorted:
Array A
12 7 1 4 16 19
4
12 7
Sorted:
Array A
4 12 7 1 16 19
swap 4
HEAPIFY()
12 7
Sorted:
Array A
4 12 7 1 16 19
12
4 7
Sorted:
Array A
12 4 7 1 16 19
Take out biggest
12
Move the last
element to the
root 4 7
Sorted:
Array A
4 7 1 12 16 19
1
swap
4 7
Sorted:
Array A
1 4 7 12 16 19
7
4 1
Sorted:
Array A
7 4 1 12 16 19
Take out biggest
7
Move the last
element to the
4 1 root
Sorted:
Array A
1 4 7 12 16 19
swap 1
HEAPIFY()
4
Sorted:
Array A
4 1 7 12 16 19
Take out biggest
Move the last 4
element to the
root
1
Sorted:
Array A
1 4 7 12 16 19
Take out biggest
1
Sorted:
Array A
1 4 7 12 16 19
Sorted:
1 4 7 12 16 19
Heapsort
A trace of heapsort (a – c)
Heapsort
A trace of heapsort (d – f)
Heapsort
A trace of heapsort (g – i)
Heapsort
A trace of heapsort (j – l)