![]() |
VOOZH | about |
A Heap is a special Tree-based data structure in which the tree is a complete binary tree. Since a heap is a complete binary tree, a heap with N nodes has log N height. It is useful to remove the highest or lowest priority element. It is typically represented as an array. There are two types of Heaps in the data structure.
In a Min-Heap the key present at every node node node must be less than all of its children. In a Min-Heap the minimum key element present at the root. Below is the Binary Tree that satisfies all the property of Min Heap.
👁 ImageIn a Max-Heap the key present at every node node must be greater than at all of its children. In a Max-Heap the maximum key element present at the root. Below is the Binary Tree that satisfies all the property of Max Heap.
👁 Image| Min Heap | Max Heap | |
|---|---|---|
| 1. | In a Min-Heap the key present at the root node must be less than all of its descendants and same thing must be true for all subtrees, | In a Max-Heap the key present at the root node must be more than all of its descendants and same thing must be true for all subtrees, |
| 2. | In a Min-Heap the minimum key element present at the root. | In a Max-Heap the maximum key element present at the root. |
| 3. | A Min-Heap uses the ascending priority. | A Max-Heap uses the descending priority. |
| 4. | In the construction of a Min-Heap, the smallest element has priority. | In the construction of a Max-Heap, the largest element has priority. |
| 5. | In a Min-Heap, the smallest element is the first to be popped from the heap. | In a Max-Heap, the largest element is the first to be popped from the heap. |
:
: