VOOZH about

URL: https://www.geeksforgeeks.org/dsa/introduction-to-min-heap-data-structure/

⇱ Introduction to Min-Heap - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to Min-Heap

Last Updated : 3 Apr, 2026

A Min-Heap is a Data Structure with the following properties.

  • It is a Complete Binary Tree.
  • The value of the root node must be the smallest among all its descendant nodes and the same thing must be done for its left and right sub-tree also.

Internal Implementation of Min-Heap Data Structure

A heap can be efficiently represented using an array.

  • If a node is stored at index i:
  • Its left child is at index 2*i + 1.
  • Its right child is at index 2*i + 2.
  • The parent of a node at index i can be found at index [(i-1)/2].
👁 minH2drawio

Operations on Min-heap Data Structure and their Implementation:

Here are some common operations that can be performed on a Heap Data Structure,

Insertion - O(log n) Time and O(n) Space

The insertion operation in a min-heap involves the following steps:

  • Add the new element to the end of the heap, in the next available position in the last level of the tree.
  • Compare the new element with its parent. If the parent is greater than the new element, swap them.
  • Repeat step 2 until the parent is smaller than or equal to the new element, or until the new element reaches the root of the tree.

Illustration:

Suppose the Heap is a Min-Heap as:

👁 Insertion in Min-Heap

Output
Inserted 10 into the min-heap: 10 
Inserted 3 into the min-heap: 3 10 
Inserted 2 into the min-heap: 2 10 3 
Inserted 4 into the min-heap: 2 4 3 10 
Inserted 5 into the min-heap: 2 4 3 10 5 
Inserted ...

Deletion - O(log n) Time and O(n) Space

Removing the smallest element (the root) from a Min-Heap involves the following steps:

  • Replace the root (or the element to be deleted) with the last element in the heap.
  • Remove the last element from the heap, since it has been moved to the root.
  • Heapify-down: The element now at the root may violate the Min-Heap property, so perform heapify starting from the root to restore the heap property.

Illustration:  


Output
Initial heap: 13 16 31 41 51 100 
Heap after deleting 13: 16 41 31 100 51 

Peek operation - O(1) Time and O(n) Space

 To access the minimum element (i.e., the root of the heap), the value of the root node is returned.

👁 Min Heap Data Structure

Output
Peak element: 13

Heapify - O(n) Time and O(log n) Space

The heapify operation is used to place an element in its correct position within the heap so that the heap property is maintained.
A heapify operation can also be used to create a min heap from an unsorted array. This is done by starting at the last non-leaf node and repeatedly performing the "heapify down" operation until all nodes satisfy the heap property. 

👁 Heapify operation in Min Heap

Output
Original array: 2 3 10 4 5 1 
Min-Heap after heapify operation: 1 3 2 4 5 10 

Min-Heap Vs Max-Heap

👁 420046868

Applications of Min-Heap Data Structure

  • Heap Sort: Min-heap is used to implement heap sort, an efficient O(n log ⁡n) sorting algorithm.
  • Priority Queue: Min-heap ensures the smallest element is always at the root, enabling efficient priority queue operations.
  • Dijkstra’s Algorithm: Min-heap stores vertices by minimum distance for efficient shortest-path computation.
  • Huffman Coding: Min-heap implements a priority queue to build optimal prefix codes.
  • Merge K Sorted Arrays: Min-heap efficiently merges K sorted arrays into one sorted array.

Advantages of Min-heap Data Structure

  • Fast Insertion and Deletion: Supports insertion and removal in O(log ⁡n) time, maintaining heap property efficiently.
  • Quick Access to Minimum: The smallest element is always at the root, retrievable in O(1) time.
  • Compact Storage: Can be implemented using arrays, saving memory compared to pointer-based structures.
  • Heap-based Sorting: Enables efficient sorting algorithms like heap sort with O(n log ⁡n) complexity.
  • Wide Applicability: Used in CPU/job scheduling (priority scheduling), graph algorithms like Dijkstra’s shortest path and Prim’s MST, and data compression like Huffman coding.
Comment
Article Tags:
Article Tags: