In a binary heap, each parent has two children. The indexing formula (2i+1 for the left child and 2i+2 for the right child) comes from the way the binary heap is structured as a complete binary tree but represented in an array.
Starting Index: Arrays are zero-indexed, so starting from an index i for a parent node,
Left Child: The next layer of the tree (the children) starts at 2i + 1 because:
Doubling i (2i) shifts to the position at the start of the next level of a complete binary tree.
Adding 1 adjusts for the zero-based indexing, pointing to the first child (left).
Right Child: Similarly, for the right child at 2i + 2, you add one more to reach the second child’s position.
This arrangement helps in maintaining a compact, easily navigable tree structure within a simple array, allowing quick calculations to move between parent and child nodes.