0% found this document useful (0 votes)
8 views50 pages

Chap9 Complete

Chapter 9 discusses trees in graph theory, defining them as connected undirected graphs without circuits and introducing concepts such as rooted trees, ordered rooted trees, and their properties. It covers tree traversal methods including preorder, inorder, and postorder traversals, as well as the representation of expressions using infix, prefix, and postfix notations. The chapter also explores spanning trees and search algorithms like depth-first and breadth-first search.

Uploaded by

Nazetta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views50 pages

Chap9 Complete

Chapter 9 discusses trees in graph theory, defining them as connected undirected graphs without circuits and introducing concepts such as rooted trees, ordered rooted trees, and their properties. It covers tree traversal methods including preorder, inorder, and postorder traversals, as well as the representation of expressions using infix, prefix, and postfix notations. The chapter also explores spanning trees and search algorithms like depth-first and breadth-first search.

Uploaded by

Nazetta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Chapter 9: Trees

2022

1/50
Outline related to network computer

Trees
Trees
Rooted Trees
Ordered Rooted Trees
Properties of Trees
Tree Traversal
Preorder Traversal
Inorder Traversal
Postorder Traversal
Infix, Prefix, and Postfix Notation
Spanning Trees
Spanning Trees
Depth-First Search
Breadth-First Search
Depth-First Search in Directed Graphs

2/50
Trees
I A tree is a connected undirected graph with no simple
circuits.

I G1 and G2 are trees


I G3 is not a tree because e, b, a, d, e is a simple circuit in
this graph
I G4 is not a tree because it is not connected.
3/50
Trees
I 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.

Figure: Example of a forest

I An undirected graph is a tree if and only if there is a


unique simple path between any two of its vertices.
4/50
Rooted Trees Y
of
is the
v and w
parents

gu ↑
v and w is child

I A rooted tree is a tree in which one vertex has been


of u

y z
designated as the root and every edge is directed away and
the of y
ancestors
from the root. I and
are u .
W

I If there is a directed edge from u to v , then u is the


parent of v and v is the child of vf.
I Vertices with the same parent are called siblings.
I The ancestors of a vertex other than the root are the
vertices in the path from the root to this vertex,
excluding the vertex itself and including the root.
I The descendants of a vertex v are those vertices that
have v as an ancestor. leaf is y and
r
,
w

I A vertex of a rooted tree is called a leaf if it has no


children.
I Vertices that have children are called internal vertices.
5/50
Rooted Trees
Example Solution:
Find the parent of c, the children
I The parent of c is b. The
of g , the siblings of h, all
ancestors of e, all descendants of children of g are h, i, and j.
b, all internal vertices, and all I The siblings of h are i and j.
leaves.
I The ancestors of e are c, b,
and a
I The descendants of b are
c, d, and e.
I The internal vertices are
a, b, c, g , h, and j.
I The leaves are d, e, f , i, k, l,
and m.
6/50
Rooted Trees

I 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.
I The subtree rooted at g of the tree in previous example is

7/50
Rooted Trees
I 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.
free
full trianary
full
O g
tree
binary
O S O O

O 8

m= 3
MY 2

I T1 is a full binary tree because each of its internal


vertices has two children.
I T2 is a full 3-ary tree because each of its internal vertices
has three children.
8/50
Rooted Trees

I In T3 each internal vertex has five children, so T3 is a full


5-ary tree. m = 5

I 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.
9/50
Ordered Rooted Trees

I An ordered rooted tree is a rooted tree where the


children of each internal vertex are ordered.
I Ordered rooted trees are drawn so that the children of
each internal vertex are shown in order from left to right.
I If an internal vertex has two children, the first child is
called the left child and the second child is called the
right child.
I 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 the
vertex.

10/50
Ordered Rooted Trees

I the left subtree of c

G
inter

I the right subtree of c

I The left child of d is f


and the right child is g .

11/50
Properties of Trees
nevertices
vertices
is internal
&E leaves mem-ary
I A tree with n vertices has n 1 edges. ↑
tree

I A full m-ary tree with i internal vertices contains


n = mi + 1 vertices.
I A full m-ary tree with
(i) n vertices has i = (n 1)/m internal vertices and
l = [(m 1)n + 1]/m leaves,
(ii) i internal vertices has n = mi + 1 vertices and
l = (m 1)i + 1 leaves,
(iii) l leaves has n = (ml 1)/(m 1) vertices and
i = (l 1)/(m 1) internal vertices.

12/50
Properties of Trees
Example
Suppose that someone starts a chain letter. Each person who
4
-

any
receives the letter is asked to -
send it on to four other people.
Some people do this, but others do not send any letters. How
many people have seen the letter, including the first person, if no
one receives more than one letter and if the chain letter ends after
there have been 100 people who read it but did not send it out?
How many peopleleaf sent out the letter?

Solution: The chain letter can be represented using a 4-ary tree.


The internal vertices correspond to people who sent out the letter,
and the leaves correspond to people who did not send it out.
Because 100 people did not send out the letter, the number of
leaves in this rooted tree is l = 100. Hence, the number of people
who have seen the letter is n = (4 · 100 1)/(4 1) = 133. Also,
the number of internal vertices is 133 100 = 33, so 33 people
sent out the letter. 13/50
Properties of Trees

I The level of a vertex v in a rooted tree is the length of


the unique path from the root to this vertex.
I The level of the root is defined to be zero.
I The height of a rooted tree is the maximum of the levels
of vertices.

14/50
Properties of Trees
Example
Find the level of each vertex in I The root a is at level 0.
the rooted tree shown below. I Vertices b, j, and k are at
What is the height of this tree?
level 1.
I Vertices c, e, f , and l are
I at level 2.
I Vertices d, g , i, m, and n
2 are at level 3.
I Vertex h is at level 4.
3
Because the largest level
of any vertex is 4, this
4
tree has height 4.

15/50
Properties of Trees
I A rooted m-ary tree of height h is balanced if all leaves
- -

are at levels h or h 1.
trees
binary

&

I T1 is balanced, because all its leaves are at levels 3 and 4.


I T2 is not balanced, because it has leaves at levels 2, 3,
and 4.
16/50
Tree Traversal
I Ordered rooted trees are often used to store information.
I We need procedures for visiting each vertex of an ordered
rooted tree to access data.
I We will describe several important algorithms for visiting
all the vertices of an ordered rooted tree.
I Ordered rooted trees can also be used to represent
various types of expressions, such as arithmetic
expressions involving numbers, variables, and operations.
I Procedures for systematically visiting every vertex of an
ordered rooted tree are called traversal algorithms.
I We will describe three of the most commonly used such
algorithms, preorder traversal, inorder traversal, and
postorder traversal.
17/50
Preorder Traversal
I Let T be an ordered
rooted tree with root r . If
T consists only of r , then
r is the preorder traversal
of T .
I Otherwise, suppose that
T1 , T2 , . . . , Tn are the
subtrees at r from left to
right in T .
I The preorder traversal
begins by visiting r .
I It continues by traversing
T1 in preorder, then T2 in
preorder, and so on, until
Tn is traversed in preorder.
18/50
Preorder Traversal
I 2
Example: In which order
does a preorder traversal visit
the vertices in the ordered
rooted tree T shown below.
I

2
Il
10 11
1215 16
3 3
12
9 Id
15 a 10

4 513
14 14
13


5
678

Solution: Visit root, visit g


6
7
subtrees left to right.
leaves
19/50
Inorder Traversal
I Let T be an ordered rooted tree with root r .
I If T consists only of r , then r is the inorder traversal of
T.
I Otherwise, suppose that T1 , T2 , . . . , Tn are the subtrees
at r from left to right.
I The inorder traversal begins by traversing T1 in inorder,
then visiting r .
I It continues by traversing T2 in inorder, then T3 in
inorder, . . . , and finally Tn in inorder.

20/50
Inorder Traversal
Example:
In which order does an inorder
traversal visit the vertices of
the ordered rooted tree T ?
A

O
A
14
O 100
z

O 8
82 15 16

I
04 Il
13

Y
3 5

leave
Solution: Visit first subtree,
root, the rest of subtrees from
left to right
21/50
Postorder Traversal
I Let T be an ordered rooted tree with root r . If T
consists only of r , then r is the postorder traversal of T .
I Otherwise, suppose that T1 , T2 , . . . , Tn are the subtrees
at r from left to right.
I The postorder traversal begins by traversing T1 in
postorder, then T2 in postorder, . . . , then Tn in
postorder, and ends by visiting r .
1
n+

I
z
M

22/50
Postorder Traversal
Example: In which order
does an postorder traversal
visit the vertices of the
ordered rooted tree T ?
16

g i
&

6
12
F 14
13
I 5 Il
18

4
23

Solution: Visit subtrees from


left to right, the lastly root.
23/50
Shortcut for Traversing an Ordered Rooted Tree
I list the vertices in preorder by
listing each vertex the first time
this curve passes it:
a, b, d, h, e, i, j, c, f , g , k ④

I list the vertices in inorder by
listing a leaf the first time the 2 g

curve passes it and listing each ⑥ ⑨



internal vertex the second time

the curve passes it:
h, d, b, i, e, j, a, f , c, k, g
a voi
srrsvs

go
rgrs v

I list the vertices in postorder by 04 6


listing a vertex the last time it ·
is passed on the way back up to
its parent:
h, d, i, j, e, b, f , k, g , c, a.
srssrusss ru 24/50
Infix, Prefix, and Postfix Notation
In order preorder postorder
I We can represent complicated expressions, such as
compound propositions, combinations of sets, and
arithmetic expressions using ordered rooted trees.
I For instance, consider the representation of an arithmetic
expression involving the operators O+ (addition), O
(subtraction), O
* (multiplication), / (division), andO
"
(exponentiation). L)
I We will use parentheses to indicate the order of the
operations. (5+ 2) * (5 2)
-

I An ordered rooted tree can be used to represent such


expressions, where the internal vertices represent
operations, and the leaves represent the variables or
numbers. Each operation operates on its left and right
subtrees (in that order).
25/50
Infix, Prefix, and Postfix Notation
Solution: We can build the tree
I The usual notation for for this expression from the
writing arithmetic expression bottom up. First for (x + y ).
(the notation we learned i
O-
since primary school) is 5 + 2 t

called infix notation.


1
I In infix notation, the 5 2

operator is written between


the operands. Next, for (x + y ) " 2

Example
What is the ordered rooted tree
that represents the expression
& ((x + y ) " 2) + ((x 4)/3)?
A A ↑
Proot
(n +
y) + 2 26/50
Infix, Prefix, and Postfix Notation
Next, for (x 4) Finally, for
((x + y ) " 2) + ((x 4)/3)
* always read back

Next, for (x 4)/3

Remark: The expression


obtained when we traverse
parentreses
the
is
very important
tree in inorder is in infix form,
i.e. ((x + y ) " 2) + ((x 4)/3)
27/50
Infix, Prefix, and Postfix Notation
Example
What is the ordered rooted trees that represents the expressions
(x + y )/(x + 3), (x + (y /x)) + 3, and x + (y /(x + 3))?

root ↑
roof
Solution: root

Figure: Rooted Trees representing (x + y )/(x + 3),


(x + (y /x)) + 3, and x + (y /(x + 3)) respectively

28/50
Infix, Prefix, and Postfix Notation
for in case of infix form

I In infix form, parentheses are important


I For example, without parentheses, inorder traversals for
the following trees lead to the same infix expression, i.e.
x + y /x + 3

Figure: Rooted Trees Representing


(x + y )/(x + 3), (x + (y /x)) + 3, and x + (y /(x + 3))

29/50
Infix, Prefix, and Postfix Notation

I We obtain the prefix form of an expression when we


traverse its rooted tree in preorder.
I In prefix notation, operators are written before the
operands, e.g. +xy . valid in prefix form
I Expressions written in prefix form are said to be in Polish
notation.
I No parentheses are needed in prefix form.

30/50
Infix, Prefix, and Postfix Notation
Example
What is the prefix form for ((x + y ) " 2) + ((x 4)/3)?
I & infix form

preordea
7

+ + + ny2) -
243

3 6 g II

4
59 10

Solution:
prefix form for this expression is + " + x y 2 / x 4 3.
31/50
Infix, Prefix, and Postfix Notation prefix form :

we start from right


to left
Solution: infix form
:
at
looking
start by
I In the prefix form of an we
the bracket
-
expression, since operators #
are written before +

operands, we can evaluate



an expression in prefix u

form by working from or


right to left.
O
Example
What is the value of the prefix O
X
expression + *2 3 5 / " 2 3 4?

32/50
Infix, Prefix, and Postfix Notation
Example
What is the postfix form for
I We obtain the postfix ((x + y ) " 2) + ((x 4)/3)?
form of an expression
11
when we traverse its
rooted tree in postorder. 5
I Expressions written in 10

postfix form are said to be


3 4
in reverse Polish g a

notation.
S I

I No parentheses are needed


6 7
in postfix form.

Solution:
xy + 2 " x4 3/ + .
33/50
Infix, Prefix, and Postfix Notation
Solution:
postfix :
left to right
BE CAREFUL
I In the postfix form of an -
expression, operators, such
S
as +, follows its operands.
Y

I Hence, we can evaluate an


O
expression in postfix form by C
working from left to right.
O
-
Example
What is the value of the postfix
expression 7 2 3 * 4 " 9 3 / +?

34/50
Infix, Prefix, and Postfix Notation

71v

I Rooted trees can be used to represent other types of


expressions, such as those representing compound
propositions and combinations of sets.
I In these examples unary operators, such as the negation
of a proposition, occur. To represent such operators and
their operands, a vertex representing the operator and a
child of this vertex representing the operand are used.

35/50
Infix, Prefix, and Postfix Notation
Solution:


g

Example ⑥
7

Find the ordered rooted


tree representing the
G O
compound proposition 3

(¬(p ^ q)) $ (¬p _ ¬q). ⑤


Then use this rooted tree e

to find the prefix, postfix,


brackets matter

and infix forms of this


'I
expression. I prefix: $ ¬ ^ pq _ ¬p¬q,

I postfix: pq ^ ¬p¬q¬_ $,

I infix: (¬(p ^ q)) $ ((¬p) _ (¬q)).


36/50
Spanning Trees
I Let G be a simple graph. A spanning tree of G is a
subgraph of G that is a tree containing every vertex of G .

Example
Find a spanning tree of the simple graph G .
tages
remove

Solution:
Graph G is not a tree because it contains simple circuits.
37/50
Spanning Trees
Removing edge {a, e}
Removing edge {c, g }

Removing edge {e, f }


Remark: The tree above is not
the only spanning tree of G .
There are many spanning trees
can be produced a graph.

38/50
Spanning Trees
For example, each of the trees shown below is a spanning tree
of G .

39/50
Depth-First Search

I Instead of constructing spanning trees by removing edges,


spanning trees can be built up by successively adding
edges.
Depth-First Search
I Arbitrarily choose a vertex of the graph as the root.
I 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.

40/50
Depth-First Search

I If the path goes through all vertices of the graph, the tree
consisting of this path is a spanning tree. However, if the
path does not go through all vertices, more vertices and edges
must be added.
I Move back to the next to last vertex in the path, and, if
possible, form a new path starting at this vertex passing
through vertices that were not already visited. If this cannot
be done, move back another vertex in the path, that is, two
vertices back in the path, and try again. Keep on repeating
this procedure.

41/50
vertices must be
Depth-First Search mentioned once
.
only

Example
Use depth-first
-
search to find a spanning tree for the graph G .
'

said start from reverse


~
What if requestion : ·

alphabetical
- "Follow 10
then reverse to
the incident
check
vertices in the
orde
path
.

Il z

there are many


solutions
possible
Solution:
I arbitrarily start with the vertex f (see figure (a))
I A path is built by successively adding edges incident with
vertices not already in the path, as long as this is
possible, i.e. f , g , h, k, j (see figure (b)).
42/50
Depth-First Search
I Next, backtrack to k. There is no path beginning at k
containing vertices not already visited. So we backtrack to h.
Form the path h, i (see figure (c)).
I Then backtrack to h, and then to f . From f build the path
f , d, e, c, a (see figure (d)).
I Then backtrack to c and form the path c, b (see figure (e)).
This produces the spanning tree.

43/50
Depth-First Search
I The edges selected by depth-first search of a graph are
called tree edges.
I All other edges of the graph must connect a vertex to an
ancestor or descendant of this vertex in the tree. These
edges are called back edges.

Figure: The Tree Edges (highlighted lines) and Back Edges of the
Depth-First Search

44/50
Breadth-First Search

I Arbitrarily choose a root from the vertices of the graph.


~

Then add all edges incident to this vertex. The new


vertices added at this stage become the vertices at level 1
in the spanning tree. Arbitrarily order them.
I Next, for each vertex at level 1, visited in order, add each
edge incident to this vertex to the tree as long as it does
not produce a simple circuit. Arbitrarily order the children
of each vertex at level 1. This produces the vertices at
level 2 in the tree.
I Follow the same procedure until all the vertices in the
tree have been added.

45/50
Breadth-First Search

Example
I We choose the vertex e to
Use breadth-first search to find a be the root. Then we add
spanning tree for the graph edges incident with all
shown below. vertices adjacent to e, so
edges from e to b, d, f , and
i are added. These four
vertices are at level 1 in the
tree.

after e
Clevel 2)

46/50
Breadth-First Search
I Next, add the edges from these vertices at level 1 to adjacent
vertices not already in the tree. Hence, the edges from b to a
and c are added, as are edges from d to h, from f to j and g ,
and from i to k. The new vertices a, c, h, j, g , and k are at
level 2.
I Next, add edges from these vertices to adjacent vertices not
already in the graph. This adds edges from g to l and from k
to m.

47/50
Depth-First Search in Directed Graphs

I We can easily modify both depth-first search and


breadth-first search so that they can run given a directed
graph as input.
I However, the output will not necessarily be a spanning
tree, but rather a spanning forest.
I In both algorithms we can add an edge only when it is
directed away from the vertex that is being visited and to
a vertex not yet added.
I If at a stage of either algorithm we find that no edge
exists starting at a vertex already added to one not yet
added, the next vertex added by the algorithm becomes
the root of a new tree in the spanning forest.

48/50
Depth-First Search in Directed Graphs
Example
What is the output of depth-first search given the graph G as
input?
> S

~ -
-

dontour onea
< E

49/50
Depth-First Search in Directed Graphs
Solution:

I We begin the depth-first search at


vertex a and add vertices b, c, and
g and the corresponding edges
where we are blocked.
I We backtrack to c but we are still
blocked, and then backtrack to b,
where we add vertices f and e and
the corresponding edges.
I Backtracking takes us all the way back to a. We then start a
new tree at d and add vertices h, l, k, and j and the
corresponding edges. We backtrack to k, then l, then h, and
back to d.
I Finally, we start a new tree at i, completing the depth-first
search.
50/50

You might also like