Junior — Middle
Can you explain what is meant by the data structure binary heap?
sobes.tech AI
Answer from AI
A binary heap is a data structure that represents a complete binary tree satisfying the heap property:
- For a min-heap: the value in each node is less than or equal to the values of its children.
- For a max-heap: the value in each node is greater than or equal to the values of its children.
Main properties:
- Complete binary tree: all levels are fully filled, except possibly the last, which is filled from left to right.
- Fast insertion and deletion of the minimum (or maximum) element in O(log n).
Applications:
- Implementation of priority queues.
- Sorting algorithms (heapsort).
Example: a min-heap with elements [2, 3, 5, 7, 8, 10]
2
/ \
3 5
/ \ /
7 8 10
Insertion and deletion operations maintain the heap structure and property through "sifting" up or down.