Professional Documents
Culture Documents
Binary Heaps, Heap Sort Algorithm: Welcome To CS221: Programming & Data Structures
Binary Heaps, Heap Sort Algorithm: Welcome To CS221: Programming & Data Structures
54 54
Insert 99
45 36 45 36
27 21 18 21 27 21 18 21
11 11 99
Fig. 1. Existing Binary heap
Fig 2. Binary heap after insertion of 99
The 1st step of the algorithm given in slide no 3 says that insert the element in the heap so that the heap is
a complete binary tree. So, insert the new value as the right child of node 27 in the heap. This is illustrated
in Fig. 2
Example contd.
54 54
Heapify
45 36 45 36
27 21 18 21 99 21 18 21
11 99 11 27
Fig 2. Fig 3.
Now, as per the 2nd step of the algorithm, let the new value i.e. 99 rise to its appropriate place in H so that
H becomes a heap as well. Compare 99 with its parent node value. If it is less than its parent’s value, then
the new node is in its appropriate place and H is a heap. If the new value is greater than that of its parent’s
node, then swap the two values. Repeat the whole process until H becomes a heap. This process is
termed percolating down. This is illustrated in Fig. 3, 4 and 5.
Example contd.
54 54
Heapify
45 36 99 36
99 21 18 21 45 21 18 21
11 27 11 27
Fig 3. Fig 4.
Example contd.
54 99
Heapify
99 36 54 36
45 21 18 21 45 21 18 21
11 27 11 27
Fig 4. Fig 5.
Example: Build a max heap by using the
algorithm given in slide no 3.
Build a max heap H from the set of numbers given below. Also draw the memory representation of the heap.
Step 1
45
Example: Build a max heap
Step 2
45
36
Example: Build a max heap
Step 3
45
36 54
Example: Build a max heap
Step 4
54
36 45
Example: Build a max heap
Step 5
54
36 45
27
Example: Build a max heap
Step 6
54
36 45
27 63
Example: Build a max heap
Step 7
54
63 45
27 36
Example: Build a max heap
Step 8
63
54 45
27 36
Example: Build a max heap
Step 9
63
54 45
27 36 72
Example: Build a max heap
Step 10
63
54 72
27 36 45
Example: Build a max heap
The memory representation of binary heap H.
Deleting an Element from a Binary Heap
Consider a max heap H having n elements. An element is always Algorithm to delete the root element from a max heap
deleted from the root of the heap. So, deleting an element from
the heap is done in the following three steps:
1. Replace the root node’s value with the last node’s value so that
H is still a complete binary tree but not necessarily a heap.
2. Delete the last node.
3. Sink down the new root node’s value so that H satisfies the
heap property. In this step, interchange the root node’s value
with its child node’s value (whichever is largest among its
children).
Example: Delete the root node containing
value 54 from Binary heap.
Step 1
54 Value of root=54 11
Value of last node=11
Replace 54 with 11 and
delete the last node
45 36 45 36
27 29 18 21 27 29 18 21
11
Fig 1. Existing Binary heap Fig 2.
Example: Delete the root node containing
value 54 from Binary heap.
Step 1 Step 3
11 45
Since 11 is less than 45,
interchange the values
45 36 11 36
27 29 18 21 27 29 18 21
Fig 2. Fig 3.
Example: Delete the root node containing
value 54 from Binary heap.
Step 3 Step 4
45 45
Since 11 is less than 29,
interchange the values
11 36 29 36
27 29 18 21 27 11 18 21
Fig 3. Fig 4.
Application of binary heap: Heap Sort
Algorithm for heap sort
Given an array ARR with n elements, the heap sort algorithm can
be used to sort ARR in two phases:
1. In phase 1, build a heap H using the elements of ARR.
2. In phase 2, repeatedly delete the root element of the heap
formed in phase 1.
In a max heap, we know that the largest value in H is always
present at the root node. So in phase 2, when the root element is
deleted, we are actually collecting the elements of ARR in
decreasing order
Heap Sort example
Now, consider this unsorted array:
Additionally, because arrays start at 0 (we started at entry 1 for binary heaps) , we
need different formulas for the children and parent
Restriction:
• The operation must be done in-place
In-place Heapification
Two strategies:
• Assume 46 is a max-heap and keep inserting the next element into the
existing heap (similar to the strategy for insertion sort)
• Start from the back: note that all leaf nodes are already max heaps, and then
make corrections so that previous nodes also form max heaps
Example Heap Sort: Phase 1
Let us look at this example: we must convert the unordered
array with n = 10 elements into a max-heap
46
52 28
17 95 63 34
81 70 3
Example Heap Sort: Phase 1
We compare 17 with its two children and swap it with the
maximum child (70)
46
52 28
81 95 63 34
17 70 3
Example Heap Sort: Phase 1
We compare 28 with its two children, 63 and 34, and swap it
with the largest child
46
52 63
81 95 28 34
17 70 3
Example Heap Sort: Phase 1
We compare 52 with its children, swap it with the largest
• Recursing, no further swaps are needed
46
46
52 63 95 63
81 95 28 34 81 52 28 34
17 70 3
17 70 3
Example Heap Sort: Phase 1
Finally, we swap the root with its largest child, and recurse,
swapping 46 again with 81, and then again with 70
46 95
95 63 46 63
81 52 28 34 81 52 28 34
17 70 3 17 70 3
95 95
81 81 63
63
70 46 52 28 34
52 28 34
46 3 17 70 3
17
Heap Sort Example: Phase 1
We have now converted the unsorted array
into a max-heap:
Heap Sort Example: Phase 2
Suppose we pop the maximum element of this heap
This leaves a gap at the back of the array: Please check the
method of deleting
the root node from
a Binary Heap in the
previous slides.
Heap Sort Example : Phase 2
This is the last entry in the array, so why not fill it with the
largest element?
There is one best case: if all the entries are identical, then the
run time is Q(n)
Also, find out (a) the parent of nodes 10, 21, and 23, and
(b) index of left and right child of node 23.