0% found this document useful (0 votes)
102 views61 pages

Discrete Math Graphs and Tree PDF

This document defines and provides examples of different types of graphs in discrete mathematics. It begins by defining what graphs are and then discusses simple graphs, multigraphs, pseudographs, directed graphs, and special graph structures. Key terms are defined such as adjacency, degree, handshaking theorem, and complete graphs. Graphs can model various real-world relationships and connections between discrete elements.

Uploaded by

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

Discrete Math Graphs and Tree PDF

This document defines and provides examples of different types of graphs in discrete mathematics. It begins by defining what graphs are and then discusses simple graphs, multigraphs, pseudographs, directed graphs, and special graph structures. Key terms are defined such as adjacency, degree, handshaking theorem, and complete graphs. Graphs can model various real-world relationships and connections between discrete elements.

Uploaded by

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

Discrete

Mathematics
5. G r a p h s & Trees
What are Graphs? Not

• General m e a n i n g in e v e r y d a y
A plot or chart of numerical data using a
math:
coordinate system.
• Technical m e a n i n g in d i s c r e t e
m at hem atics:
A particular class of discrete structures (to be
defined) that is useful for representing relations and
has a convenient webby-looking graphical
representation.

Discrete Math ematics, Sp r in g


2009
Simple Graphs
Visual Representation
of a Simple Graph

• T h e g r a p h in w h i c h e a c h e d g e
c o n n e c t s t w o different vertices a n d
w h e r e n o t w o e d g e s c onn e c t t h e
s a m e pair o f vertices

• C o r r e s p o n d to s y m m e t r i c binary
relations R.

Discrete Math ematics, Sp r in g


2009
Simple Graphs
Visual Representation
of a Simple Graph
• Definition:
A simple graph
G=(V,E) c o n s i s t s of:
− a s e t V o f vertices or nodes (V c o r r e s p o n d s to
t h e u n i v e r s e o f t h e relation R), a n d
− a s e t E o f edges / arcs / links: u n o r d e r e d
pairs o f [distinct?] e l e m e n t s u,v  V,
s u c h that uRv.

Discrete Math ematics, Sp r in g


2009
Example of a Simple
• Graph
Let V b e t h e s e t o f s t a t e s in t h e far-
sout heastern U.S.:
V={FL, GA, AL, M S, LA, SC, TN, NC}
• Let E={{u,v}|u adjoins v}
{FL,LA},{GA,AL},
={{FL,GA},{FL,AL},{FL,MS}, NC
TN
{AL,MS},
{MS,LA},{GA,SC}, SC
MS AL
{GA,TN},
{SC,NC},{NC,TN}, GA
{MS,TN}, LA
{MS,AL}} FL

Discrete Math ematics, Sp r in g


2009
M ultigraph s
• Like s i m p l e g r a p h s , bu t t h e r e m a y b e more than
one
e d g e c onne ct i ng t w o g i v e n n o d e s .

• Definition:
A multigraph G=(V, E, f ) c o n s i s t s o f a s e t V o f
vertices, a s e t E 
f:E{{u,v}|u,vV o f e d g e s ( a s primitive objects),Parallel
uv}.
a n d a function edges
• Example:
n o d e s are cities a n d
e d g e s are s e g m e n t s o f major
highways.
Discrete Math ematics, Sp r in g
2009
Pseudographs
• Like a multigraph, bu t e d g e s
c onn e ct i ng a n o d e to itself are
a l l owe d.
• Definition:
A pseudograph G=(V, E, f ) w h e r e
f:E{{u,v}|u,vV}. E d g e eE is loop
a loop if f(e)={u,u}={u}.
• Example:
n o d e s are c a m p s i t e s
in a state p a r k a n d e d g e s
are hiking trails t h r o u g h t h e
woods.
Discrete Math ematics, Sp r in g
2009
Directed Graphs
• C o r r e s p o n d to arbitrary binary
relations R, w h i c h n e e d not b e
symmetric.
• Definition:
A directed graph (V, E) c o n s i s t s o f a s e t o f
vertices
V a n d a binary relation E o n V.

• Example:
V = p e o p l e , E={(x,y) | x l o v e s y}

Discrete Math ematics, Sp r in g


2009
Walk, loop, sling, and path
• Definition:
− A walk is a s e q u e n c e x 0 , x 1 , …, x n of the
v er tices o f a d i g r a p h s u c h that x i x i+1 , 0≤i≤n-1,
is an e d g e .
− T h e length of a walk is t h e n u m b e r o f e d g e s
in t h e walk.
− If a w a l k h o l d s x i ≠x j (i≠j) i,j=0, …, n, e x c e p t x 0 ,
x n (i.e x 0 =x n ), t h e w a l k is c a lle d a cycle.
− A loop is a c y c l e o f l e n g t h one.
− A sling is a c y c l e o f l e n g t h two.
− A w a l k is a path if n o e d g e is r e p e a t e d m o r e
than o n ce.

Discrete Math ematics, Sp r in g


2009
Directed Multigraphs
• Like d i r e c t e d g r a p h s , bu t t h e r e m a y b e m o r e
than o n e arc f r o m a n o d e to another.
• Definition:
A directed multigraph G=(V, E, f ) c o n s i s t s o f a s e t
V o f vertices, a s e t E o f e d g e s , a n d a function
f:EVV.

• Example:
− The W W W is a directed multigraph.
− V=web p a g e s , E=hyperlinks.

Discrete Math ematics, Sp r in g


2009
Types of Graphs: Summary
• K e e p in m i n d this t e r m i no l ogy is not fully
standardized...

E dge M ultiple S elf-


T erm type edges ok? loops ok?
S im ple graph U ndir. No No
M ultigraph U ndir. Y es No
P seudograph U ndir. Y es Y es
D irected graph D irected No Y es
D irected m ultigraph D irected Y es Y es

Discrete Math ematics, Sp r in g


2009
Graph Terminology
• Adjacent, connects, endpoints, degree, initial,
terminal, in-degree, out-degree, complete, cycles,
wheels, n-cubes, bipartite, subgraph, and union.

Discrete Math ematics, Sp r in g


2009
Adjacency

Le t G b e an u nd ire c te d g r a p h w ith e d g e s e t
E.
Le t
eE b e (or m a p to) t h e pair {u,v}. Then
w e sa y:
• u, v are adjacent / neighbors / connected.
• E d g e e is incident with vertices u a n d v.
• E d g e e connects u a n d v.
• Vertices u a n d v are endpoints o f e d g e e.

Discrete Math ematics, Sp r in g


2009
Degree of a Vertex
• Let G b e an u n d i r e c t e d g r a p h , vV a vertex.
• T h e degree o f v, d e g ( v ) , is its n u m b e r o f
incident e d g e s . ( E x c e p t that a n y s e l f -
l o o p s are c o u n t e d twice.)
• A vert ex wi t h d e g r e e 0 is isolated.
• A vert ex o f d e g r e e 1 is pendant.

Discrete Math ematics, Sp r in g


2009
Handshaking Theorem
• Theorem:
− Let G b e an u n d i r e c t e d ( s i mp l e , multi-, or
p s e u d o - ) g r a p h wi t h vertex s e t V a n d e d g e
s e teE.
Th
n
∑deg(v)  2 E
vV
• Corollary:
− A n y u n d i r e c t e d g r a p h h a s an e v e n
n u m b e r o f vertices o f o d d d e g r e e .

Discrete Math ematics, Sp r in g


2009
Directed Adjacency
• Let G b e a d i r e c t e d ( p o s s i b l y multi-)
g r a p h , a n d let e b e an e d g e o f G that is
(or m a p s to) (u,v). T h e n w e sa y:
− u is adjacent to v, v is adjacent from u
− e comes from u, e goes to v.
− e connects u to v, e goes from u to v
− t h e initial vertex o f e is u
− t h e terminal vertex o f e is v

Discrete Math ematics, Sp r in g


2009
Directed Degree
• Definition:
Let G b e a d i r e c t e d g r a p h , v a vertex o f G.
− T h e in-degree o f v, deg  (v), is t h e
n u m b e r o f e d g e s g o i n g to v.
− T h e out-degree o f v, deg  (v), is t h e
n u m b e r o f e d g e s c o m i n g f r o m v.
− T h e degree o f v, deg(v)deg  (v)+deg  (v),
is t h e s u m o f v’s i n - d e g r e e a n d o u t -
degree.

Discrete Math ematics, Sp r in g


2009
Directed Handshaking Theorem
• Theorem:
− Let G b e a d i r e c t e d ( p o s s i b l y multi-) g r a p h
wi t h vertex s e t V a n d e d g e s e t E.Then:
1
∑ deg 
(v)  ∑ deg (v) 
∑ deg(v)  E
vV
vV
2 vV

• N o t e that t h e d e g r e e o f a n o d e is
u n c h a n g e d b y w h e t h e r w e c o n s i d e r its
e d g e s to b e d i r e c t e d or undirected.

Discrete Math ematics, Sp r in g


2009
Special Graph Structures

Speci al c a s e s o f u n d i r e c t e d g r a p h
structures:
• C o m p l e t e G r a p h s Kn
• C y c l e s Cn
• W h e e l s Wn
• n- Cube s Q n
• Bipartite G r a p h s
• C o m p l e t e Bipartite G r a p h s Km,n

Discrete Math ematics, Sp r in g


2009
Complete Graphs

• Definition:
− For a n y nN, a complete graph o n n vertices,
Kn, is a s i m p l e g r a p h wi t h n n o d e s in
w h i c h e v e r y n o d e is adjacent to e v e r y
o t h e r n o d e : u,vV: uv{u,v}E.

K1 K2 K3 K4 K5 K6
n1
n(n 1)
Note that K n
has
i1
i
2
edges.

Discrete Math ematics, Sp r in g
2009
Cycles
• Definition:
− For a n y n3, a cycle o n n vertices, Cn, is a
s i m p l e g r a p h w h e r e V={v1,v2,… ,v n } a n d
E={{v1 ,v2 },{v2 ,v3 },…,{vn1 ,vn },{vn ,v1 }}.

C3 C4 C8
C5 C6
C7

How many edges


Discrete areSp rthere
Math ematics, in g in Cn?
2009
W heels

• Definition:
− For a n y n3, a wheel W n , is a s i m p l e g r a p h
o b t ai n ed b y taking t h e c y c l e Cn a n d a d d i n g o n e
extra vertex v h u b a n d n extra e d g e s {{v h u b ,v 1 },
{v h u b ,v 2 },…,{v h u b ,v n }}.

W3 W4 W5 W6 W7 W8

How many edges are there in Wn?


Discrete Math ematics, Sp r in g
2009
n-Cubes (hypercubes)
• Definition:
− A g r a p h that h a s vertices r e p r e s e n t i n g t h e
2n bit strings o f l e n g t h n
− For a n y nN, t h e hypercube Q n is a s i m p l e
g r a p h co n si st i n g o f t w o c o p i e s o f Q n-1
c o n n e c t e d t o g e t h e r at c o r r e s p o n d i n g
nodes. Q0 h a s
1 node.

Q0 Q1 Q2 Q3 Q4
Number of vertices: 2n. Number of edges:Exercise to try!
Discrete Math ematics, Sp r in g
2009
n-Cubes (hypercubes)
• Definition:
For a n y nN, t h e h y p e r c u b e Q n can b e
d e f i n e d recu rsi v el y a s follows:
• Q 0 =({v 0 },) ( o n e n o d e a n d n o e d g e s )
• For a n y nN, if Qn=(V,E), w h e r e V={v1,…,va}
E={e
a n d 1 ,…,eb }, th e n Q {v1 ´,…,v
E{en+1
=(V 1 ´,…,e {{v1 ,v1 ´},{v ´}, 2 ´},
2 ,v
a

´}a ,vab´}}) w h…,


{v e r e1 v ´,…,v
a ´ are n e w vertices,
w j k,v } t h e in e ´={v
a nhde r e if ie ={v j k
´,v ´}.

Discrete Math ematics, Sp r in g


2009
Bipartite Graphs
• Definition:
− A s i m p l e g r a p h G is called bipartite if its
vertex s e t V can b e partitioned into t w o
disjoint s e t s V1 a n d V2 s u c h that e v e r y
e d g e in t h e g r a p h c o n n e c t s a vertex in V1
a n d a vertex in V2 ( s o that n o e d g e in G
c o n n e c t s either t w o vertices in V1 or t w o
vertices in V 2 )

V1 V2
a bipartite

Discrete Math ematics, Sp r in g


2009
Complete Bipartite Graphs

• Definition:
Let m, n b e p o si t i v e integers. T h e complete
bipartite graph Km,n is t h e g r a p h w h o s e
vertices can b e partitioned V = V1 ∪ V2
s u c h that
1. |V1| = m
2. |V2| = n
3. For all x ∈ V1 a n d for all y ∈ V2 , t h e r e is
an e d g e b e t w e e n x a n d y
4. N o e d g e h a s b o t h its e n d p o i n t s in V1 or
b o t h its e n d p o i n t s in V2

Discrete Math ematics, Sp r in g


2009
Complete Bipartite Graphs
(cont.)

V1 V2

K2,3
V1

V2

K3,4

Discrete Math ematics, Sp r in g


2009
Su b graph s
• Definition:
− A s u b g r a p h o f a g r a p h G=(V,E) is
a graph
H=(W,F) w h e r e WV a n d FE.

G H

Discrete Math ematics, Sp r in g


2009
Graph Unions
• Definition:
− T h e union G1G2 o f t w o s i m p l e g r a p h s
G1=(V1, E 1 ) a n d G2 =(V2 ,E2 ) is t h e s i m p l e
g r a p h (V 1 V 2 , E1E2).

Discrete Math ematics, Sp r in g


2009
Graph Representations &
Isomorphism
• Gr a ph representations:
− A d j a c e n c y lists.
− A d j a c e n c y matrices.
− In ci d en ce matrices.
• Gr a ph i s o m o r p h i s m :
− Tw o g r a p h s are i s o m o r p h i c iff t h e y are
identical e x c e p t for their n o d e n a m e s .

Discrete Math ematics, Sp r in g


2009
Adjacency Lists
• A table wi t h 1 r o w p e r vertex, listing its
adjacent vertices.
• A w a y to r e p r e s e n t a g r a p h w/ n o multiple
edges

a b a. b, c
Vertex
b. a, c, e, f
c d Adjacent
c.a, b, Vertices
f d
e
f e. b
f. c, b

Discrete Math ematics, Sp r in g


2009
Directed Adjacency Lists
• 1 r o w p e r n o d e , listing t h e terminal
n o d e s o f e a c h e d g e incident f r o m that
node.

Discrete Math ematics, Sp r in g


2009
Adjacency Matrices
• Matrix A=[aij], w h e r e aij is 1 if {vi , v j } is an
e d g e o f G, 0 o t h e r w i s e .

• Simple graph
represent ation
Discrete Math ematics, Sp r in g
2009
Adjacency Matrices
• Matrix M=[m ij ], w h e r e
− mij = 1w h e n e d g e ej is incident wi t h vi,
− mij = 0o t h e r w i s e .
• A w a y of represent graphs
− can b e u s e d to r e p r e s e n t multiple
e d g e s and loops

Discrete Math ematics, Sp r in g


2009
Graph Isomorphism

• Definition:
S i m p l e g r a p h s G1=(V1, E 1 ) a n d G2=(V2, E 2 ) are
isomorphic iff  a bijection f:V1V2 s u c h that 
a,bV1, a a n d b are adjacent in G1 iff f(a) a n d f(b)
are adjacent in G2.

• f is t h e “renaming” function that m a k e s t h e


t w o g r a p h s identical.
• Definition can e a si l y b e e x t e n d e d to ot h e r
t ype s of graphs.
Discrete Math ematics, Sp r in g
2009
Graph Invariants under Isomorphism

• Gr a ph Invariant
−a property preserved by isomorphism of
graphs
− Necessary b u t not sufficient conditions for
G1=(V1, E 1 ) to b e i s o m o r p h i c to G2=(V2, E 2 ):
• |V1|=|V2|, |E1|=|E2|.
• T h e n u m b e r o f v er tices w i t h d e g r e e n is t h e
s a m e in b o t h g r a p h s .
• For e v e r y p r o p e r s u b g r a p h g o f o n e g r a p h ,
t h e r e is a p r o p e r s u b g r a p h o f t h e o th e r
g r a p h that is i s o m o r p h i c to g.

Discrete Math ematics, Sp r in g


2009
Isomorphism Example
• If i s o m o r ph i c , label t h e 2nd g r a p h to
s h o w t h e i s o m o r p h i s m , e l s e identify
difference.
b d
a b a
d c
e
e c f
f

Discrete Math ematics, Sp r in g


2009
Are These Isomorphic?
• If i s o m or ph i c , label t h e 2nd g r a p h to
s h o w t h e i s o m o r p h i s m , e l s e identify
difference.

a * Same # of vertices
b
* Same # of edges

d * Different # of verts of
degree 2! (1 vs 3)
c e

Discrete Math ematics, Sp r in g


2009
Connectedness

• Definition:
A path of length n f r o m u to v in G is
a s e q u e n c e o f n e d g e s e1, e2, … , en s u c h that e1 is
a s s o c i a t e d wi t h {x0 , x1 }, e2 is a s s o c i a t e d wi t h {x1 ,
x2 }, a n d s o on, wi t h en a s s o c i a t e d wi t h {xn-1 ,xn },
w h e r e x0=u a n d xn=v;

• T h e p a t h is circuit if it b e g i n s a n d e n d s at t h e
s a m e vertex, that is u=v, a n d h a s l e n g t h
great er than zero.
• A p a t h or circuit is simple if it d o e s not
contain t h e s a m eDiscrete
e dMath
g eematics,
m oSprr ineg than once.
2009
Connectedness

• Definition:
− An u n d i r e c t e d g r a p h is connected iff t h e r e is a
p a t h b e t w e e n e v e r y pair o f distinct vertices
in t h e g r a p h .

• Theorem:
− T h e r e is a simple p a t h b e t w e e n a n y pair o f
vertices in a c o n n e c t e d u n d i r e c t e d g r a p h .

Discrete Math ematics, Sp r in g


2009
Directed Connectedness
• Definition:
− A d i r e c t e d g r a p h is strongly connected iff
t h e r e is a d i r e c t e d p a t h f r o m a to b for a n y
t w o vertices a a n d b.
− It is weakly connected iff t h e u n d e r l y i n g
undirected g r a p h (i.e., wi t h e d g e directions
r e m o v e d ) is c o n n e c t e d .

• N o t e strongly i m p l i e s weakly bu t not vice-


versa.

Discrete Math ematics, Sp r in g


2009
Paths & Isomorphism
• N o t e that c o n n e c t e d n e s s , a n d t h e
e x i s t e n c e o f a circuit or s i m p l e circuit o f
l e n g t h k are g r a p h invariants wi t h
r e s p e c t to i s o m o r p h i s m .

Discrete Math ematics, Sp r in g


2009
Euler & Hamilton Paths
• Definition:
− An Euler circuit in a g r a p h G is a s i m p l e
circuit containing e v e r y e d g e o f G.
− An Euler path in G is a s i m p l e p a t h containing
e v e r y e d g e o f G.
• Examples: a b a b

d c c d e
a, e, c, d, e, b, a a, c, d, e, b, d, a, b
Euler circuit Euler path
Discrete Math ematics, Sp r in g
2009
Euler & Hamilton Paths (cont.)
• Definition:
− A Hamilton circuit is a s i m p l e circuit that
t r a v e r s e s e a c h v er tex in G ex actly o n ce.
− A Hamilton path is a s i m p l e p a t h that t r a v e r s e s
e a c h v er tex in G ex actly o n ce.
• Examples:
a b a b

e c

d c
d
a, b, c, d, e, a a, b, c, d
Hamilton circuit Hamilton path

Discrete Math ematics, Sp r in g


2009
Trees
• Definition:
− A tree is a c o n n e c t e d u n d i r e c t e d g r a p h w i t h n o
s i m p l e circuits.
• Since a tree cannot h a v e a circuit, a tree cannot
contain mu ltip le e d g e s or l o o p s .
• Therefore, a n y tree m u s t b e a simple graph.

• Theorem:
− A n u n d i r e c t e d g r a p h is a tree if a n d o n l y if t h e r e is
a
unique simple path b e t w e e n a n y t w o o f its vertices.
• In general, w e u s e t r e e s to r e p r e s e n t hierarchical
structures. Discrete Math ematics, Sp r in g
2009
Trees
Example: Ar e t h e f ol l owi ng g r a p h s
trees?

No. Yes
.

Yes No.
.
Discrete Math ematics, Sp r in g
2009
Root & Rooted
tree
• We often d e s i g n a t e a particular vertex o f
a tree a s t h e root. Since t h e r e is a u n i q u e
p a t h f r o m t h e root to e a c h vertex o f t h e
g r a p h , w e direct e a c h e d g e a w a y f r o m
t h e root.

• Definition:
A rooted tree is a tree in w h i c h o n e vertex h a s
b e e n d e s i g n a t e d a s t h e root a n d e v e r y e d g e
is d i r e c t e d a w a y f r o m t h e root

Discrete Math ematics, Sp r in g


2009
Tree Terminology
• Definition:
− If v is a vertex in a r o o t e d tree o t h e r than t h e
root, t h e parent o f v is t h e u n i q u e vertex u
s u c h that t h e r e is a d i r e c t e d e d g e f r o m u to
v.
− W h e n u is t h e parent o f v, v is called t h e
child o f u.
− Vertices wi t h t h e s a m e parent are called
siblings.
− T h e ancestors o f a vertex o t h e r than t h e root
are t h e vertices in t h e p a t h f r o m t h e root to
this vertex, eDiscrete
x c l uMathdematics,
i n g tSphr ineg vertex itself a n d
2009
including t h e root.
Tree Terminology (cont.)
• Definition:
− T h e descendants o f a vertex v are t h o s e vertices
that h a v e v a s an ancestor.
− A vertex o f a tree is called a leaf if it h a s n o
children.
− Vertices that h a v e children are
called internal vertices.
− If a is a vertex in a tree, t h e n t h e subtree
wi t h a a s its root is t h e s u b g r a p h o f t h e tree
co n si st i n g o f a a n d its d e s c e n d a n t s a n d all
e d g e s incident to t h e s e d e s c e n d a n t s .
Discrete Math ematics, Sp r in g
2009
Tree Terminology
• Definition:
− T h e level o f a vertex v in a r o o t e d tree is
the length of the unique path from the
root to this vertex.
− T h e level o f t h e root is d e f i n e d to b e
zero.
− T h e height o f a r o o t e d tree is t h e
m a x i m u m o f t h e l e v e l s o f vertices.

Discrete Math ematics, Sp r in g


2009
Trees
Example 1: Family tree
James

Christine Bob

Frank Joyce Petra

Discrete Math ematics, Sp r in g


2009
Trees (cont.)
Example 2: File
system

usr bin temp

bin spool ls

Discrete Math ematics, Sp r in g


2009
Trees (cont.)
Example 3: Arithmetic
expressions

+ -

y z x y
• This tree r e p r e s e n t s t h e e x p r e s s i o n ( y +
z)(x - y).
Discrete Math ematics, Sp r in g
2009
m-ary tree
• Definition:
− A r o o t e d tree is called an m-ary tree if e v e r y
internal vertex h a s n o m o r e than m children.
− T h e tree is called a full m-ary tree if e v e r y
internal vertex h a s exactly m children.
− An m-ary tree wi t h m = 2 is called a binary
tree.

• Theorem:
− A tree wi t h n vertices h a s (n – 1) e d g e s .
− A full m-ary tree wi t h i internal vertices
contains n = Discrete Math ematics, Sp r in g
2009
m·i + 1 vertices.
Ordered Rooted Tree
• Definition:
− An ordered rooted tree is a r o o t e d tree
w h e r e t h e children o f e a c h internal vertex
are o r d e r e d .

• For e x a m p l e , in an o r d e r e d binary tree (just


called binary tree), if an internal vertex h a s
t w o children,
− t h e first child is called t h e left child
a n d t h e s e c o n d is called t h e right
child.
− t h e tree r o o t e d at t h e left child is called
t h e left su b t r ee, a n d at t h e right child, t h e
right s u b t r e e
Discrete Math ematics, Sp r in g
• O r d e r e d r o o t e d2009
t r e e s can b e d e f i n e d
Ordered Rooted Tree (binary tree)
Left child of the a
Vertex a Right child of
the Vertex a
Left subtree of b c
the Vertex b

d e f g

h i j Right subtree of
the Vertex b
k l
Discrete Math ematics, Sp r in g
2009
Tree Traversal
• P r o c e d u r e s for syst e m a t i c al l y visiting
e v e r y vertex o f an o r d e r e d r o o t e d
tree are called traversal algorithms.

Discrete Math ematics, Sp r in g


2009
Preorder traversal
step 1
• Definition: r Visit r
Let T b e an o r d e r e d
r o o t e d tree w i t h root r.
If T c o n s i s t s o n l y o f r, t h e n
r is t h e preorder traversal o f
T. Oth erwise, s u p p o s e
that T1 , T2 , … , T n are t h e
s u b t r e e s at r f r o m left to
right in T. T h e preorder T1 T2 ... Tn
traversal b e g i n s b y visiting
r. It c o n tin u e s b y Step 2 step 3 Step n+1
traversing T 1 in p r e o r d e r, Visit T1 in Visit T2 in Visit Tn in
t h e n T 2 in p r eo r d er, a n d s o preorder preorder preorder
on, until T n is t r a v e r s e d in
p reo rder. Discrete Math ematics, Sp r in g
2009
Inorder traversal
step 2
• Definition: r Visit r
Let T b e an o r d e r e d
r o o t e d tree w i t h root r.
If T c o n s i s t s o n l y o f r, t h e n
r is t h e inorder traversal o f
T. Oth erwise, s u p p o s e
that T1 , T2 , … , T n are t h e
s u b t r e e s at r f r o m left to
right. T h e inorder traversal T1 T2 ... Tn
b e g i n s b y traversing
visiting T 1 in inorder, t h e n Step 1 step 3 Step n+1
visiting r. It c o n t i n u e s b y Visit T1 in Visit T2 in Visit Tn in
traversing T 2 in inorder, inorder inorder inorder
t h e n T 3 in inorder, … , a n d
finally T n in inorder
Discrete Math ematics, Sp r in g
2009
Postorder traversal
step n+1
• Definition: r Visit r
Let T b e an o r d e r e d
r o o t e d tree w i t h root r.
If T c o n s i s t s o n l y o f r, t h e n
r is t h e postorder traversal o f
T. Oth erwise, s u p p o s e that
T1 , T2 ,
… , T n are t h e s u b t r e e s at r
f r o m left to right. T h e T1 T2 ... Tn
postorder traversal b e g i n s b y
traversing T 1 in p o s t o r d e r, Step 1 step 2 Step n
t h e n T 2 in p o s t o r d e r, … , Visit T1 in Visit T2 in Visit Tn in
postorder postorder postorder
t h e n T n in p o s t o r d e r, a n d
e n d s b y visiting r.
Discrete Math ematics, Sp r in g
2009
Traversal Example
a

b c d

g
• Preorder : a, b, e, j, k, n,
e
f o, p, f, c, d, g, l, m, h, I
h i
• Inorder : j, e, n, k, o, p,
k l
j m b, f, a, c, l, g, m, d, h, i
• Postorder : j, n, o, p, k, e,
n o p f, b, c, l, m, g, h, i, d, a

Discrete Math ematics, Sp r in g


2009

You might also like