ClassS04CS141/Notes 03 31

Top | ClassS04CS141 | recent changes | Preferences

Leaves and interior nodes in binary trees

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:


proof: Put a coin on each leaf. Then repeat the following process:

For any interior node without a coin where each child has at least one coin, take a coin from each of the two children and put these two coins on the node.

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,

#leaves = #coins before = #coins after = #interior nodes + 1

Had some discussion of what constitutes a proof.

Geometric sums

1 + 2 + 4 + 8 + … + 2n = 2n+1 - 1.

More generally,

1 + x + x2 + x3 + … + xn = (xn+1 - 1)/(x-1) unless x=1.

Using sum notation:

i=0n xi = (xn+1-1)/(x-1).

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.

Growable array

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.


Top | ClassS04CS141 | recent changes | Preferences
This page is read-only | View other revisions
Last edited April 2, 2004 3:51 pm by Neal (diff)
Search: