Member-only story
Heap in Data Structure and Algorithm
A *heap* is a special kind of tree-based data structure that follows a unique rule known as the *heap property*. It is a key tool in computer science and is widely used in various applications like priority queues, sorting algorithms, and memory management.
Types of Heap
Heaps are mainly divided into two types:
1. **Min-Heap:** In a min-heap, the value of the parent node is always smaller than or equal to the values of its child nodes. This means the smallest value is always found at the root of the heap.
— Example:
10
/ \
20 15
/ \ / \
40 50 30 35
Here, each parent node has a value smaller than its child nodes.
2. **Max-Heap:** In a max-heap, the value of the parent node is always greater than or equal to the values of its child nodes. The largest value is located at the root of the heap.
— Example:
50
/ \
30 40
/ \ / \
10 20 15 25
Here, each parent node has a value larger than its child nodes.