Académique Documents
Professionnel Documents
Culture Documents
Lecture No. 31
___________________________________________________________________
Data Structures
Lecture No. 31
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 6
6.3.3, 6.3.4
Summary
BuildHeap
Other Heap methods
C++ Code
BuildHeap
In the previous lecture, we discussed about the BuildHeap method of heap abstract
data structure. In this handout, you are going to know why a programmer adopts this
method. Suppose we have some data that can be numbers or characters or in some
other form and want to build a min-Heap or max-Heap out of it. One way is to use the
insert() method to build the heap by inserting elements one by one. In this method, the
heap property will be maintained. However, the analysis shows that it is NlogN
algorithm i.e. the time required for this will be proportional to NlogN. Secondly, if we
have all the data ready, then it will be better to build a heap at once as it is a better
option than NlogN.
In the delete procedure, when we delete the root, a new element takes the place of
root. In case of min-heap, the minimum value will come up and the larger elements
will move downwards. In this regard, percolate procedures may be of great help. The
percolateDown procedure will move the smaller value up and bigger value down.
This way, we will create the heap at once, without calling the insert() method
internally. Lets revisit it again:
Suppose, there are N data elements (also called as N Keys). We will put this data in an
array and call it as a binary tree. As discussed earlier, a complete binary tree can be
stored in an array. We have a tree in an array but it is not a heap yet. It is not necessary
that the heap property is satisfied. In the next step, we apply the algorithm. Why did
we start the i from N/2? Lets apply this algorithm on the actual data. We will use the
diagram to understand the BuildHeap. Consider the diagram given below:
65
2 31
3 32
4 26
8 13
5 21
9 24 10 15
6 19
11 14
12 16
7 68
13 5 14 70
15 12
i
65 31 32 26 21 19 68 13 24 15 14 16 5 70 12
0
9 10 11 12 13 14 15
In the above diagram, there is an array containing the elements as 65, 31, 32, 26, 21,
19, 68, 13, 24, 15, 14, 16, 5, 70, 12. We have all the data in this array. The zeroth
element of the array is empty. We kept it empty intentionally to apply the 2i, 2i+1
scheme. We will take this array as a complete binary tree. Lets take the first element
that is 65, applying the 2i and 2i+1 formula. This element has 31 and 32 as its
children. Take the second element i.e. 32 and use the formula 2i and 2i+1. Its children
are 26 and 21. We can take the remaining elements one by one to build the tree. The
tree is shown in the above figure. This tree is not a min-heap as it has 65 as root
element while there are smaller elements like 13, 15, 5 as its children. So, this being a
binary tree has been stored in an array.
Lets think about the above formula, written for the heap building. What will be the
initial value of i. As the value of N is 15, so the value of i (i=N/2) will be 7 (integer
division). The first call to percolateDown will be made with the help of the value of i
as 7. The element at 7th position is 68. It will take this node as a root of subtree and
make this subtree a minimum heap. In this case, the left child of node 68 is 70 while
the right child of node 68 is 12. The element 68 will come down while the element 12
moves up.
65
2 31
3 32
4 26
8 13
5 21
9 24 10 15
6 19
12 16
11 14
7 12
13 5 14 70
15 68
i
65 31 32 26 21 19 12 13 24 15 14 16 5 70 68
0
9 10 11 12 13 14 15
Look at this subtree with 12 as the root node. Its left and right children are smaller
than root. This subtree is a minimum heap. Now in the loop, the value of i is
decreased by 1, coming to the mark of 6. The element at 6 th position is 19 that has
been passed to percolateDown method. It will convert this small tree into a minimum
heap. The element 19 is greater than both of its children i.e.16 and 5. One of these will
move up. Which node should move up? The node 5 will move up while node 19 will
move down. In the next repetition, the value of i becomes 5, i.e. the element 21. We
apply the percolateDown() method again and convert it into minimum heap. Now the
subtree with node 26 will be converted into a minimum heap.
65
2 31
3 32
4 13
8 26
5 14
9 24 10 15
6 5
11 21
12 16
i
7 12
13 19 14 70
15 68
i
65 31 32 13 14 5 12 26 24 15 21 16 19 70 68
0
9 10 11 12 13 14 15
2 13
3 12
4 24
8 26
5 14
9 31 10 15
6 16
11 21
12 32
7 65
13 19 14 70
15 68
5 13 12 24 14 16 65 26 31 15 21 32 19 70 68
0
9 10 11 12 13 14 15
Is this a minimum heap? Does the heap property satisfy or not? Lets analyze this tree.
Start from the root of the tree. Is the value of the root is smaller than that of its left
and right children? The node 5 is smaller than the node 13 and node 12. Check this on
the next level. You will see that the values at each node are smaller than its children.
Therefore it satisfies the definition of the min-heap.
Now we have to understand why we have started the for loop with i = N/2. We can
start i from N. As i moves from level to level in the tree. If we start i from N, it will
C++ Code
Now we will look at the C++ code of the Heap class. The objects of Heap may be got
from this factory class. This class will contain the methods including those discussed
earlier and some new ones. Heap is used both in priority queues and sorting.
We have some public and private methods in the class. Lets have a look on the code.
/* The heap class. This is heap.h file */
template <class eType>
class Heap
{
public: