Java Reference
In-Depth Information
Any number of
internal nodes
Figure5.4 A tree containing many internal nodes and a single leaf.
bound will occur when each internal node has two non-empty children, that is,
when the tree is full. However, this observation does not tell what shape of tree will
yield the highest percentage of non-empty leaves. It turns out not to matter, because
all full binary trees with n internal nodes have the same number of leaves. This fact
allows us to compute the space requirements for a full binary tree implementation
whose leaves require a different amount of space from its internal nodes.
Theorem5.1 Full Binary Tree Theorem: The number of leaves in a non-empty
full binary tree is one more than the number of internal nodes.
Proof: The proof is by mathematical induction on n, the number of internal nodes.
This is an example of an induction proof where we reduce from an arbitrary in-
stance of size n to an instance of size n 1 that meets the induction hypothesis.
• Base Cases: The non-empty tree with zero internal nodes has one leaf node.
A full binary tree with one internal node has two leaf nodes. Thus, the base
cases for n = 0 and n = 1 conform to the theorem.
• Induction Hypothesis: Assume that any full binary tree T containing n 1
internal nodes has n leaves.
• Induction Step: Given tree T with n internal nodes, select an internal node I
whose children are both leaf nodes. Remove both of I's children, making
I a leaf node. Call the new tree T 0 . T 0 has n 1 internal nodes. From
the induction hypothesis, T 0 has n leaves. Now, restore I's two children. We
once again have tree T with n internal nodes. How many leaves does T have?
Because T 0 has n leaves, adding the two children yields n+2. However, node
I counted as one of the leaves in T 0 and has now become an internal node.
Thus, tree T has n + 1 leaf nodes and n internal nodes.
By mathematical induction the theorem holds for all values of n 0. 2
When analyzing the space requirements for a binary tree implementation, it is
useful to know how many empty subtrees a tree contains. A simple extension of
the Full Binary Tree Theorem tells us exactly how many empty subtrees there are
in any binary tree, whether full or not. Here are two approaches to proving the
following theorem, and each suggests a useful way of thinking about binary trees.
Search WWH ::




Custom Search