Professional Documents
Culture Documents
CHAPTER 4 TREES
4.1 Introduction to Trees
4.2 Application of Trees
4.3 Tree Traversal
4.4 Trees and Sorting (Data structure)
4.5 Spanning Trees
4.6 Minimum Spanning
Trees
4.1 INTRODUCTION TO TREES
CHAPTER 4 TREES
• Define and recognize a m-ary tree
A Tree
EXAMPLES
a b c a b c a b c
d e f d e f d e f
EXAMPLES
f g a c
a
b d
d b c e
b d
a e f g e
f g
c
A TREE with A TREE with
A TREE
root a root c
Properties of Rooted Trees
descendants of g
ancestor of
d
lea
parent of f
d
child of
c subtree
sibling of
d
m-ary Tree
i. n vertices has i =
n −1
internal vertices and l =
( m − 1) n + 1
leaves. m m
CHAPTER 4 TREES
• Introduce Game Trees
Binary Search Trees
EXAMPLE
A Game tree
that represents
the first two
level of the
tic-tac-toe
game.
4.3 TREE TRAVERSAL
CHAPTER 4 TREES
• Determine the infix, prefix, and postfix form
of an expression.
Tree Traversal
r STEP 1
Visit r TIPS
Preorder Traversal:
Visit root, visit
subtrees left to right
T1 T2 … Tn
e
f g h
i
j k
m
l
n
o p
EXAMPLE: Preorder Traversal
r STEP 2
Visit r TIPS
Inorder Traversal:
Visit leftmost
subtree, Visit root,
T1 T2 … Tn Visit other
subtrees left to
STEP 1 STEP 3 STEP n+1 right.
Visit T1 in inorder Visit T2 in inorder Visit Tn in inorder
EXAMPLE: Inorder Traversal
a
d
b
e
f
g h
i
j k
m
l
n
o p
EXAMPLE: Inorder Traversal
r STEP n+1
Visit r TIPS
Postorder Traversal:
Visit subtrees
left to right, Visit
T1 T2 … Tn root.
STEP 1 STEP 2 STEP n
Visit T1 in postorder Visit T2 in postorder Visit Tn in postorder
EXAMPLE: Postorder Traversal
a
d
b
e
f
g h
i
j k
m
l
n
o p
EXAMPLE: Postorder Traversal
e f
g h i
k j n o pk e f bc l mg hi d a
j m
l •• • ••• ••• •• • •• • •
ITERATION 4
n
o p
ITERATION 1
The post order traversal of T
Represent Expression by Rooted Tree
↑ /
+ _
2 3
x y x 4
Infix, Prefix & Postfix Notation
+-*235/↑234 +-*2352
2↑3=8 2*3=6
+-*235/84 +-652
8/4 =2 6 – 5 =1
+-*2352 +12
2*3=6 1+2=3
Evaluating Postfix Expression
723*-4↑93/+ 14↑93/+
2*3=6 1↑4=1
76-4↑93/+ 193/+
7-6=1 9/3 = 3
14↑93/+ 13+
1↑4=1 1+3=4
4.5 SPANNING TREES
CHAPTER 4 TREES
Spanning Trees
a b c d Not
Spanning
e f g tree
e f g a b c d
Spanning
A simple graph tree
e f g
4.6 MINIMUM SPANNING TREES
CHAPTER 4 TREES
Kruskal’s algorithm
Minimum Spanning Trees
7 10
FIGURE 1
a
The minimum 3 {b, e} 11 b d
spanning tree is
given by: 7 10
c 11
a e
12 d
b 4 {e, c} 12 c
12 d
7 10 b
11 e
a 7 10
11 e
Total weight = 40 a
Kruskal’s Algorithm
SUMMARY