## Binary Trees

Binary trees are special cases of rooted trees. Binary trees have zero or more nodes. Each node has at most two children. A node without children is called a leaf. This type of tree is the most commonly encountered in practice. That is why we spend additional time with them.

#### 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.

**Example 10.4.1 Distinct Ordered Rooted Trees. **The trees in Figure 10.4.2 are identical rooted trees, with root 1, but as ordered trees, they are different.

**Figure 10.4.2 **Two different ordered rooted trees

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.

**Definition 10.4.3 Binary ****T****ree.**

(1) A tree consisting of no vertices (the empty tree) is a binary tree

(2) A vertex together with two subtrees that are both binary trees is a binary tree. The subtrees are called the left and right subtrees of the binary tree.

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.4 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.

**Figure 10.4.4 **Two different binary trees

**List 10.4.5 Terminology and General Facts about Binary Trees**

(a) A vertex of a binary tree with two empty subtrees is called a **leaf. **All other vertices are called *internal vertices*.

(b) The number of leaves in a binary tree can vary from one up to roughly half the number of vertices in the tree (see Exercise 4 of this section).

(c) The maximum number of vertices at level *k *of a binary tree is 2* ^{k}*,

*k*≥0 (see Exercise 6 of this section).

(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.6.7 of this section for a general fact about full binary trees.

#### 10.4.2 Traversals of Binary Trees

The traversal of a binary tree consists of visiting each vertex of the tree in some prescribed order. Unlike graph traversals, the consecutive vertices that are visited are not always connected with an edge. The most common binary tree traversals are differentiated by the order in which the root and its subtrees are visited. The three traversals are best described recursively and are:

**Preorder Traversal:**

(1) Visit the root of the tree.

(2) Preorder traverse the left subtree.

(3) Preorder traverse the right subtree.

**Inorder Traversal:**

(1) Inorder traverse the left subtree.

(2) Visit the root of the tree.

(3) Inorder traverse the right subtree.

**P****ostorder Traversal:**

(1) Postorder traverse the left subtree.

(2) Postorder traverse the right subtree.

(3) Visit the root of the tree.

Any traversal of an empty tree consists of doing nothing.

**Example 10.4.6 Traversal Examples. **For the tree in Figure 10.4.7, 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.

**Figure 10.4.7 **A Complete Binary Tree to Level 2

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*_{1}, *a*_{2}*, . . .*, *a** _{n}*,

*n*≥ 1, we first execute the following algorithm that creates a binary tree:

**Algorithm 10.4.8 Binary ****Sort Tree Creation.**

*(1) Insert a*_{1} i*nto the root of the tree.*

*(2) For k := 2 to n // insert a*_{k }*into the tree*

*(a) r = a*_{1}

*(b) inserted = false*

*(c) while not(inserted):*

*if a*_{k }*< r:*

*if r has a left child:*

*r = left child of r*

*else:*

*make a*_{k }*the left child of r*

*inserted = true*

*else:*

*if r has a right child:*

*r = right child of r*

*else:*

*make a*_{k }*the right child of r*

*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.9. 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.

**Figure 10.4.9 **A Binary Sorting Tree

#### 10.4.3 Expression Trees

A convenient way to visualize an algebraic expression is by its expression tree. Consider the expression

*X *= *a **∗ **b **− **c */ *d* + *e.*

Since it is customary to put a precedence on multiplication/divisions, *X *is evaluated as ((*a **∗ **b*) *− *(*c */ *d*)) + *e*. Consecutive multiplication/divisions or addition/subtractions are evaluated from left to right. We can analyze *X *further by noting that it is the sum of two simpler expressions (*a **∗ **b*) *− *(*c */ *d*) and *e*. The first of these expressions can be broken down further into the difference of the expressions *a **∗ **b *and *c */*d*. When we decompose any expression into (left expression)operation(right expression), the expression tree of that expression is the binary tree whose root contains the 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.10.

**Figure 10.4.10 **Building an Expression Tree

**Example 10.4.11 Some Expression ****T****rees.**

(a) If we intend to apply the addition and subtraction operations in *X *first, we would parenthesize the expression to *a **∗ *(*b **− **c*) / (*d* + *e*). Its expression tree appears in Figure 10.4.12(a).

(b) The expression trees for *a*^{2 }*− **b*^{2} and for (*a *+ *b*) *∗ *(*a **− **b*) appear in Figure 10.4.12(b) and Figure 10.4.12(c).

**Figure 10.4.12 **Expression Tree Examples

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:

**List 10.4.13 Prefix and postfix forms of an algebraic expression**

**Prefix**

(a) A variable or number is a prefix expression

(b) Any operation followed by a pair of prefix expressions is a prefix expression.

**P****ostfix**

(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.

**Example 10.4.14 Traversing ****an Expression Tree. **The preorder traversal of the tree in Figure 10.4.10 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.

#### 10.4.4 Counting Binary Trees

We close this section with a formula for the number of different binary trees with *n *vertices. The formula is derived using generating functions. Although the complete details are beyond the scope of this text, we will supply an overview of the derivation in order to illustrate how generating functions are used in advanced combinatorics.

Let *B*(*n*) be the number of different binary trees of size *n *(*n *vertices), *n *≥ 0. By our definition of a binary tree, *B*(0) = 1. Now consider any positive integer *n *+ 1, *n *≥ 0. A binary tree of size *n *+ 1 has two subtrees, the sizes of which add up to *n*. The possibilities can be broken down into *n *+ 1 cases:

Case 0: Left subtree has size 0; right subtree has size *n*.

Case 1: Left subtree has size 1; right subtree has size *n **− *1.

...

Case *k*: Left subtree has size *k*; right subtree has size *n **− **k*.

...

Case *n*: Left subtree has size *n*; right subtree has size 0.

In the general Case *k*, we can count the number of possibilities by multiplying the number of ways that the left subtree can be filled, *B*(*k*), by the number of ways that the right subtree can be filled. *B*(*n **− **k*). Since the sum of these products equals *B*(*n *+ 1), we obtain the recurrence relation for *n *≥ 0:

Now take the generating function of both sides of this recurrence relation:

or

Recall that . If we abbreviate *G*(*B*; *z*) to *G*, we get

Using the quadratic equation we find two solutions:

The gap in our derivation occurs here since we don't presume a knowledge of calculus. If we expand *G*_{1} as an extended power series, we find

The coefficients after the first one are all negative and there is a singularity at 0 because of the term. However if we do the same with *G*_{2} we get

Further analysis leads to a closed form expression for *B*(*n*), which is

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.

#### 10.4.5 SageMath Note - Power Series

It may be of interest to note how the extended power series expansions of *G*_{1} and *G*_{2} are determined using Sage. In Sage, one has 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*_{1} above.

R . < z > = P o w e r S e r i e s R i n g ( ZZ , ' z ' )

G 1 = ( 1 + s q r t ( 1 - 4 * z ) ) / ( 2 * z )

G 1

z ^ -1 - 1 - z - 2* z ^2 - 5* z ^3 - 14* z ^4 - 42* z ^5 - 132* z ^6 - 429* z ^7 - 1430* z ^8 - 4862* z ^9 - 16796* z ^10 - 58786* z ^11 - 208012* z ^12 - 742900* z ^13 - 2674440* z ^14 - 9694845* z ^15 - 35357670* z ^16 - 129644790* z ^17 - 477638700* z ^18 + O ( z ^19)

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*2 uses identical code, and its coefficients are the values of *B*(*n*).

R . = PowerSeriesRing ( ZZ , ' z ' )

G2 =(1 - sqrt (1 -4* z ) ) /(2* z )

G2

1 + z + 2* z ^2 + 5* z ^3 + 14* z ^4 + 42* z ^5 + 132* z ^6 + 429* z ^7 + 1430* z ^8 + 4862* z ^9 + 16796* z ^10 + 58786* z ^11 + 208012* z ^12 + 742900* z ^13 + 2674440* z ^14 + 9694845* z ^15 + 35357670* z ^16 + 129644790* z ^17 + 477638700* z ^18 + O ( z ^19)

Source: Al Doerr and Ken Levasseur, http://faculty.uml.edu/klevasseur/ads-latex/ads.pdf

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License.