Binary Heap - Fresno State Email - California State University, Fresno

California State University, Fresno
Introduction to Data Structure
Chapter 14
Ming Li
Department of Computer Science
California State University, Fresno
Fall 2006
Introduction to Data Structure, Fall 2006 Slide- 1
California State University, Fresno
Binary Heap
Binary heaps are a particularly simple kind of heap data structure
created using a binary tree. It can be seen as a binary tree with two
additional constraints:
The shape property: the tree is either a perfectly balanced binary tree (all
leaves are at the same level), or, if the last level of the tree is not
complete, the nodes are filled from left to right.
The heap property: each node is greater than or equal to each of its
children according to some comparison predicate which is fixed for
the entire data structure.
Introduction to Data Structure, Fall 2006 Slide- 2
California State University, Fresno
Maximum and Minimum Heaps Example
55
40
52
50
10
25
20
11
15
5
30
10
22
(A) Maximum Heap (9 nodes)
(B) Maximum Heap (4 nodes)
Introduction to Data Structure, Fall 2006 Slide- 3
California State University, Fresno
Maximum and Minimum Heaps Example
10
5
11
25
15
50
10
20
52
55
30
40
22
(C) Minimum Heap (9 nodes)
(D) Minimum Heap (4 nodes)
Introduction to Data Structure, Fall 2006 Slide- 4
California State University, Fresno
Example of Heap Before and After
Insertion of 50
63
63
v[0]
v[0]
30
40
30
v[2]
v[1]
25
10
v[4]
v[3]
5
3
18
v[7]
v[8]
v[9]
(a)
8
v[5]
40
v[2]
v[1]
38
25
10
v[6]
8
v[4]
v[3]
v[5]
5
3
18
50
v[7]
v[8]
v[9]
v[10]
38
v[6]
(b)
Introduction to Data Structure, Fall 2006 Slide- 5
California State University, Fresno
Reorder the tree in pushHeap()
63
63
63
v[0]
...
30
...
v[0]
v[1]
...
50
v[9]
v[0]
30
...
v[1]
30
v[4]
25
v[10]
Step 1 Compare 50 and 25
(Exchange v[10] and v[4])
18
v[9]
...
50
v[1]
v[4]
18
...
50
v[4]
25
v[10]
Step 2 Compare 50 and 30
(Exchange v[4] and v[1])
18
25
v[9]
v[10]
Step 3 Compare 50 and 63
(50 in correct location)
Introduction to Data Structure, Fall 2006 Slide- 6
California State University, Fresno
Exchanging elements in popHeap()
63
18
v[0]
v[0]
30
40
30
40
v[1]
v[2]
v[1]
v[2]
25
10
v[4]
v[3]
5
3
18
v[7]
v[8]
v[9]
8
38
10
v[5]
v[6]
v[3]
Before a deletion
25
v[4]
5
3
63
v[7]
v[8]
v[9]
8
38
v[5]
v[6]
After exchanging the root
and last element in the heap
Introduction to Data Structure, Fall 2006 Slide- 7
California State University, Fresno
Adjusting the heap for popHeap()
...
40
40
v[0]
v[0]
...
18
38
v[2]
v[2]
8
38
8
v[5]
v[6]
v[5]
Step 1: Exchange 18 and 40
18
v[6]
Step 2: Exchange 18 and 38
Introduction to Data Structure, Fall 2006 Slide- 8
California State University, Fresno
Heap Operations - POP
96
47
83
10
32
21
17
22
5
2
10
Introduction to Data Structure, Fall 2006 Slide- 9
California State University, Fresno
Heap Operations - PUSH
96
47
83
32
21
17
75
22
10
5
2
75
Introduction to Data Structure, Fall 2006 Slide- 10
California State University, Fresno
Heap Sort
Sort a list of objects by using a heap data structure.
• All elements to be sorted are inserted into a heap
• The heap organizes the elements added to it in such a way that
either the largest value or the smallest value can be quickly
extracted.
• This gives us the elements in order.
More info: http://en.wikipedia.org/wiki/Heapsort
Introduction to Data Structure, Fall 2006 Slide- 11