Lecture 22 Heap
Lecture 22 Heap
11/05/08 1
Heap
A heap is a data structure that stores a
collection of objects (with keys), and has the
following properties:
– Complete Binary tree
– Heap Order
1 1
0 0
0 0
2 1 2 1
1 6 1 7
1 1 1 1 1 1
4 9 7 4 9 6
1
9
1 1
2 6
1 4 7
19 12 16 1 4 7
Array A
What is a Heap?
A heap is also known as a priority queue and
can be represented by a binary tree with the
following properties:
Structure property: A heap is a completely filled binary
tree with the exception of the bottom row, which is filled from
left to right
1 1
2 6
1 4 7
19 12 16 1 4 7
Array A
Min heap example
1
4 16
7 12 19
1 4 16 7 12 19
Array A
Heap
• The root of the tree A[1] and given index i of a node, the indices of
its parent, left child and right child can be computed
PARENT (i)
return floor(i/2)
LEFT (i)
return 2i
RIGHT (i)
return 2i + 1
Array implementation
For any node i, the following formulas apply:
The index of its parent = i / 2
53 44 25 15 21 13 18 3 12 5 7
0 1 2 3 4 5 6 7 8 9 10 11
Heap Implementation
• We can use an array (due to the regular structure or completeness of binary tree).
• Example
1
0
1 2 3 4 5 6 7 8 9 0
10 2 1
21 17 14 19 16
0 1 7
1 1 1
4 9 6
Figure: Heap and Its Array Representation
Insertion
Algorithm
1. Add the new element to the next available position at the
lowest level
2. Restore the max-heap property if violated
General strategy is percolate up (or bubble up):
if the parent of the element is smaller than the
element, then interchange the parent and child.
OR
1 1 1
0 0 2
1 1
8 5 5 5
2 0
1
8 8
2 4
Other children are not affected
1 1 1
2 2 4
1 1 1
5 5 5
0 4 2
1 1 1
8 8 8
4 0 0
1 1 1 1
2 6 2 6
4 7 1 4 7 17
1
Insert 17
1
9
1 1
2 7 swap
1 4 7 16
• Delete min
– Copy the last number to the root ( overwrite the minimum
element stored there ).
– Restore the min heap property by percolate down.
A sample heap
• Here’s a sample binary tree after it has been heapified
2
5
2 1
2 7
1 2 1 1
9 2 4 5
1 1 2 1
3 9
8 4 1 1
1 2 1 1
9 2 4 5
1 1 2 1
3 9
8 4 1 1
• How can we fix the binary tree so it is once again balanced
and left-justified?
• Solution: remove the rightmost leaf at the deepest level
and use it for the new root
The reHeap method I
• Our tree is balanced and left-justified, but no longer a heap
• However, only the root lacks the heap property
1
1
2 1
2 7
1 2 1 1
9 2 4 5
1 1 2
3 9
8 4 1
1 2 1 1
9 2 4 5
1 1 2
3 9
8 4 1
1 1 1 1
9 1 4 5
1 1 2
3 9
8 4 1
1 2 1 1
9 1 4 5
1 1 1
3 9
8 4 1
• While ( new root < its children ) exchange it with the greater of its children.
• Example:
5 2 4 4
0 1 0 0
4 2 4 2 2 2 3 2
0 3 0 3 1 3 0 3
1 3 2 2 1 3 2 1 3 2 1 2 2
8 0 0 1 8 0 0 8 0 0 8 1 0
1. If Left = N and if Last < Heap[Left] then Set Heap[Ptr] := Heap[Left] and Ptr := Left.
3. Return
Heap Sort
• The heapsort algorithm consists of two phases:
- build a heap from an arbitrary array
- use the heap to sort the data
• To sort the elements in the decreasing order, use a min heap
• To sort the elements in the increasing order, use a max heap
1
9
1 1
2 6
1 4 7
Example of Heap Sort
Take out
19
biggest
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
16
biggest
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
Move the last biggest 4
element to the
root
1
Sorted
Array
:
A
1 4 7 12 16 19
Take out
1 biggest
Sorted
Array
:
A
1 4 7 12 16 19
Sorted
:
1 4 7 12 16 19
END!!!