The mathematical proof lies in the following relationship:

The total number of nodes ($n) in a tree comprises leaves ($l) and interior nodes ($i), i.e. $n = $l + $i. Therefore, if you have ($i) interior nodes, and each interior node is a parent of 2 children, that gives you 2*$i children. Plus the root node is (2*$i)+1.

So in the 1st diagram, you have 4 interior nodes – the root node counts as an interior node since it has 2 children – and 5 leaves for a total of 9 nodes. i.e. n = l + i => 9 = 4 + 5, and 2i + 1 => 2(4) + 1 = 9.

As we’re using an array to represent our binary heap, we can infer that the 2nd half (i.e. floor($n/2)) of the array will contain the leaf nodes – since a complete binary heap with a full last level will have $n/2 + 1 nodes in it. So, when you consider a node rooted at $i, if 2*$i + 1 > the last item in the tree (i.e. $n), the node must be a leaf.

Thus, as you rightly inferred, if $n is the total number of nodes in binary tree, the node rooted at position $i is a leaf node, if $n <= 2*$i + 1.

]]>I read this maybe ten times before I understood what you meant.

For anyone else who can’t grasp it, I’ll reword it and make some explanations:

Let’s implement the heap as array counting items from left to right, top to bottom. That is for the second picture we start at the top (let’s call it level 1):

$a[0] = 7; // 1st level, root

$a[1] = 19; // 2nd level, left child of root

$a[2] = 36; // 2nd level, right child of root

$a[3] = 17; // 3rd level, left child of $a[1]

$a[4] = 3; // 3rd level, right child of $a[1]

…

$a[7] = 2; // 4th level, left child of $a[3]

The claim is that a COMPLETE binary tree has a good property that allows us to quickly detect if the node is a leaf. The node $a[$n] is a leaf if the complete binary tree contains not more than 2*$n+1 items.

quick check: count($a) = 8; $a[3] shouldn’t be a leaf (8 > 2*3+1) – correct; $a[4] should be a leaf (8 <= 2*4+1) – correct;