![]() |
VOOZH | about |
A Binary Heap is a complete Binary Tree that is used to store data efficiently to get the max or min element based on its structure.
A Binary Heap is either a Min Heap or a Max Heap. In a Min Binary Heap, the key at the root must be minimum among all keys present in a Binary Heap. The same property must be recursively true for all nodes in the Binary Tree. Max Binary Heap is similar to MinHeap.
Examples of Min Heap:
10 10
/ \ / \
20 100 15 30
/ / \ / \
30 40 50 100 40
Implement a binary heap data structure in Python, which supports the following operations:
insert(key): Insert a new element into the heap.delete_min(): Delete the minimum element from the heap.get_min(): Get the minimum element from the heap without deleting it.is_empty(): Check if the heap is empty.A binary heap is a complete binary tree where the value of each parent node is less than or equal to the values of its children. The minimum element is at the root. The main operations are insert and delete_min.
Minimum element in the heap: 2 Deleted minimum element: 2 Minimum element in the heap after deletion: 3