Trees:Binary Tree Traversals
Binary Trees and Properties
Binary trees were defined in Section 3.1. For convenience, a binary tree is sometimes extended by adding external nodes. External nodes are imaginary nodes that are added wherever an empty subtree was present in the original tree. The original tree nodes are known as internal nodes. Figure 3.5(a) shows a binary tree and (b) the corresponding extended tree. Observe that in an extended binary tree, all internal nodes have degree 2 while all external nodes have degree 0. (Some authors use the term full binary tree to denote a binary tree whose nodes have 0 or two children.) The external path length of a tree is the sum of the lengths of all root-to-external node paths in the tree. In the example, this is 2 + 2 + 3 + 3 + 2 = 12. The internal path length is similarly defined by adding lengths of all root-to-internal node paths. In the example, this quantity is 0 + 1 + 1 + 2 = 4.
Properties
LEMMA 3.2 A binary tree with n internal nodes has n + 1 external nodes.
Proof Each internal node in the extended tree has branches leading to two children. Thus, the total number of branches is 2n. Only n − 1 internal nodes have a single incoming branch from a parent (the root does not have a parent). Thus, each of the remaining n +1 branches points to an external node.
LEMMA 3.3 For any non-empty binary tree with n0 leaf nodes and n2 nodes of degree 2, n0 = n2 + 1.
Proof Let n1 be the number of nodes of degree 1 and n = n0 + n1 + n2 (Eq. 1) be the total number of nodes. The number of branches in a binary tree is n − 1 since each non-root node has a branch leading into it. But, all branches stem from nodes of degree 1 and 2. Thus, the number of branches is n1 + 2n2. Equating the two expressions for number of branches, we get n = n1 + 2n2 + 1 (Eq. 2). From Eqs. 1 and 2, we get n0 = n2 + 1.
LEMMA 3.4 The external path length of any binary tree with n internal nodes is 2n greater than its internal path length.
Proof The proof is by induction. The lemma clearly holds for n = 0 when the internal and external path lengths are both zero. Consider an extended binary tree T with n internal nodes. Let ET and IT denote the external and internal path lengths of T . Consider the extended binary tree S that is obtained by deleting an internal node whose children are both external nodes (i.e., a leaf) and replacing it with an external node. Let the deleted internal node be at level l. Thus, the internal path length decreases by l while the external path length decreases by 2(l + 1) − l = l + 2. From the induction hypothesis, ES = IS + 2(n − 1). But, ET = ES + l + 2 and IT = IS + l. Thus, ET − IT = 2n.
LEMMA 3.5 The maximum number of nodes on level i of a binary tree is 2i, i ≥ 0.
Proof This is easily proved by induction on i.
LEMMA 3.6 The maximum number of nodes in a binary tree of height k is 2k+1 − 1.
Proof Each level i, 0 ≤ i ≤ k, has 2i nodes. Summing over all i results in ),k 2i = 2k+1 − 1.
LEMMA 3.7 The height of a binary tree with n internal nodes is at least llog2(n + 1)l and at most n − 1.
Proof The worst case is a skewed tree (Figure 3.6(a)) and the best case is a tree with 2i nodes at every level i except possibly the bottom level (Figure 3.6(b)). If the height is h, then n + 1 ≤ 2h, where n + 1 is the number of external nodes.
LEMMA 3.8 The number of distinct binary trees with n nodes is
Proof
For a detailed proof, we refer the reader to [7]. However, we note that Cn = are known as the Catalan numbers, which occur frequently in combinatorial problems. The Catalan number Cn also describes the number of trees with n + 1 nodes and the number of binary trees with 2n + 1 nodes all of which have 0 or 2 children.
Comments
Post a Comment