A Min-Heap is a complete binary tree in which the value in each internal node is smaller than or equal to the values in the children of that node.
Mapping the elements of a heap into an array is trivial: if a node is stored a index k, then its left child is stored at index 2k + 1 and its right child at index 2k + 2.
Example of Min Heap :
5 13 / \ / \ 10 15 16 31 / / \ / \ 30 41 51 100 41
How is Min Heap represented ?
A Min Heap is a Complete Binary Tree. A Min heap is typically represented as an array. The root element will be at Arr. For any ith node, i.e., Arr[i]:
- Arr[(i -1) / 2] returns its parent node.
- Arr[(2 * i) + 1] returns its left child node.
- Arr[(2 * i) + 2] returns its right child node.
Operations on Min Heap :
- getMin(): It returns the root element of Min Heap. Time Complexity of this operation is O(1).
- extractMin(): Removes the minimum element from MinHeap. Time Complexity of this Operation is O(Log n) as this operation needs to maintain the heap property (by calling
heapify()) after removing root.
- insert(): Inserting a new key takes O(Log n) time. We add a new key at the end of the tree. If new key is larger than its parent, then we don’t need to do anything. Otherwise, we need to traverse up to fix the violated heap property.
Below is the implementation of Min Heap in Python –
The Min Heap is PARENT : 3 LEFT CHILD : 5 RIGHT CHILD :6 PARENT : 5 LEFT CHILD : 9 RIGHT CHILD :84 PARENT : 6 LEFT CHILD : 19 RIGHT CHILD :17 PARENT : 9 LEFT CHILD : 22 RIGHT CHILD :10 The Min val is 3
Using Library functions :
We use heapq class to implement Heaps in Python. By default Min Heap is implemented by this class.
Head value of heap : 10 The heap elements : 10 30 20 400 The heap elements : 20 30 400
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Difference between Binary Heap, Binomial Heap and Fibonacci Heap
- Convert min Heap to max Heap
- Heap Sort for decreasing order using min heap
- Difference between Min Heap and Max Heap
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Convert BST to Min Heap
- Merge k sorted linked lists | Set 2 (Using Min Heap)
- Print all nodes less than a value x in a Min Heap.
- Maximum element in min heap
- Fibonacci Heap - Deletion, Extract min and Decrease key
- K’th Least Element in a Min-Heap
- Maximise the number of toys that can be purchased with amount K using min Heap
- Merge two sorted arrays in constant space using Min Heap
- Complexity analysis of various operations of Binary Min Heap
- k largest(or smallest) elements in an array | added Min Heap method
- Heap queue (or heapq) in Python
- Python Program for Heap Sort
- Python Code for time Complexity plot of Heap Sort
- How are variables stored in Python - Stack or Heap?
- Max Heap in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.