Chap9 Complete
Chap9 Complete
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.
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
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
10/50
Ordered Rooted Trees
G
inter
11/50
Properties of Trees
nevertices
vertices
is internal
&E leaves mem-ary
I A tree with n vertices has n 1 edges. ↑
tree
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?
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
&
2
Il
10 11
1215 16
3 3
12
9 Id
15 a 10
4 513
14 14
13
↑
5
678
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
go
rgrs v
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
28/50
Infix, Prefix, and Postfix Notation
for in case of infix form
29/50
Infix, Prefix, and Postfix Notation
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 :
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
notation.
S I
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
34/50
Infix, Prefix, and Postfix Notation
71v
35/50
Infix, Prefix, and Postfix Notation
Solution:
①
g
Example ⑥
7
I postfix: pq ^ ¬p¬q¬_ $,
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 }
38/50
Spanning Trees
For example, each of the trees shown below is a spanning tree
of G .
39/50
Depth-First Search
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 .
'
alphabetical
- "Follow 10
then reverse to
the incident
check
vertices in the
orde
path
.
Il z
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
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
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: