Trees: Discrete Mathematics
Trees: Discrete Mathematics
DISCRETE MATHEMATICS
M. M. Loy-a
CONTENT
1
2
3
4
5
Introduction to Trees
Application of Trees
Tree Traversal
Spanning Trees
Minimum Spanning
Trees
1 INTRODUCTION TO TREES
A Tree
A tree is a connected undirected graph with no simple circuit.
NOT A TREE
A TREE
NOT A TREE
EXERCISE 1
1. Which of the following graphs are trees?
a
FIGURE 1
FIGURE 2
v2
c
v1
d
e
b
FIGURE 3
v3
v5
v4
FIGURE 4
A Rooted Tree
A rooted tree is a tree in which one vertex has been designated as
the root and every edge is directed away from the root.
Different choice of root produce different rooted tree
EXAMPLES
e
b
e
c
A TREE
f
A TREE with
root a
g
A TREE with
root c
EXAMPLES
a
b
f
h
EXAMPLES
a
b
f
h
A subtree rooted at g
EXERCISE
2. Answer these questions about the rooted tree illustrated.
a) Which vertex is the root?
b) Which vertices are internal?
c) Which vertices are leaves?
d) Which vertices are children of g?
a
b
d
c
k l
o
n
p
q
e) Find a subgraph.
f) Find the level of
each vertex.
g) What is the height
of this tree?
h) Is this tree a
balanced tree?
m-ary Tree
A rooted tree is called an m-ary tree if every vertex has no more than
m children.
The tree is called a full m-ary tree if every internal vertex has exactly
m children.
a
b
b
g h
i j k l
A 3-ary TREE
e
b
f
f
m
f
m-ary Tree
THEOREM 1: A full m-ary tree with i internal vertices contains
n = mi + 1 vertices
THEOREM 2: A full m-ary tree with
m 1 n 1
n 1
i. n vertices has i
internal vertices and l
m
m
leaves.
ii. i internal vertices has n mi 1 vertices and l m 1 i 1
leaves.
ml 1
l 1
n
APPLICATION OF TREES
EXAMPLE
physics
geology
metereology
psycology
zoology
Decision Trees
EXAMPLE
A rooted tree in
which each
internal vertex
corresponds to a
decision, with a
subtree at these
vertices for each
possible outcome
of decision.
a>b
a:c
b:c
a<c
a>c
The possible
solutions of the
problem
correspond to the
paths to the leaves
of this rooted tree.
b:c
b>c
a>b>c
c>a>b
b<c
a>c>b
b<c
b>c
a>c
b>a>c
a:c
c>b>a
a<c
b>c>a
Game Trees
A Game tree
that represents
the first two
level of the
tic-tac-toe
game.
TREE TRAVERSAL
Tree Traversal
Ordered trees are often used to restore data/info.
Tree traversal is a procedure for systematically visiting each
vertex of an ordered rooted tree to access data.
Preorder Traversal
Let T be an ordered rooted tree with root r.
T1
STEP 1
Visit r
T2
TIPS
STEP 2
STEP 3
Visit T1 in preorder Visit T2 in preorder
Tn
STEP n+1
Visit Tn in preorder
Preorder Traversal:
Visit root, visit
subtrees left to right
a b
c
e
f
k
j
n
a
o p
a b e
f
k
j
n
o p
h i
o p
ITERATION 2
k f c d g l mh i
n
h
o p
ITERATION 3
ab e j k n opf c dgl mh i
ITERATION 4
ITERATION 1
Inorder Traversal
Let T be an ordered rooted tree with root r.
T1
STEP 1
Visit T1 in inorder
STEP 2
Visit r
T2
STEP 3
Visit T2 in inorder
TIPS
Tn
STEP n+1
Visit Tn in inorder
Inorder Traversal:
Visit leftmost
subtree, Visit root,
Visit other
subtrees left to
right.
e b f
c
e
f
k
j
n
j
n
o p
b
e
f
k
o p
d
n
h
d h i
o p
j e
a c
ITERATION 2
k b f a c l g md h i
o p
ITERATION 3
j e n k opb f ac l g m d h i
ITERATION 4
ITERATION 1
Postorder Traversal
Let T be an ordered rooted tree with root r.
T1
STEP n+1
Visit r
T2
TIPS
Tn
STEP 1
STEP 2
STEP n
Visit T1 in postorder Visit T2 in postorder Visit Tn in postorder
Postorder Traversal:
Visit subtrees left to
right, Visit root.
f
k
j
n
o p
f
k
j
n
o p
h i d a
c
e
b c
o p
k e
ITERATION 2
f b c l mg h i da
o p
ITERATION 3
j n o pk e f bc l mg hi d a
ITERATION 4
ITERATION 1
EXERCISE
1. Determine the order in which a preorder, inorder, and
postorder traversal visits the vertices of the following rooted
tree.
a
a
b
c
k l
h
m
FIGURE 1
i
o
n
q
f
h
FIGURE 2
+
x
+-*235/234
23=8
+-*235/84
8/4 =2
+-*2352
2*3=6
+-652
6 5 =1
+-*2352
+12
2*3=6
1+2=3
723*-493/+
2*3=6
76-493/+
7-6=1
1493/+
14=1
193/+
9/3 = 3
1493/+
13+
14=1
1+3=4
EXERCISE
2. Represent the following expression using binary trees.
Then write these expression in infix, prefix and postfix
notations.
a. ((x+2)3)*(y (3+x)) 5
b. (AB) (A(B A))
3. What is the value of these expression in prefix expression?
a. + 3 2 2 3 / 6 4 2
b. * + 3 + 3 3 + 3 3 3
4. What is the value of these expression in postfix expression?
a. 3 2 * 2 5 3 8 4 / *
b. 9 3 / 5 + 7 2 *
SPANNING TREES
Spanning Trees
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.
A simple graph is connected if and only if it has a spanning
tree.
Applied in IP multitasking.
a
A simple graph
d
e
Not
Spanning
tree
d
Spanning
tree
EXERCISE
1. Find a spanning tree for the following graphs.
b
h
d
FIGURE 1
FIGURE 2
b
b
d
FIGURE 3
FIGURE 4
c
d
Prims Algorithm
1. Chose an edge with the least weight.
2. Include it in spanning tree, T.
3. Select an edge of least weight that is incident with a vertex
of an edge in T.
4. If it does not create a cycle (simple circuit) with the edges in
T, then include it in T; otherwise discard it.
5. Repeat STEPS 3 and 4 until T contains n-1 edges.
There may be more than one minimum spanning tree
for a given connected weighted simple graph.
If there are two edges with similar smallest weight,
chose either one.
13
10
16
12 15
10 11
e
12
CHOICE
EDGE
WEIGHT
{a, b}
a
2
{a, d}
10
The minimum
spanning tree is
given by:
c
12
FIGURE 1
SPANNING TREE
b
{b, e}
11
10
a
b
7
d
10
11
{e, c}
12
12
10
11
Total weight = 40
10
11
Kruskals Algorithm
1.
2.
3.
4.
5.
13
10
16
EDGE
WEIGHT
{a, b}
a
2
{b, d}
10
The minimum
spanning tree is
given by:
c
10
10
FIGURE 1
SPANNING TREE
b
7
12 15
10 11
e
12
CHOICE
10
11
12
Total weight = 40
11
7
d
11
{b, e}
a
b
{e, c}
12
b
10
12
11
EXERCISE
1. Construct a minimum spanning tree for each of the following
connected weighted graphs using Prims and Kruskals
algorithm.
a
c
3
b
1
5
d
FIGURE 1
11
1
e
2
1
8
3
1
7
8
7
FIGURE 2
g 1
c