heap
heap
1 2
B k C
3 5 6
4
D 2k E 2k+1 F G
7 8 9 10 11
H I J K L
A B C D E F G H I J K L
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Finding parent/child or siblings
Numbering of a tree’s nodes for storage in an array
A B C D E F G H I J K L
0 1 2 3 4 5 6 7 8 9 10 11 12 13
9 12
37 16 47 22
41 44 32 19 50
3 9 12 37 16 47 22 41 44 32 19 50
0 1 2 3 4 5 6 7 8 9 10 11 12
Almost Complete Binary Tree
32 47
41 16 50 22
37 44 3 19 12
9 32 47 41 16 50 22 37 44 3 19 12
0 1 2 3 4 5 6 7 8 9 10 11 12
9
32 47
41 16 50 22
37 44 3 19 12
9 12
37 16 47 22
41 44 32 19 50 5
3 9 12 37 16 47 22 41 44 32 19 50 5
0 1 2 3 4 5 6 7 8 9 10 11 12
Inserting a new element
Insert the new element into the next
available slot in the array (preserving
the structure)
To preserve the order, check against
its parent. If larger than parent, fine.
If smaller than parent, swap it with
the parent (percolating up).
Keep checking until the the new
element is in correct order.
Insert a new element
9 12
37 16 5 22
41 44 32 19 50 47
3 9 12 37 16 5 22 41 44 32 19 50 47
0 1 2 3 4 5 6 7 8 9 10 11 12
Insert a new element
3
9 5
37 16 12 22
41 44 32 19 50 47
3 9 5 37 16 12 22 41 44 32 19 50 47
0 1 2 3 4 5 6 7 8 9 10 11 12
Delete the minimum node
Minimum is at the root
Remove it and pass it to the calling
routine
Now there is a hole at the root
Put last element into root. Decrement
current size by 1.
Percolate the new root down.
DeleteMin
3
9 5
37 16 12 22
41 44 32 19 50 47
3X 9 5 37 16 12 22 41 44 32 19 50 47
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Percolate down
47
37 16 12 22
41 44 32 19 50
47 9 5 37 16 12 22 41 44 32 19 50
0 1 2 3 4 5 6 7 8 9 10 11 12
Percolate down
5
Swap result
9 47
37 16 12 22
41 44 32 19 50
5 9 47 37 16 12 22 41 44 32 19 50
0 1 2 3 4 5 6 7 8 9 10 11 12
Percolate down
5
9 47
37 16 12 22
Compare left and
right (12 & 22) and
41 44 32 19 50 then smaller one with
root(47).
5 9 47 37 16 12 22 41 44 32 19 50
0 1 2 3 4 5 6 7 8 9 10 11 12
Heap after DeleteMin
9 12
37 16 47 22
Swap result
41 44 32 19 50
5 9 12 37 16 47 22 41 44 32 19 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Heap after DeleteMin
9 12
37 16 47 22
Compare 47 with 50. Now
no need to swap, just stop
41 44 32 19 50 there.
Else we have to check till
Last index.
5 9 12 37 16 47 22 41 44 32 19 50
0 1 2 3 4 5 6 7 8 9 10 11 12 13
Are these Heaps?
10
15
8 7
6
2 3 6
20
10 12
What about these?
10 12
6
15 2 7 10 21
8 3 6
10
6 8 7
15 2 3 6
Which are min-heaps?
10 10 10
20 80 20 80
20 80
30 15 40 60 99
40 60 85 99
10 20
50 700
20 80 10 10 80
40 60 85 99 40 60 85 99
20 80
50 700 50 700
40 60
Which are max-heaps?
wrong!
30 48 80
10 20 21 10 25 30
14 24
50
33 30
10 17 10 30 35
40
22 28 18 9
7 3
wrong! 11
Enqueuing an element to a heap
To enqueue an element, the element is added at the
end of the heap as the last leaf.
Restoring the heap property in the case of enqueuing
is achievied by moving from the last leaf toward the
root.
The algorithm for enqueuing is as follows
heapEnqueue(el)
put el at the end of heap;
while el is not in the root and el > parent(el)
swap el with its parent
Enqueuing an element to a heap
20
10 15
8 7 13 14
20
2 5 6
10 15
7 13 14
8
2 5 6 15
Enqueuing an element to a heap
20
10 15
15 13 14 20
8
2 5 6 7
15 15
10 13 14
8
2 5 6 7
Dequeuing an element from heap
Dequeuing an element from the heap
consists of removing the root element from
the heap, since
By the heap property it is the element with
the greatest priority.
Then the last leaf is put in its place,
and the heap property almost certainly has
to be restored,
This time by moving down the root down
the tree.
Dequeuing an element from heap
The algorithm for dequeuing is as follows:
heapDequeue()
extract the element from the root;
put the element from the last leaf in its
place;
remove the last leaf;
//both subtrees of the root are heaps;
p = root;
while p is not a leaf and p < any of its
children
swap p with the larger child;
Dequeuing an element from heap
dequeue
20
10 15
8 7 13 14
2 5 6 6
10 15
8 7 13 14
2 5
Dequeuing an element from heap
15
10 6
8 7 13 14
15
2 5
10 14
8 7 13 6
2 5
Dequeuing an element from heap
heapDequeue()
extract the element from the root;
put the element from the last leaf in its place;
remove the last leaf;
//both subtrees of the root are heaps;
p = root;
while p is not a leaf and p < any of its children
swap p with the larger child;
The last three lines of this algorithm can be treated as the
separate algorithm that
Restore the heap property only if it has been violated by
the root of the tree.
Array implementation
We start from position 1 in the array.
The first element contains the root
Left child of element at position j is at
position 2j, right child is at position 2j
+1
Parent of element at position j is at
j/2
Need to know the size of the heap.
Insertion
Find an empty position in the heap
If some nodes have one child, then the empty
position is the other child
If all nodes have 0 or 2 children, then the empty
position is the left child of a leaf.
Insert the element there.
Let the element be inserted at position j. If
the parent of the element is more than the
element, then interchange the parent and
child
Interchange elements at j/2 and j.
If necessary, interchange elements at j/2
with j/2 /2
And so on till we reach the root.
We are maintaining the heap property at
every stage.
For (k = empty_pos; k >1; k = k/2 )
If (Heap[k] < Heap[k/2 ])
interchange(Heap[k] , Heap[k/2 ]) ;
DeleteMin
Delete the root.
Compare the two children of the root
Make the lesser of the two root.
An empty spot is created.
Bring the lesser of the two children of
the empty spot to the empty spot.
A new empty spot is created.
Continue
pseudocode
Delete root; k =1;
Do
{
If A[k] is empty, break;
If A[k] min(A[2k], A[2k + 1]), break;
If A[2k] < A[2k + 1] j = 2k, else j = 2k+1;
A[k] = A[j];
k = j;
}Insert A[currsize] at A[k]; }
Implementation of Heap
Main()
{
/* This variable is used to indicate
size of the array acting as binary heap */
unsigned int HeapSize;
/* This pointer points to the array
which behaves as binary heap */
int *heap;
/*This variable indicates the last insertion
point I.e. points to last inserted leaf */
int last;