Heap Data Structure
Heap Data Structure
Heap data structure is a complete binary tree that satisfies the heap property, where any given
node is
always greater than its child node/s and the key of the root node is the largest among all other
always smaller than the child node/s and the key of the root node is the smallest among all
Max Heap
Min Heap
Heap Operations
Some of the important operations performed on a heap are described below along with their
algorithms.
Heapify
Heapify is the process of creating a heap data structure from a binary tree. It is used to create a
Min-Heap or a Max-Heap.
Initial Array
3. Start from the first index of non-leaf node whose index is given by n/2 - 1.
5. The index of left child is given by 2i + 1 and the right child is given by 2i + 2.
If left Child is greater than current Element (i.e. element at ith index),set left Child
Index as largest.
If right Child is greater than element in largest, set right Child Index as largest.
Swap if necessary