HEAP DATA STRUCTURE
1
A Heap is a special Tree-based data structure in
which the tree is a complete binary tree.
Generally, Heaps can be of two types:
Max-Heap: In a Max-Heap the key present at the root
node must be greatest among the keys present at
all of it’s children. The same property must be
recursively true for all sub-trees in that Binary
Tree.
Min-Heap: In a Min-Heap the key present at the root
node must be minimum among the keys present
at all of it’s children. The same property must be
recursively true for all sub-trees in that Binary
Tree.
Binary Heap: Definition
Binary heap.
Almost complete binary tree.
– filled on all levels, except last, where filled from left to right
Min-heap ordered.
– every child greater than (or equal to) parent
06
14 45
78 18 47 53
83 91 81 77 84 99 64
4
Binary Heap: Properties
Properties.
Min element is in root.
Heap with N elements has height = log2 N.
06
N = 14
Height = 3
14 45
78 18 47 53
83 91 81 77 84 99 64
5
Binary Heaps: Array Implementation
Implementing binary heaps.
Use an array: no need for explicit parent or child pointers.
– Parent(i) = i/2
– Left(i) = 2i
– Right(i) = 2i + 1
06
1
14 45
2 3
78 18 47 53
4 5 6 7
83 91 81 77 84 99 64
8 9 10 11 12 13 14
6
Insertion in Heaps
Given a Binary Heap and a new element to be added
to this Heap. The task is to insert the new element
to the Heap maintaining the properties of Heap.
Process of Insertion: Elements can be inserted to
the heap as
First increase the heap size by 1, so that it can
store the new element.
Insert the new element at the end of the Heap.
This newly inserted element may distort the
properties of Heap for its parents. So, in order to
keep the properties of Heap, heapify this newly
inserted element following a bottom-up approach.
Binary Heap: Insertion
Insert element x into heap.
Insert into next available slot.
Bubble up until it's heap ordered.
– Peter principle: nodes rise to level of incompetence
06
14 45
78 18 47 53
83 91 81 77 84 99 64 42 next free slot
10
Binary Heap: Insertion
Insert element x into heap.
Insert into next available slot.
Bubble up until it's heap ordered.
– Peter principle: nodes rise to level of incompetence
06 swap with parent
14 45
78 18 47 53
83 91 81 77 84 99 64 42
11
Binary Heap: Insertion
Insert element x into heap.
Insert into next available slot.
Bubble up until it's heap ordered.
– Peter principle: nodes rise to level of incompetence
06 swap with parent
14 45
78 18 47 42
83 91 81 77 84 99 64 53
42
12
Binary Heap: Insertion
Insert element x into heap.
Insert into next available slot.
Bubble up until it's heap ordered.
– Peter principle: nodes rise to level of incompetence
O(log N) operations.
06 stop: heap ordered
14 42
78 18 47 45
83 91 81 77 84 99 64 53
13
Construct a Min Heap using the
following Numbers
44,33,77,11,55,88,66,22
14
Insert 10
Insert Heap( tree, n, item )
{ n=n+1;
loc = n;
while ( loc >1)
{
parent = (floor) loc/2;
if ( item <= tree[parent])
{ tree[loc]=item; return;
}
tree[loc]=tree[parent]; // Bubble up
loc= parent;
}
tree[1]=item;
return;
}
Deletion in Heap
Given a Binary Heap and an element
present in the given Heap. The task is to
delete an element from this Heap.
The standard deletion operation on Heap
is to delete the element present at the
root node of the Heap.
That is if it is a Max Heap, the standard
deletion operation will delete the
maximum element and if it is a Min
heap, it will delete the minimum element.
Process of Deletion:
Since deleting an element at any intermediary
position in the heap can be costly, so we can
simply replace the element to be deleted by the
last element and delete the last element of the
Heap.
Replace the root or element to be deleted by
the last element.
Delete the last element from the Heap.
Since, the last element is now placed at the
position of the root node. So, it may not follow
the heap property. Therefore, heapify the last
node placed at the position of root.
Binary Heap: Delete Min
Delete minimum element from heap.
Exchange root with rightmost leaf.
Push down until it's heap ordered.
– power struggle principle: better subordinate is promoted
06
14 42
78 18 47 45
83 91 81 77 84 99 64 53
21
Binary Heap: Delete Min
Delete minimum element from heap.
Exchange root with rightmost leaf.
Bubble root down until it's heap ordered.
– power struggle principle: better subordinate is promoted
53
14 42
78 18 47 45
83 91 81 77 84 99 64 06
22
Binary Heap: Delete Min
Delete minimum element from heap.
Exchange root with rightmost leaf.
Bubble root down until it's heap ordered.
– power struggle principle: better subordinate is promoted
53 exchange with left child
14 42
78 18 47 45
83 91 81 77 84 99 64
23
Binary Heap: Delete Min
Delete minimum element from heap.
Exchange root with rightmost leaf.
Bubble root down until it's heap ordered.
– power struggle principle: better subordinate is promoted
14 exchange with right child
53 42
78 18 47 45
83 91 81 77 84 99 64
24
Binary Heap: Delete Min
Delete minimum element from heap.
Exchange root with rightmost leaf.
Bubble root down until it's heap ordered.
– power struggle principle: better subordinate is promoted
O(log N) operations.
14 stop: heap ordered
18 42
78 53 47 45
83 91 81 77 84 99 64
25
26
Binary Heap: Heapsort
Heapsort.
Insert N items into binary heap.
Perform N delete-min operations.
O(N log N) sort.
No extra storage.
27
Maxheapify(A,n,i) n
{
int largerst=i;
2n 2n+1
int left = (2*i);
int right= (2*i)+1;
while(left<=n && A[left]>A[largest])
largest = left;
while ( right<=n &&
A[right]>A[largest])
largest= right;
if ( largest != i )
{ swap ( A[largest],A[i]);
Maxheapify(A, n, largest);
}
}
Heapsort (A,n)
{
for( i=n/2;i>=1;i--) // Build
Maxheap
Maxheapify (A,n,i)
for (i=n; i>1; i--) // Delete
{
swap( A[1], A[i])
Maxheapify(A,n, 1)
}
Time complexity of Insertion in Heap and
Deletion in Heap is
O(nlogn)