Introduction
A min heap is a binary tree data structure that satisfies the heap property, where the parent node is always less than or equal to its children. In a min heap, the root node is always the minimum value in the tree.
This tutorial is from open-source community. Access the source code
A min heap is a binary tree data structure that satisfies the heap property, where the parent node is always less than or equal to its children. In a min heap, the root node is always the minimum value in the tree.
Implement a min heap with the following methods:
extract_min()
: removes and returns the minimum value in the heapinsert(value)
: inserts a new value into the heap while maintaining the heap propertyThe implementation should meet the following requirements:
Consider the following min heap:
_5_
/ \
20 15
/ \ / \
22 40 25
extract_min()
: removes and returns the minimum value in the heap, which is 5. The resulting heap is: _15_
/ \
20 25
/ \ / \
22 40
insert(2)
: inserts the value 2 into the heap while maintaining the heap property. The resulting heap is: _2_
/ \
20 5
/ \ / \
22 40 25 15
In summary, a min heap is a binary tree data structure that maintains the heap property, where the parent node is always less than or equal to its children. The implementation of a min heap should include the extract_min()
and insert(value)
methods, and meet the requirements of integer inputs and fitting in memory.