Heap
In the lesson, we'll discuss heap, a very useful tree-based data structure.
We'll cover the following...
Structure
Heap is a complete binary tree. There are two types of heaps
- Min Heap
- Max Heap
Min heap
In min-heap, each node is smaller than its children. This means the root has the smallest key among all the nodes. Similarly, the left child of the root is the smallest among all nodes in the left subtree of the root, and so on.
Similarly, in a max heap, the key for each node is greater than both its children.
Representation
Due to the structure of the heap, it can be represented using an array. This means you don’t have to deal with pointers and the code is much simpler.
Let’s see how an array is visualized as a heap.