From:Binary Min — Max Heap and example of heapSort
A binary heap is a heap data structure created using a binary tree.
binary tree has two rules -
- Binary Heap has to be complete binary tree at all levels except the last level. This is called shape property.
- All nodes are either greater than equal to (Max-Heap) or less than equal to (Min-Heap) to each of its child nodes. This is called heap property.
Implementation:
- Use array to store the data.
- Start storing from index 1, not 0.
- For any given node at position i:
- Its Left Child is at [2*i] if available.
- Its Right Child is at [2*i+1] if available.
- Its Parent Node is at [i/2]if available.