11 W11 DM
11 W11 DM
Copyright © McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill Education.
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)
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.
Trees
Definition: A forest is a graph that has no simple
circuit, but is not connected. Each of the connected
components in a forest is a tree.
Trees (continued)
Theorem: An undirected graph is a tree if and only if there is a unique simple
path between any two of its vertices.
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
(1821-1895)
Trees as Models
Trees are used as models in computer
science, chemistry, geology, botany,
psychology, and many other areas.
Trees were introduced by the mathematician
Cayley in 1857 in his work counting the
number of isomers of saturated
hydrocarbons. The two isomers of butane are
shown at the right.
The organization of a computer file system
into directories, subdirectories, and files is
naturally represented as a tree.
Trees are used to represent the structure of
organizations.
Rooted Trees
Definition: A rooted tree is a tree in which one vertex
has been designated as the root and every edge is
directed away from the root.
An unrooted tree is converted into different rooted
trees when different vertices are chosen as the root.
Rooted Tree Terminology
Terminology for rooted trees is a
mix from botany and
genealogy (such as this family tree
of the Bernoulli family of
mathematicians).
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
Solution:
(i) 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.
(ii) The internal vertices are a, b, c, g, h, and j. The
leaves are d, e, f, i, k, l, and m.
(iii) 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 m children. The tree is called a full m-ary tree if every internal vertex
has exactly m children. An m-ary tree with m = 2 is called a binary tree.
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.
Solution:
(i) The root a is at level 0. Vertices b, j, and k are at level 1.
Vertices c, e, f, and l are at level 2. Vertices d, g, i, m, and n are at level 3.
Vertex h is at level 4.
(ii) The height is 4, since 4 is the largest level of any vertex.
Balanced m-Ary Trees
Definition: A rooted m-ary tree of height h is
balanced if all leaves are at levels h or h − 1.
Example: Which of the rooted trees shown below is
balanced?
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
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 using ordered
rooted trees.
Consider the expression ((x + y) ↑ 2 ) + ((x − 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.
Jan Łukasiewicz
(1878-1956)
Solution: The graph is connected, but is not a tree because it contains simple
circuits. Remove the edge {a, e}. Now one simple circuit is gone, but the
remaining subgraph still has a simple circuit. Remove the edge {e, f} and then
the edge {c, g} to produce a simple graph with no simple circuits. It is a
spanning tree, because it contains every vertex of the original graph.
Spanning Trees (continued)
Theorem: A simple graph is connected if and only if it has a
spanning tree.
Proof: Suppose that a simple graph G has a spanning tree T. T
contains every vertex of G and there is a path in T between any
two of its vertices. Because T is a subgraph of G, there is a path in
G between any two of its vertices. Hence, G is connected.
Now suppose that G is connected. If G is not a tree, it contains a
simple circuit. Remove an edge from one of the simple circuits.
The resulting subgraph is still connected because any vertices
connected via a path containing the removed edge are still
connected via a path with the remaining part of the simple
circuit. Continue in this fashion until there are no more simple
circuits. A tree is produced because the graph remains connected
as edges are removed. The resulting tree is a spanning tree
because it contains every vertex of G.
Depth-First Search
To use depth-first search to build a spanning tree for a connected
simple graph first arbitrarily choose a vertex of the graph as the
root.
Form a path starting at this vertex by successively adding vertices
and edges, where each new edge is incident with the last vertex in
the path and a vertex not already in the path. Continue adding
vertices and edges to this path as long as possible.
If the path goes through all vertices of the graph, the tree consisting
of this path is a spanning tree.
Otherwise, move back to the next to the last vertex in the path, and
if possible, form a new path starting at this vertex and passing
through vertices not already visited. If this cannot be done, move
back another vertex in the path.
Repeat this procedure until all vertices are included in the spanning
tree.
Depth-First Search (continued)
Example: Use depth-first search
to find a spanning tree of this graph.
Solution: We arbitrarily choose vertex e as the root. We then add the edges from e to b, d, f, and i.
These four vertices make up level 1 in the tree. Next, we add the edges from b to a and c, the edges
from d to h, the edges from f to j and g, and the edge from i to k. The endpoints of these edges not at
level 1 are at level 2. Next, add edges from these vertices to adjacent vertices not already in the graph.
So, we add edges from g to l and from k to m. We see that level 3 is made up of the vertices l and m.
This is the last level because there are no new vertices to find.
Depth-First Search in Directed
Graphs
Both depth-first search and breadth-first search can be
easily modified to run on a directed graph. But the
result is not necessarily a spanning tree, but rather a
spanning forest.