Heap Structure
Heap Structure
Heaps
• A binary heap is a binary tree (NOT a BST) that is:
• Complete: the tree is completely filled except possibly the bottom
level, which is filled from left to right
• Satisfies the heap order property
• The ordering can be one of two types:
• min-heap property: the value of each node is greater than or equal
to the value of its parent, with the minimum-value element at the
root.
• max-heap property: the value of each node is less than or equal to
the value of its parent, with the maximum-value element at the root.
Example
5 7
5 7
6 2 1
4 2 5
tree 1 tree 2
5 7
2 1
tree 3
Binary heap vs Binary search tree
Consider the figure given below and state whether
it is a heap or not.
Array representation of heap
• Since a heap is defined as a complete binary tree, all its elements can
be stored sequentially in an array.
• It follows the same rules of complete binary tree.
• That is, if an element at position i in the array, has its left child stored
at position 2i and its right child at position 2i + 1.
• Conversely, an element at position i have its position stored at
position i/2.
Simplifying things
• Place the root at array index 1, its left child at index 2, its right child at index
3, so on and so forth…
53
44 25
15 21 13 18
3 12 5 7
53 44 25 15 21 13 18 3 12 5 7
0 1 2 3 4 5 6 7 8 9 10 11
53
44 25
15 21 13 18
3 12 5 7
53 44 25 15 21 13 18 3 12 5 7
0 1 2 3 4 5 6 7 8 9 10 11
• Priority queues
13
Building a Heap
• Convert an array A[1 … n] into a max-heap (n = length[A])
• The elements in the subarray A[(n/2+1) .. n] are leaves
• Apply MAX-HEAPIFY on elements between 1 and n/2
Alg: BUILD-MAX-HEAP(A) 1
4
1. n = length[A]
2 3
2. for i ← n/2 downto 1 1 3
4 5 6 7
3. do MAX-HEAPIFY(A, i, n) 8
2 9 10
16 9 10
14 8 7
A: 4 1 3 2 16 9 10 14 8 7
14
Example: A 4 1 3 2 16 9 10 14 8 7
4 4 4
2 3 2 3 2 3
1 3 1 3 1 3
4 5 6 7 4 5 6 7 4 5 6 7
8
2 9 10
16 9 10 8 2 9 10
16 9 10 8 14 9 10
16 9 10
14 8 7 14 8 7 2 8 7
i=2 i=1
1 1 1
4 4 16
2 3 2 3 2 3
1 10 16 10 14 10
4 5 6 7 4 5 6 7 4 5 6 7
8
14 9 10
16 9 3 8
14 9 10
7 9 3 8
8 9 10
7 9 3
2 8 7 2 8 1 2 4 1
15
Maintaining the Heap Property
• Suppose a node is smaller than a child
• Left and Right subtrees of i are max-heaps
• To eliminate the violation:
• Exchange with larger child
• Move down the tree
• Continue until node is not smaller than
children
16
Example
MAX-HEAPIFY(A, 2, 10)
A[2] A[4]
A[2] violates the heap property A[4] violates the heap property
A[4] A[9]
24
Heapsort
• Goal:
• Sort an array using heap representations
• Idea:
• Build a max-heap from the array
• Swap the root (the maximum element) with the last
element in the array
• “Discard” this last node by decreasing the heap size
• Call MAX-HEAPIFY on the new root
• Repeat this process until only one node remains
25
Example: A=[7, 4, 3, 1, 2]
MAX-HEAPIFY(A, 1, 1)
26
Alg: HEAPSORT(A)
1. BUILD-MAX-HEAP(A)
2. for i ← length[A] downto 2
3. do exchange A[1] ↔ A[i]
4. MAX-HEAPIFY(A, 1, i - 1)
27
Problems
• Demonstrate, step by step, the operation of Build-Heap on the array
and perform sorting using heapsort
A=[5, 3, 17, 10, 84, 19, 6, 22, 9]
28