Section 10.1: Introduction To Trees
Section 10.1: Introduction To Trees
Section 10.1
Introduction to Trees
a b a b a b a b
d c
c d c c d d
e f e f e f e f
YES YES NO NO
Forest
• What if there are no simple circuits but the
graph is not connected?
• Each of the connected components is a tree
• The collection is called a forest.
Rooted Tree
• Specify a vertex as root, then direct each
edge away from the root. The resulting tree
is called a rooted tree.
Root
Example
Root b
a b c
a d e f c
d e f b
d f
a c
What if a different root is chosen?
Root
a b c c
b c
d e f f
d b
e f
d
b c a
e
a
a d e f
A different rooted tree results.
Tree Terminology
Root a
b c d
e f g h
i j k l m
Example
Siblings b c d
e f g h
i j k l m
Tree Terminology (Cont.)
a
Ancestors of k
b c d
e f g h
i j k l m
Example
Descendants of d a
b c d
e f g h
i j k l m
Tree Terminology (Cont.)
Leaves a
b c d
e f g h
i j k l m
Example
a
Internal vertices
b c d
e f g h
i j k l m
Tree Terminology (Cont.)
a
Subtree at b Subtree at d
b c d
e f g h
i j k l m
Tree Terminology (Cont.)
b c
d e h i
Left child of d
f g j k l
m
Example
b c
d e h i
f g j k l
Right child of d m
Example
b c
d e h i
f g j k l
Left subtree of c m
Example
b c
d e h i
f g j k l
m Right subtree of c
Tree Terminology (Cont.)
a
Levels 0
b c d 1
e f g h
2
i j k l m
3
Height = 3
Properties of Trees
T1 T2 Tn
Preorder Traversal
Step 1: Visit r
Step 2: Visit T1 in preorder r
Step 3: Visit T2 in preorder
.
. T1 T2 Tn
.
Step n+1: Visit Tn in preorder
Example
Tree: M
A Y E
J R H
P Q T
Visiting sequence:
M A J Y R H P Q T E
Inorder Traversal
Tree: M
A Y E
J R H
P Q T
Visiting sequence:
J A M R Y P H Q T E
Postorder Traversal
Tree: M
A Y E
J R H
P Q T
Visiting sequence:
J A R P Q T H Y E M