Binary Heap
Binary Heap
There are several types of heaps, but in the current article we are going to discuss
the binary heap. For short, let's call it just "heap". It is used to implement priority
queue ADT and in the heapsort algorithm. Heap is a complete binary tree, which
answers to the heap property.
It is said, that binary tree is complete, if all its levels, except possibly the deepest,
are complete. Thought, incomplete bottom level can't have "holes", which means
that it has to be fulfilled from the very left node and up to some node in the middle.
See illustrations below.
There are two possible types of binary heaps: max heap and min heap. The
difference is that root of a min heap contains minimal element and vice versa.
Priority queue is often deal with min heaps, whereas heapsort algorithm, when
sorting in ascending order, uses max heap.
For every node in a heap, node's value is lesser or equal, than values of the
children.
For every node in a heap, node's value is greater or equal, than values of the
children.
A heap is stored in array level by level. The topmost level contains root only. It is
mapped to the first element of an array (with index 0). Root's children are mapped
to the second and third elements and so on. A heap is a complete binary tree,
which guarantees, that heap's nodes take up places in the array compactly,
making the mapping quite efficient.
You can see now, that navigation over a heap, mapped to an array, is, actually,
very easy.
Code snippets
Java implementation
Insertion algorithm
Now, let us phrase general algorithm to insert a new element into a heap.
Example
In the general case, after insertion, heap property near the new node is broken:
To restore heap property, algorithm sifts up the new element, by swapping it with
its parent:
Complexity analysis
Complexity of the insertion operation is O(h), where h is heap's height. Taking into
account completeness of the tree, O(h) = O(log n), where n is number of elements
in a heap.
Code snippets
Java implementation
………………..
if (heapSize == data.length)
else {
heapSize++;
data[heapSize - 1] = value;
siftUp(heapSize - 1);
……………….
if (nodeIndex != 0) {
parentIndex = getParentIndex(nodeIndex);
tmp = data[parentIndex];
data[parentIndex] = data[nodeIndex];
data[nodeIndex] = tmp;
siftUp(parentIndex);
Removal algorithm
Example
Root has two children. Swap root's value with the smallest:
Complexity analysis
Code snippets
Java implementation
Summary
A binary heap is a complete binary tree which satisfies the heap ordering property.
The ordering can be one of two types:
the min-heap property: the value of each node is greater than or equal to the
value of its parent, with the minimum-value element at the root.
the max-heap property: the value of each node is less than or equal to the
value of its parent, with the maximum-value element at the root.
Since a heap is a complete binary tree, it has a smallest possible height - a heap
with N nodes always has O(log N) height.
A heap is useful data structure when you need to remove the object with the
highest (or lowest) priority. A common use of a heap is to implement a priority
queue.
Using a binary heap, the runtime of both the deleteMin and insert operations is
O(log n).