Chapter 11
Chapter 11
Chapter 11
Copyright © McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-
Chapter Summary
Introduction to Trees
Applications of Trees (not currently included
in overheads)
Tree Traversal
Spanning Trees
Minimum Spanning Trees (not currently
included in overheads)
Introduction to Trees
Section 11.1
Section Summary
Introduction to Trees
Rooted Trees
Trees as Models
Properties of Trees
Trees
Definition: A tree is a connected undirected graph with no simple circuits.
Solution: G1 and G2 are trees - both are connected and have no simple circuits.
Because e, b, a, d, e is a simple circuit, G3 is not a tree. G4 is not a tree because it
is not connected.
them (by Theorem 1 of Section 10.4). This path must be unique - for if there
Hence, if x and y are distinct vertices of T, there is a simple path between
Now assume that there is a unique simple path between any two vertices of
a graph T. Then T is connected because there is a path between any two of
its vertices. Furthermore, T can have no simple circuits since if there were
a simple circuit, there would be two paths between some two vertices.
Hence, a graph with a unique simple path between any two vertices is a
tree.
Arthur
Cayley
Trees as Models
(1821-1895)
If v is a vertex of a rooted tree other than the root, the parent of v is the unique vertex u such
that there is a directed edge from u to v. When u is a parent of v, v is called a child of u. Vertices
with the same parent are called siblings.
The ancestors of a vertex are the vertices in the path from the root to this vertex, excluding the
vertex itself and including the root. The descendants of a vertex v are those vertices that have v
as an ancestor.
A vertex of a rooted tree with no children is called a leaf. Vertices that have children are called
internal vertices.
If a is a vertex in a tree, the subtree with a as its root is the subgraph of the tree consisting of a
and its descendants and all edges incident to these descendants.
Terminology for Rooted Trees
Example: In the rooted tree T (with root a):
(i) Find the parent of c, the children of g, the
siblings of h, the ancestors of e, and the
descendants of b.
(ii) Find all internal vertices and all leaves.
(iii) What is the subtree rooted at G?
Solution:
(iv) The parent of c is b. The children of g are h, i,
and j. The siblings of h are i and j. The ancestors
of e are c, b, and a. The descendants of b are c, d,
and e.
(v) The internal vertices are a, b, c, g, h, and j. The
leaves are d, e, f, i, k, l, and m.
(vi) We display the subtree rooted at g.
m-ary Rooted Trees
Definition: A rooted tree is called an m-ary tree if every internal vertex has no more than
Example: Are the following rooted trees full m-ary trees for some positive integer m?
Solution: T1 is a full binary tree because each of its internal vertices has two children. T2 is
a full 3-ary tree because each of its internal vertices has three children. In T3 each internal
vertex has five children, so T3 is a full 5-ary tree. T4 is not a full m-ary tree for any m
because some of its internal vertices have two children and others have three children.
Ordered Rooted Trees
Definition: An ordered rooted tree is a rooted tree where the children of each
internal vertex are ordered.
We draw ordered rooted trees so that the children of each internal vertex are
shown in order from left to right.
Definition: A binary tree is an ordered rooted where where each internal vertex has
at most two children. If an internal vertex of a binary tree has two children, the first
is called the left child and the second the right child. The tree rooted at the left child
of a vertex is called the left subtree of this vertex, and the tree rooted at the right
child of a vertex is called the right subtree of this vertex.
has k − 1 edges.
INDUCTIVE STEP: Assume that every tree with k vertices
Because T has one more edge than T′, we see that T has k
edges. This completes the inductive step.
Counting Vertices in Full m-Ary Trees
Theorem 3: A full m-ary tree with i internal
vertices has n = mi + 1 vertices.
Proof : Every vertex, except the root, is the
child of an internal vertex. Because each of
the i internal vertices has m children, there
vertices.
Counting Vertices in Full m-Ary Trees
(continued)
Theorem 4: A full m-ary tree with
n vertices has i = (n − 1)/m internal
vertices and l = [(m −
(i)
1)n + 1]/m leaves,
(ii) i internal vertices has n = mi + 1 vertices
and l = (m − 1)i + 1
proofs of
leaves,
parts (ii)
vertices and i = (l −
left as
Example:
(i) Find the level of each vertex in
the tree to the right.
(ii) What is the height of the tree?
BASIS STEP: Consider an m-ary trees of height 1. The tree consists of a root and no more than m
Proof (by mathematical induction on height):
children, all leaves. Hence, there are no more than m1 = m leaves in an m-ary tree of height 1.
INDUCTIVE STEP: Assume the result is true for all m-ary trees of height < h. Let T be an m-ary
Each of these subtrees has height ≤ h− 1. By the inductive hypothesis, each of these subtrees has
at most mh− 1 leaves. Since there are at most m such subtees, there are at most m mh− 1 = mh
leaves in the tree.
Corollary 1: If an m-ary tree of height h has l leaves, then h ≥ ⌈logm l⌉. If the m-ary tree is full
and balanced, then h = ⌈logm l⌉. (see text for the proof)
Tree Traversal
Section 11.3
Section Summary
Universal Address Systems (not currently
included in overheads)
Traversal Algorithms
Infix, Prefix, and Postfix Notation
Tree Traversal
Procedures for systematically visiting every
vertex of an ordered tree are called
traversals.
The three most commonly used traversals are
preorder traversal, inorder traversal, and
postorder traversal.
Preorder Traversal
Definition: Let T be an ordered rooted tree
with root r. If T consists only of r, then r is
the preorder traversal of T. Otherwise,
suppose that T1, T2, …, Tn are the subtrees of
r from left to right in T. The preorder
traversal begins by visiting r, and continues
by traversing T1 in preorder, then T2 in
preorder, and so on, until Tn is traversed in
preorder.
Preorder Traversal (continued)
procedure preorder (T: ordered
rooted tree)
r := root of T
list r
for each child c of r from left to right
T(c) := subtree with c as root
preorder(T(c))
Inorder Traversal
Definition: Let T be an ordered rooted tree
with root r. If T consists only of r, then r is
the inorder traversal of T. Otherwise, suppose
that T1, T2, …, Tn are the subtrees of r from
left to right in T. The inorder traversal
begins by traversing T1 in inorder, then
visiting r, and continues by traversing T2 in
inorder, and so on, until Tn is traversed in
inorder.
Inorder Traversal (continued)
procedure inorder (T: ordered rooted
tree)
r := root of T
if r is a leaf then list r
else
l := first child of r from left to right
T(l) := subtree with l as its root
inorder(T(l))
list(r)
for each child c of r from left to right
T(c) := subtree with c as root
inorder(T(c))
Postorder Traversal
Definition: Let T be an ordered rooted tree
with root r. If T consists only of r, then r is
the postorder traversal of T. Otherwise,
suppose that T1, T2, …, Tn are the subtrees of
r from left to right in T. The postorder
traversal begins by traversing T1 in
postorder, then T2 in postorder, and so on,
after Tn is traversed in postorder, r is visited.
Postorder Traversal (continued)
procedure postordered (T: ordered
rooted tree)
r := root of T
for each child c of r from left to right
T(c) := subtree with c as root
postorder(T(c))
list r
Expression Trees
Complex expressions can be represented
4)/3).
A binary tree for the expression can be built
from the bottom up, as is illustrated here.
Infix Notation
An inorder traversal of the tree representing
an expression produces the original
expression when parentheses are included
except for unary operations, which now
immediately follow their operands.
We illustrate why parentheses are needed
with an example that displays three trees all
yield the same infix representation.
Łukasiewicz
Jan
(1878-1956)
is + ↑ + x y 2 / − x 4 3.
Prefix expressions are evaluated by working
from right to left. When we encounter an
operator, we perform the corresponding
operation with the two operations to the right.
Postfix Notation
Example: We
show the steps
used to evaluate
We obtain the postfix form of an expression a particular
by traversing its binary trees in postorder. postfix
Expressions written in postfix form are said expression.
to be in reverse Polish notation.
Parentheses are not needed as the postfix
x y + 2 ↑ x 4 − 3 / + is the postfix
form is unambiguous.
visit(v1)
T := tree consisting only of the vertex v1
and i. These four vertices make up level 1 in the tree. Next, we add the edges from b to a and c,
Solution: We arbitrarily choose vertex e as the root. We then add the edges from e to b, d, f,
these edges not at level 1 are at level 2. Next, add edges from these vertices to adjacent
the edges from d to h, the edges from f to j and g, and the edge from i to k. The endpoints of
level 3 is made up of the vertices l and m. This is the last level because there are no new
vertices not already in the graph. So, we add edges from g to l and from k to m. We see that
vertices to find.
Breadth-First Search Algorithm
We now use pseudocode to describe breadth-
first search.
procedure BFS(G: connected graph with vertices v1, v2,
…, vn)