Title: 308-203A Introduction to Computing II Lecture 10: Heaps
1308-203AIntroduction to Computing IILecture
10 Heaps
Fall Session 2000
2Motivation
- Data structures supporting extraction of max
element - are quite useful, for example
- Priority queues - list of tasks to perform with
priority - (always take highest priority task)
- Event simulators, e.g. video games
- (always simulate the nearest event in the future)
3Heap
Extract-Max()
Heap
Insert(object, key)
4How could we do this?
- Sorted list but recall that Insertion-Sort
- was suboptimal
- Binary tree but binary trees can become
- unbalanced and costly
- A more clever way a special case of binary
trees
5Arrays as Binary Trees
Take an array of n elements A1..n For an
index i e 1 .. n define Parent(i)
i/2 Left-child(i) 2i Right-child(i) 2i
1
6Example (as array)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
7Example (as tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
8Facts
- These trees are always balanced
- Easy to find next leaf to add
- (element n1 in the array)
- Not as flexible as trees built with pointers
9The Heap Property
For any node X with parent PARENT(X)
PARENT(X).key gt X.key
Compare to the Binary Search Tree Property
from last lecture this is a much weaker condition
10Example
16
14
10
8
7
9
3
2
4
1
11Heap-Insert
Heap-Insert(A1..n, k) A.length
An1 k j n1 while (j ? 1 and
Aj gt APARENT(j) swap(A, j,
PARENT(j)) j PARENT(j)
12Heap-Insert Example
16
14
10
8
7
9
3
swap
2
4
1
15
13Heap-Insert Example
16
14
10
swap
15
8
9
3
2
4
1
7
14Heap-Insert Example
16
Done (15 lt 16)
10
15
14
8
9
3
2
4
1
7
15Helper routine Heapify
Given a node X, where Xs children are
heaps, guarantee the heap property for the
ensemble of X and its children
X
Left heap
Right heap
16Heapify
Heapify(A1..n, i) l Left(i) r
Right(i) if (Ai gt Al and Ai gt Ar )
return if (Al gt Ar) swap(Ai,
Al) Heapify(A, l) else swap(Ai,
Al) Heapify(A, l)
17Heapify example
HEAP PROPERTY VIOLATED
6
14
10
8
7
9
3
2
4
1
18Heapify example
Swap with max(14, 6, 10)
14
10
6
8
7
9
3
2
4
1
19Heapify example
Swap with max(8, 6, 7)
14
10
8
7
9
3
6
2
4
1
20Heapify example
Done 6 max(2, 6, 4)
14
10
8
7
9
3
6
2
4
1
21Heap-Extract-Max
Heap-Extract-Max(A1..n) returnValue
A1 A1 An heap.length --
Heapify(A1..(n-1), 1) return
returnValue
22Heap-Extract-Max example
returnValue 16
16
14
10
8
7
9
3
2
4
1
23Heap-Extract-Max example
Replace with An
1
14
10
8
7
9
3
2
4
24Heap-Extract-Max example
Heapify from top
14
10
8
7
9
3
4
2
1
25Order of Growth
All three, proportional to height of tree
O( log n ) O( log n ) O( log n )
Heap-insert Heapify Heap-Extract-Max
26Other useful operations
- Build-Heap convert an unordered array into a
heap - O( n )
- Heap-Sort sort by removing elements from the
heap - until it is empty
- O( n log n )
27Build-Heap
Build-Heap(A1..n) for i ?n/2? downto
1 Heapify(A, i)
28Heap-Sort
Heap-Sort(A1..n) result new
array1..n for i n downto 1 resulti
Heap-Extract-Max (A1..i) return
result
29Any questions?