claim: In any binary tree where each interior node has two children, it is the case that #leaves = #interior nodes + 1.
We did an in-class exercise to try prove this claim. Students came up with inductive proofs and a direct counting argument. Instructor proposed the following proof:
When the process finishes, each leaf has no coins, each interior node has one coin, and the root has two coins.
Since the number of coins is the same before and after this process,
Had some discussion of what constitutes a proof.
More generally,
Using sum notation:
We showed a proof of this by multiplying both sides by (1-x) and simplifying.
For this class, the important thing is that for constant x > 1, the above is O(xn).
In words, with a geometrically increasing sum (where each term is a constant factor larger than the previous term), the entire sum is proportional to the largest term.
(This is the key point of the class today!)
When x=2, the sum counts the number of interior nodes of a complete binary tree with 2n+1 leaves. Thus, the number of nodes in a complete binary tree is proportional to the number of leaves.
This phenomenon is important for the design and analysis of algorithms. For example, consider the next section.
We discussed an implementation of a growable array, where, when the array needs to be made larger, the space allocated for it is doubled. We discussed how the work required for N operations is O(N+M), where M is the maximum index of any array cell that is explicitly referenced.
To show this we used 1+2+4+… + 2n = O(2n).
The term 2i in the sum counts the work done in growing the array from size 2i-1 to size 2i.