Lab 12 Binary Heaps Implementation
Lab 12 Binary Heaps Implementation
Learning Outcomes:
After successfully completing this lab the students will be able to:
Figure 12.1 shows a binary min heap and a binary max heap. The properties of binary heaps are
given as follows:
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 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.
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 log2n, where n is the number of elements. Heaps (also
known as partially ordered trees) are a very popular data structure for implementing priority queues.
A binary heap is a useful data structure in which elements can be added randomly but only the
element with the highest value is removed in case of max heap and lowest value in case of min
heap. A binary tree is an efficient data structure, but a binary heap is more space efficient and
simpler.
Page 1 of 3
Consider a max heap H with n elements. Inserting a new value into 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.
The first step 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.
Now, as per the second step, let the new value 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.
Deleting an Element from a Binary Heap
Consider a max heap H having n elements. An element is always 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). Here, the value of root node = 54 and the value of the last node = 11. So, replace
54 with 11 and delete the last node.
This is illustrated in Figure 12.3 and 12.4 below.
Page 2 of 3
Figure12. 3: The root node
always gets deleted
Page 3 of 3