10.04__Binary_Trees
10.04__Binary_Trees
4: Binary Trees
10.4.1: Definition of a Binary Tree
An ordered rooted tree is a rooted tree whose subtrees are put into a definite order and are, themselves, ordered rooted trees. An
empty tree and a single vertex with no descendants (no subtrees) are ordered rooted trees.
The trees in Figure 10.4.1 are identical rooted trees, with root 1, but as ordered trees, they are different.
If a tree rooted at v has p subtrees, we would refer to them as the first, second,..., p th
subtrees. There is a subtle difference between
certain ordered trees and binary trees, which we define next.
The difference between binary trees and ordered trees is that every vertex of a binary tree has exactly two subtrees (one or both of
which may be empty), while a vertex of an ordered tree may have any number of subtrees. But there is another significant
difference between the two types of structures. The two trees in Figure 10.4.2 would be considered identical as ordered trees.
However, they are different binary trees. Tree (a) has an empty right subtree and Tree (b) has an empty left subtree.
c. The maximum number of vertices at level k of a binary tree is 2 , k ≥ 0 (see Exercise 10.4.6 of this section).
k
10.4.1 https://math.libretexts.org/@go/page/80543
d. A full binary tree is a tree for which each vertex has either zero or two empty subtrees. In other words, each vertex has
either two or zero children. See Exercise 10.4.7 of this section for a general fact about full binary trees.
For the tree in Figure 10.4.3, the orders in which the vertices are visited are:
A-B-D-E-C-F-G, for the preorder traversal.
D-B-E-A-F-C-G, for the inorder traversal.
D-E-B-F-G-C-A, for the postorder traversal.
Binary Tree Sort. Given a collection of integers (or other objects than can be ordered), one technique for sorting is a binary tree
sort. If the integers are a , a , … , a , n ≥ 1, we first execute the following algorithm that creates a binary tree:
1 2 n
10.4.2 https://math.libretexts.org/@go/page/80543
Algorithm 10.4.1: Binary Sort Tree Creation
a. r = a
1
b. inserted = false
c. while not(inserted):
if a < r:
k
inserted = true
else:
if r has a right child:
r = right child of r
else:
make a the right child of r
k
inserted = true
If the integers to be sorted are 25, 17, 9, 20, 33, 13, and 30, then the tree that is created is the one in Figure 10.4.4. The inorder
traversal of this tree is 9, 13, 17, 20, 25, 30, 33, the integers in ascending order. In general, the inorder traversal of the tree that is
constructed in the algorithm above will produce a sorted list. The preorder and postorder traversals of the tree have no meaning
here.
X = a ∗ b − c/d + e.
operation and whose left and right subtrees are the trees of the left and right expressions, respectively. Additionally, a simple
variable or a number has an expression tree that is a single vertex containing the variable or number. The evolution of the
expression tree for expression X appears in Figure 10.4.5.
10.4.3 https://math.libretexts.org/@go/page/80543
Figure 10.4.5 : Building an Expression Tree
b. The expression trees for a − b and for (a + b) ∗ (a − b) appear in Figure 10.4.6(b) and Figure 10.4.6(c).
2 2
10.4.4 https://math.libretexts.org/@go/page/80543
The three traversals of an operation tree are all significant. A binary operation applied to a pair of numbers can be written in three
ways. One is the familiar infix form, such as a + b for the sum of a and b. Another form is prefix, in which the same sum is written
+ab. The final form is postfix, in which the sum is written ab + . Algebraic expressions involving the four standard arithmetic
operations (+, −, ∗, and/) in prefix and postfix form are defined as follows:
Prefix
a. A variable or number is a prefix expression
b. Any operation followed by a pair of prefix expressions is a prefix expression.
Postfix
a. A variable or number is a postfix expression
b. Any pair of postfix expressions followed by an operation is a postfix expression.
The connection between traversals of an expression tree and these forms is simple:
a. The preorder traversal of an expression tree will result in the prefix form of the expression.
b. The postorder traversal of an expression tree will result in the postfix form of the expression.
c. The inorder traversal of an operation tree will not, in general, yield the proper infix form of the expression. If an expression
requires parentheses in infix form, an inorder traversal of its expression tree has the effect of removing the parentheses.
The preorder traversal of the tree in Figure 10.4.5 is + − ∗ab/cde, which is the prefix version of expression X. The postorder
traversal is ab ∗ cd/ − e + . Note that since the original form of X needed no parentheses, the inorder traversal,
a ∗ b − c/d + e, is the correct infix version.
= ∑ B(k)B(n − k)
k=0
10.4.5 https://math.libretexts.org/@go/page/80543
Now take the generating function of both sides of this recurrence relation:
∞ ∞ n
n n
∑ B(n + 1)z = ∑ ( ∑ B(k)B(n − k)) z (10.4.1)
or
2
G(B ↑; z) = G(B ∗ B; z) = G(B; z) (10.4.2)
G(B;z)−B(0) G(B;z)−1
Recall that G(B ↑; z) = z
=
z
If we abbreviate G(B; z) to G, we get
G−1 2 2
=G ⇒ zG −G+1 = 0
z
The gap in our derivation occurs here since we don't presume a knowledge of calculus. If we expand G1 as an extended power
series, we find
− −−− −
1 + √ 1 − 4z 1
2 3 4 5
G1 = = − 1 − z − 2z − 5z − 14 z − 42 z +⋯ (10.4.5)
2z z
The coefficients after the first one are all negative and there is a singularity at 0 because of the 1
z
term. However if we do the same
with G we get
2
− −−− −
1 − √ 1 − 4z
2 3 4 5
G2 = = 1 + z + 2z + 5z + 14 z + 42 z +⋯ (10.4.6)
2z
This sequence of numbers is often called the Catalan numbers. For more information on the Catalan numbers, see the entry
A000108 in The On-Line Encyclopedia of Integer Sequences.
the capability of being very specific about how algebraic expressions should be interpreted by specifying the underlying ring. This
can make working with various algebraic expressions a bit more confusing to the beginner. Here is how to get a Laurent expansion
for G above.
1
1 R.<z>=PowerSeriesRing(ZZ,'z')
2 G1=(1+sqrt(1-4*z))/(2*z)
3 G1
The first Sage expression above declares a structure called a ring that contains power series. We are not using that whole structure,
just a specific element, G1 . So the important thing about this first input is that it establishes z as being a variable associated
with power series over the integers. When the second expression defines the value of G1 in terms of z , it is automatically
converted to a power series.
The expansion of G uses identical code, and its coefficients are the values of B(n).
2
In Chapter 16 we will introduce rings and will be able to take further advantage of Sage's capabilities in this area.
10.4.6 https://math.libretexts.org/@go/page/80543
10.4.6: Exercises
Exercise 10.4.1
Answer
Figure 10.4.7
Figure 10.4.8
Exercise 10.4.2
b. xy + xz + yz
10.4.7 https://math.libretexts.org/@go/page/80543
Exercise 10.4.3
Write out the preorder, inorder, and postorder traversals of the trees in Exercise 10.4.1 above.
Answer
Preorder Inorder Postorder
(a) ⋅a + bc a⋅ b +c abc + ⋅
(b) + ⋅ abc a⋅ b +c ab ⋅ c+
(c) + ⋅ ab ⋅ ac a⋅ b +a⋅ c ab ⋅ ac ⋅ +
Exercise 10.4.4
Verify the formula for B(n), 0 ≤ n ≤ 3 by drawing all binary trees with three or fewer vertices.
Exercise 10.4.5
a. Draw a binary tree with seven vertices and only one leaf.
b. Draw a binary tree with seven vertices and as many leaves as possible.
Answer
Add texts here. Do not delete this text first.
Figure 10.4.9
Exercise 10.4.6
Exercise 10.4.7
Prove that if T is a full binary tree, then the number of leaves of T is one more than the number of internal vertices (non-
leaves).
Answer
Solution 1:
Basis: A binary tree consisting of a single vertex, which is a leaf, satisfies the equation leaves = internal vertices + 1
Induction: Assume that for some k ≥ 1 , all full binary trees with k or fewer vertices have one more leaf than internal
vertices. Now consider any full binary tree with k + 1 vertices. Let T and T be the left and right subtrees of the tree
A B
which, by the definition of a full binary tree, must both be full. If i and i are the numbers of internal vertices in T and
A B A
T , and j
B and j are the numbers of leaves, then j = i + 1 and j = i + 1 . Therefore, in the whole tree,
A B A A B B
10.4.8 https://math.libretexts.org/@go/page/80543
the number of leaves = jA + jB
= (iA + 1) + (iB + 1)
= (iA + iB + 1) + 1
Solution 2:
Imagine building a full binary tree starting with a single vertex. By continuing to add leaves in pairs so that the tree stays
full, we can build any full binary tree. Our starting tree satisfies the condition that the number of leaves is one more than the
number of internal vertices . By adding a pair of leaves to a full binary tree, an old leaf becomes an internal vertex,
increasing the number of internal vertices by one. Although we lose a leaf, the two added leaves create a net increase of one
leaf. Therefore, the desired equality is maintained.
This page titled 10.4: Binary Trees is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Al Doerr & Ken
Levasseur via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.
10.4.9 https://math.libretexts.org/@go/page/80543