Binary Heap
Priority Queues are often implemented with a binary heap.
Nearly Complete binary tree is a binary tree in which all levels are “full”, except for possibly bottom level which is filled in from the left.
Each element in the array represents a tree node
Let's say elements are stored in A[1..heap-size].
length
: refers to the total number of elements that the array (which is being used to represent the binary heap) can hold. It's the maximum capacity of the array.heap-size
: refers to the actual number of elements currently in the binary heap. It indicates how much of the array is currently being used to store the heap.
In a binary heap, heap-size
is less than or equal to length
. When the heap is full (i.e., when all available spaces are filled with valid data), heap-size
equals length
. When the heap is not full, heap-size
is less than length
.
Index rule:

Root of tree = A[1]
Parent index of
i
index = i/2Left index of
i
index = 2iRight index of
i
index = 2i + 1First leaf node =
Note: the number of leaves in a heap = which is
The maximum height of a nearly complete tree containing n nodes:
The number of nodes in a complete binary tree where all levels have the maximum number of nodes is where is the height of the tree.
Therefore the number of nodes of a nearly complete tree of height is
Maximum number of nodes:
Minimum number of nodes:
Binary Heap Data Structure:
A min binary heap (min-heap) is a complete binary tree in which every node satisfies the heap property:
A max binary heap (max-heap) is a complete binary tree in which every node satisfies the heap property:
Min-heap operations
MIN-HEAPIFY(A, i)
Percolate down by swapping with smaller child
This function always starts with an index of 1, and it maintains the heap order property. Essentially, if the priority of the parent item (identified by the index) is higher than that of its child item, the function compares the left and right children. Subsequently, it swaps the child with a lower key value than the key value at the given index. The time complexity for this operation is , making it quite efficient.
HEAP-EXTRACT-MIN(A)
Delete minimum node and place last item in hole and percolate down
The function starts by removing the first element in the array. Before returning this element, it fills the now-empty spot with the last element from the heap tree. Following this, it calls upon the MIN-HEAPIFY function to reorder the element that has been moved to the first position, ensuring that it fits correctly within the heap tree's structure.
HEAP-DECREASE-KEY(A, i, key)
Percolate up
The process of "percolating up" through comparison with the parent and increasing the priority of a key at a certain index, works hand in hand with "MIN-HEAP-INSERT(A, key)". In the latter, the heap size is increased and "HEAP-DECREASE-KEY(A, heap-size, key)" is called to meet the heap property. These two procedures function as a pair in managing the heap.
BUILD-MIN-HEAP(A)
Percolate down
The process begins from the parent of the last node and swaps nodes in reverse level order. For each swap, it descends to the very bottom of the structure, carrying out the swaps along the way. The time complexity of this operation is
Amortized analysis: determine worst-case running time of a sequence of data structure operations as a function of the input size.
Aggregate method: sum up sequence of operations, weighted by their cost.
More to learn?
Last updated