Open In App

Min Heap meaning in DSA

Last Updated : 17 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A min heap is a binary tree-based data structure where the value of each node is less than or equal to its child nodes. 

In other words, the root node is always the minimum element in the heap. Here is an example of the min heap tree:

Example of min Heap

Example of min Heap

Array representation of the above min heap

Array representation of the above min heap

Characteristics of Min Heap :

  • A min heap is a complete binary tree. This property ensures that the tree is balanced and can be represented efficiently using an array.
  • The value of each node in a min heap is less than or equal to the values of its child nodes.
  • Insertion and deletion of elements take O(log n) time in a min heap, where n is the number of elements in the heap. This makes it efficient for large datasets.

Applications of Min Heap:

  • Priority queues: Min heap is commonly used to implement priority queues, where elements are stored based on their priority.
  • Heap sort: Heap sort is a sorting algorithm that uses a min heap to sort elements in ascending order. The elements are first added to a heap, and then removed in order to create a sorted list.
  • Graph algorithms: Min heap is used in graph algorithms like Dijkstra’s algorithm and Prim’s algorithm to maintain the set of vertices with the shortest distance or minimum weight. In these algorithms, the heap is used to store the unexplored vertices and to extract the vertex with the minimum distance/weight.
  • Memory management: Min heap can be used in memory management to allocate memory to processes in an efficient manner by extracting a block with the minimum size.

Advantages of Min Heap:

  • Constant-time access to the minimum element: The root of a min heap always holds the minimum element, which can be accessed in constant time, providing efficient access to the minimum element.
  • Efficient insertion and deletion: Insertion and deletion of elements take O(log n) time, where n is the number of elements in the heap. This makes it efficient for large datasets.
  • Space efficiency: A min heap can be implemented using an array

Disadvantages of Min Heap:

  • Accessing non-minimum elements in a min heap can be time-consuming, as there is no direct way to access them. It may require traversing the entire heap to find the desired element.
  • Although the insertion and deletion of elements take O(log n) time, building a heap from an unsorted array takes O(n) time, which can be time-consuming for large datasets.

What else can you read?


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads