What is a Heap and how does it work?
mainA Heap is a data structure used to implement a Priority Queue (Highest Priority First - HPF). Unlike a standard FIFO queue, a Heap allows for efficient access to the highest priority element.
A Heap is implemented as a complete binary tree, meaning every level is filled except possibly the last, which is filled from left to right. This structure allows the heap to be efficiently represented using an array rather than pointers.
Types of Heaps
- Max Heap: The root node is the maximum element. For every node, the parent must be greater than or equal to its children.
- Min Heap: The root node is the minimum element. For every node, the parent must be smaller than or equal to its children.