Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

Welcome to CS221: Programming & Data Structures

Binary Heaps, Heap Sort Algorithm


• Topics covered are: Binary Heaps, Heap Sort algorithm
Note:
• The slides are adapted from the textbook “Data Structures Using C” by Reema Theraja, Oxford University Press
• A few of the slides on ECE 250 Algorithms and Data Structures, Dept. of Electrical and Computer Engineering, University of Waterloo, Ontario, Canada were adapted.
Binary Heaps Min heap

• A binary heap is a complete binary tree in which every node satisfies


the heap property which states that: 4
If B is a child of A, then key(A) ≥ key(B)
• This implies that elements at every node will be either greater than 6 12
or equal to the element at its left and right child. Thus, the root node
has the highest key value in the heap. Such a heap is commonly
known as a max-heap.
7
• Alternatively, elements at every node will be either less than or 9 21 39
equal to the element at its left and right child. Thus, the root has the
lowest key value. Such a heap is called a min-heap.
13
19 10
The properties of binary heaps are given as follows:
• Since a heap is defined as a complete binary tree, all its elements can be Max heap
stored sequentially in an array. It follows the same rules as that of a
complete binary tree. That is, if an element is at position i in the array,
then its left child is stored at position 2i and its right child at position 2i+1.
Conversely, an element at position i has its parent stored at position i/2. 99
• Being a complete binary tree, all the levels of the tree except the last level
are completely filled.
• The height of a binary tree is given as
elements.
n, where n is the number of 69 72
• Heaps (also known as partially ordered trees) are a very popular data
structure for implementing priority queues
45
63 21 39
27
36 54
Inserting a New Element in a Binary Heap
Consider a max heap H with n elements. Inserting a new value into Algorithm to insert an element in a max heap
the heap is done in the following two steps:
1. Add the new value at the bottom of H in such a way that H is
still a complete binary tree but not necessarily a heap.
2. Let the new value rise to its appropriate place in H so that H
now becomes a heap as well.
To do this, compare the new value with its parent to check if
they are in the correct order. If they are, then the procedure halts,
else the new value and its parent’s value are swapped and Step 2 is
repeated.
Example: Consider the max heap given in Fig. 1 and insert 99 in it.

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:

This array represents the following complete tree:

This is neither a min-heap, max-heap, or binary search tree


In-place Heapification
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

The formulas are now:


Children 2*k + 1 2*k + 2
Parent (k + 1)/2 - 1
In-place Heapification
Can we convert this complete tree into a max heap?

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

None of the leaf nodes need to


be percolated down, and the first
non-leaf node is in position n/2

Thus we start with position 10/2 = 5


Example Heap Sort: Phase 1
We compare 3 with its child and swap them

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?

Repeat this process: pop the maximum element, and then


insert it at the end of the array:
Heap Sort Example : Phase 2
Repeat this process
• Pop and append 70

• Pop and append 63


Heap Sort Example : Phase 2
We have the 4 largest elements in order
• Pop and append 52

• Pop and append 46


Heap Sort Example : Phase 2
Continuing...
• Pop and append 34

• Pop and append 28


Heap Sort Example : Phase 2
Finally, we can pop 17, insert it into the 2nd location, and the
resulting array is sorted
Heap Sort
Heapification runs in Q(n)

Popping n items from a heap of size n, as we saw, runs in Q(n


ln(n)) time
• We are only making one additional copy into the blank left at the end of
the array

Therefore, the total algorithm will run in Q(n ln(n)) time


Heap Sort
There are no worst-case scenarios for heap sort
• Dequeuing from the heap will always require the same number of
operations regardless of the distribution of values in the heap

There is one best case: if all the entries are identical, then the
run time is Q(n)

The original order may speed up the heapification, however, this


would only speed up an Q(n) portion of the algorithm
Run-time Summary
The following table summarizes the run-times of heap sort
Case Run Time Comments
Worst Q(n ln(n)) No worst case
Average Q(n ln(n))
Best Q(n) All or most entries are the same
Solve:
1. Sort the following 12 entries using heap sort :34, 15, 65, 59, 79, 42, 40, 80, 50, 61, 23, 46.
2. A heap sequence is given as: 52, 32, 42, 22, 12, 27, 37, 12, 7. Which element will be deleted when the deletion algorithm is called
thrice? Show the resulting heap when values 35, 24, and 10 are added to the heap.
3. Given the following array structure, draw the heap.

Also, find out (a) the parent of nodes 10, 21, and 23, and
(b) index of left and right child of node 23.

You might also like