21

I'm trying to prove that a binary heap with $n$ nodes has exactly $\left\lceil \frac{n}{2} \right\rceil$ leaves, given that the heap is built in the following way:

Each new node is inserted via percolate up. This means that each new node must be created at the next available child. What I mean by this is that children are filled level-down, and left to right. For example, the following heap:

    0
   / \
  1   2

would have to have been built in this order: 0, 1, 2. (The numbers are just indexes, they give no indication of the actual data held in that node.)

This has two important implications:

  1. There can exist no node on level $k+1$ without level $k$ being completely filled

  2. Because children are built left to right, there can be no "empty spaces" between the nodes on level $k+1$, or situations like the following:

        0
       / \
      1   2
     / \   \
    3  4    6
    

(This would be an illegal heap by my definition.) Thus, a good way to think of this heap is an array implementation of a heap, where there can't be any "jumps" in indeces of the array.

So, I was thinking induction would probably be a good way to do this... Perhaps something having to deal with even an odd cases for n. For example, some induction using the fact that even heaps built in this fashion must have an internal node with one child for an even n, and no such nodes for an odd n. Ideas?

Raphael
  • 72,336
  • 29
  • 179
  • 389
varatis
  • 463
  • 1
  • 4
  • 8

2 Answers2

18

Here's a simpler logical proof.

Last leaf is $n^{th}$ index. Its parent is at index $\lfloor{n/2}\rfloor$ and similarly, there is no element such that its parent is ($\lfloor n/2 +1\rfloor)^{th}$ element. Thus leaves are indexed from $\lfloor{n/2}\rfloor$ +1 to n.

Hence, total number of leaves = n- $\lfloor(n/2)\rfloor$ = $\lceil(n/2)\rceil$.

crisron
  • 105
  • 4
Zubin Pahuja
  • 189
  • 1
  • 2
10

If I get your question correctly, the obtained heap is just an ordered binary-tree, where in ordered I mean that the $k$th level can only be occupied after the $k-1$ level has been completely filled, and each level is occupied from left to right, without skipping.

Then the proof goes like this.

  1. A perfect tree of depth $k$ has exactly $2^{k+1}-1$ nodes.
  2. Assume that the heap reaches depth $k$. Thus
    1. up to level $k-1$ the tree is perfect (and has $2^k-1$ nodes there)
    2. on the last level, there are exactly $n-2^k+1$ nodes, which are all leaves.
  3. Each leaf on the $k$th level has a parent. Moreover, each two consecutive leaves have the same father (maybe except for the last node, whose father has only one child)
  4. Thus, out of the $2^{k-1}$ nodes at level $k-1$, $\left\lceil \frac{n-2^{k}+1}{2} \right\rceil$ are parents, and the rest $2^{k-1} - \left\lceil\frac{n-2^{k}+1}{2}\right\rceil$ are leaves.
  5. The total amount of leaves is $$n-2^k+1 + 2^{k-1} - \left\lceil\frac{n-2^{k}+1}{2}\right\rceil$$ Which gives you what you need.
Raphael
  • 72,336
  • 29
  • 179
  • 389
Ran G.
  • 20,684
  • 3
  • 60
  • 115
  • 1
    Note that full are different from complete are different from perfect binary trees. Unfortunate, ambiguous and inconsistent choice of words there, but what can you do about it. I guess sticking to Wikipedia's definition makes sense, as most will look there first? – Raphael Mar 28 '12 at 06:49
  • Oh, wow, I didn't even know these terms. Thanks for pointing this out. – Ran G. Mar 28 '12 at 07:22
  • "up to level k−1 the tree is perfect (and has 2^k − 1 nodes there)" and "Thus, out of the 2^(k−1) nodes at level k−1" seem to be conflicting statements, or am I missing something? – adrian h. Aug 12 '16 at 10:36
  • @adrianh. ($2^k-1$) nodes in the entire subtree, $2^{k-1}$ nodes only on the last level (thus in the entire subtree there are $2^{k-1}+2^{k-2}+...$ nodes.) – Ran G. Aug 12 '16 at 14:11
  • Ah you're completely right, thanks a lot for the clarification! – adrian h. Aug 13 '16 at 12:38
  • how to conclude that there are 2^k -1 nodes in at height k ? – Zubair Sep 23 '17 at 11:14
  • Where did +1 came from in step: on the last level, there are exactly n−2k+1 nodes, which are all leaves. – Zubair Sep 23 '17 at 13:49
  • Hi there. Too late and dumb to ask this, but please let me know as to why is it: $\left\lceil \frac{n-2^{k}+1}{2} \right\rceil$ and not $\left\lfloor \frac{n-2^{k}+1}{2} \right\rfloor$ – Sachin Bahukhandi Apr 11 '20 at 15:09
  • 1
    @SachinBahukhandi 2 children -> 1 parent. 1 (last) children -> 1 parent. Then the number of parents is half the children, maybe plus one because of the last (single) child. – Ran G. Apr 11 '20 at 16:20
  • Hi @RanG. I got your point on why is it $n-2^k+1$ but cannot get the ceiling function usage and derivation as how can I show its importance. – Sachin Bahukhandi Apr 11 '20 at 18:03
  • @SachinBahukhandi Try the example from my previous comment with $n$ children and $\lceil n/2 \rceil$ parents. Substitute $n=2$ and $n=1$. – Ran G. Apr 11 '20 at 18:16