Heap Trees
Heap Trees
Types of heaps:
Generally, heaps are of two types.
Max-Heap:
In this heap, the value of the root node must be the greatest among all its
child nodes and the same thing must be done for its left and right sub-tree
also.
The total number of comparisons required in the max heap is according to
the height of the tree. The height of the complete binary tree is always log n;
therefore, the time complexity would also be O(log n).
Min-Heap:
In this heap, the value of the root node must be the smallest among all its
child nodes and the same thing must be done for its left and right sub-tree
also.
The total number of comparisons required in the min heap is according to the
height of the tree. The height of the complete binary tree is always log n;
therefore, the time complexity would also be O(log n).
Page | 1
Properties of Heap:
Heap has the following Properties:
Heap Property: This property ensures that the minimum (or maximum)
element is always at the root of the tree according to the heap type.
Heapify:
It is the process to rearrange the elements to maintain the property of heap
data structure. It is done when a certain node creates an imbalance in the
heap due to some operations on that node. It takes O(log N) to balance the
tree.
For max-heap, it balances in such a way that the maximum element is
the root of that binary tree and
For min-heap, it balances in such a way that the minimum element is the
root of that binary tree.
Page | 2
Insertion:
If we insert a new element into the heap since we are adding a new
element into the heap so it will distort the properties of the heap so we
need to perform the heapify operation so that it maintains the property of
the heap.
This operation also takes O(logN) time.
Deletion:
If we delete the element from the heap it always deletes the root element
of the tree and replaces it with the last element of the tree.
Since we delete the root element from the heap it will distort the
properties of the heap so we need to perform heapify operations so that it
maintains the property of the heap.
It takes O(logN) time.
Page | 3
Assume initially heap(taking max-heap) is as follows
15
/ \
5 7
/ \
2 3
Now if we delete 15 into the heap it will be replaced by leaf node of the tree
for temporary.
3
/ \
5 7
/
2
After heapify operation final heap will be look like this
7
/ \
5 3
/
2
It finds the maximum element or minimum element for max-heap and min-
heap respectively and as we know minimum and maximum elements will
always be the root node itself for min-heap and max-heap respectively. It
takes O(1) time.
removeMin or removeMax:
This operation returns and deletes the maximum element and minimum
element from the max-heap and min-heap respectively. In short, it deletes
the root element of the heap binary tree.
Page | 4
A max (min)heap is a complete binary tree with the property that the value at
each node is at least as large as(as small as) the values at its children(if they
exist).Call this property the heap property.
The definition of a max heap implies that one of the largest elements is at the
root of the heap. If the elements are distinct, then the root contains the
largest item. A max heap can be implemented using an array a[].
To insert an element into the heap, one adds it "at the bottom" of the Heap
and then compares it with its parent, grand parent, great grandparent, and so
on, until it is less than or equal to one of these values.
Page | 5
To delete the maximum key from the max heap, we use an algorithm called
Adjust. Adjust takes as input the array a[] and the integers i and n.
It regards a[l:n] as a complete binary tree. If the sub trees rooted at 2i and
2i+1are already max heaps, then Adjust will rearrange elements of a[] such
that the tree rooted at i is also a maxheap. The maximum element from the
maxheap a[1:n] can be deleted by deleting the root of the corresponding
Complete binary tree. The last element of the array, that is, a[n],is copied to
the root, and finally we call Adjust(a,1,n-1).
Page | 6
Forming a heap from the set {40,80,35,90,45,50,70}
Page | 7
Action of Heapify(a,7)on the data (100,119,118,171,112, 151,and 132)
Algorithm Heapsort
The call of Heapify requires only O(n) operations, Adjust possibly requires
O(log n) operations for each invocation. Thus the worst-case time is
O(n logn).
Page | 8