Trees
Trees
(Discrete Structure)
Chapter Outline
9.1 Introduction to trees
9.2 Rooted Trees
9.3 M-ary and Balanced Trees
9.4 Tree Traversal and Notation
9.5 Spanning Trees
9.6 Weighted Trees
Introduction of Trees
G1 G G3
2
5 4
6 5
n n -1
Figur
e 9.4
Figure 9.5
Figur
e 9.6
Exercise
:
1.Draw a tree with 8 vertices such that 2 vertices
are of degree 1.
2.Draw a forest with 3 components and 9 vertices.
3.Draw the chemical structure of Benzene (C6H6).
4.Construct an Isomorphic tree using 6 vertices
5.Show the isomers of C4H4 of (Butane
and
isobidene).
Rooted Trees
A tree is Rooted tree if one vertex has been designated
the root, the term root usually refers to a top-most vertex
extends downward meaning each edge is simply directed
away from the root. On contrary a tree w/o root is called
ee
FFrigeuereTr9.7
.
Figure 9.8 IT Division Organization
Chart
A. Vertex Level and Height of a Tree
The Level (or depth) of a vertex is its distance
from the root.
The Height of the tree is the
number of vertices or edges traversed from
the most distant vertex. the root to get
Figure 9.11
B. Parent and Child of a Tree
In rooted tree, the Parent of a vertex is the vertex
connected to it on the path to the root (or a vertex
which closer to the root vertex and connected to
another vertex by a single bridge).
C. Sibling
A Sibling is a vertex which shares the same parent.
D. Descendants and ancestor
A Descendant is a vertex which is further away
from the root than some other vertex. The term
descendant is always in reference to another vertex.
An Ancestor is any vertex between a given vertex
and rooted including the root.
E. Terminal and Internal Vertices
A Terminal Vertex (or leaf) is a vertex with no
children (meaning, a vertex of degree one). On the
other hand an Internal Vertex (or branch
vertex) in a rooted tree is a vertex that has at least
one child.
F. Sub-tree
A Sub-tree is (usually) a smaller portion of a tree
starting at some specified vertex.
Figure 9.12
G. Ordered Rooted Tree
An Ordered rooted Tree is a rooted tree if all
groups of sibling vertices are assigned a relative
order (meaning the set of children of each
internal vertex is
ordered)
.
Figure 9.13
Exercise 9.2
Figure 9.14
Answer the ff. using figure 9.14
1. Height of the tree
2. Level of vertex x , p
3. The children on vertex i
4. The parent of vertex f
5. The siblings of vertex m
6. The ancestor of vertex j
7. The descendants of the h
8.The internal and terminal of
vertices
M-ary and Balanced Trees
A. M-ary Tree
If each vertex of a rooted tree T has most
m
children,
ee then we can say that T is an m-ary
Fitg.ru
re
9.15
Figure 9.16
Figure 9.17
Theorem 9.7: Given a full m-ary tree T or height
h. Then ha
s
Left-child a - d f - - -
Right-child b c e - - - -
Theorem 9.8 The sum of the heights of the sub-trees in
complete Binary with v vertices and height h is v – h – L
Tree traversal and Notation
One of the most common operations performed on
trees are tree traversals. A Traversal is a process
visiting every vertex of an ordered rooted tree in a
systematic manner. Traversals are either breadth-first or
depth first.
A. Traversal / Search Trees
A breadth-first traversal is a strategy for traversing in
a tree, it begins at a root and the visiting each vertex,
starting from the lowest level and moving down by level,
visiting vertices from left to right.
While depth-first traversal is an algorithm for
traversing or searching a tree structure; it
starts
tree, at the root and visits as fast as possible along
each branch before backtracking.
Example 1: Given a tree in Figure 9.22,
the breadth-first traversal.
Determine
Solution: Figure 9.22
Breath-first traversal: r , a, b, c, d, e, f
The example clearly shows that adth-first
bre must traversal
Complete each level before proceed
the next level starting with theing to
root.
B. In-order , Pre-order , and Post-order
In-order Traversal of Tree T is done by the order of
L-P-R
meaning left child-parent-right child.
Prefix Notation : + a b
Postfix Notation : a b
Example 4: Construct the binary tree of the given
expression
(a ^ b)*((c - d (d / e))+f and determine the
equivalent infix notation and postfix notation
equivalent.
Solution:
a. In expression (a ^ b)*((c - d (d / e))+f the main
operator is “*” (multiplication) which will serve as
the root of the binary tree. The left sub-tree will
contain (a^ b) expression, while the right sub-tree
will contain ((c-(d/e))+f) expression.
In the expression (a + b), a and b are siblings,
while “+” (addition) operator is their parent.
On the right sub-tree ((c-(d/e))+f) its main operator
is “+” which in turn can be divided into right and
left sub-trees for level 2. The left sub-tree is f
and the right sub-tree is the expression (c-(d/e)).
Applying the same approach for the right sub-tree (c-
(d/e)) for level 3, which contain a main operator of
“-” (subtraction).
Lastly, the expression (d/e), will be represented
where d is the left leaf, their parents is “/”
(division). Operator.
Figure 9.27 show the step by step procedure in
plotting the (a^b)*((c - d (d / e))+f in binary tree.
Figure 9.27
b. The infix of the expression (a^b)*((c-(d/e)+f)
is a^b*c-d/e+f, we only need to remove the
parenthesis.
labeled
trees with v
vertices.
Theorem 9.14: Given a spanning tree T
of G and an edge
e of G
which does not belong to T the
Example 1 : In figure 9.29 given a graph G we
can obtain a spanning forest like F1 and F2 as
shown below. We can obtain a number of
different forests in a given a graph as noted on
theorem 9.12.
Figure 9.29
Example 2:
Find the number of trees of the complete
spanning graph K3
Solution:
Using Cayley’s theorem where
Thus, there are 3 sub-trees for complete graph K3.
Graphically figure 9.30 shows 3 different sub-trees T1,
FiTg2u,raen9d3
. T03 of K3
b. Minimum Spanning Tree
The three knows personalities in the field of graph
theory, specifically on the spanning tree: Robert Clay
Prim , Joseph Bernard Kruskal and Edsger W.
Dejkstra.
Robert Clay Prim Developed the Prim’s
algorithm 1957 which is a greedy algorithm that
determines
a minimum spanning tree for
a connected
weighted
undirected graph. The
algorithm finds a
subset of
Joseph Bernard Kruskal an mathematician
that introduced the Kruskal’s Algorithm for
computing minimal spanning trees (MST) of a
weighted tree. Kruskal’s Algorithm first orders
the edges by weight and then proceeds
through the ordered list adding an edge to the
partial MST provided that adding the new edge
does not create a cycle.
Edsger W. Dijkstra a computer scientist who
conceived
The Dijkstra’s Algorithm.The algorithm is a graph
Search Algorithm that determines the single
source
shortest Path problem for graph producing a
shortest path Tree.
Example: Find the spanning tree T such that the sum of
the weights of the edges is the smallest possible
using Prim’s and Kruskal’s Algorithms.
Figure 9.30
Solution:
a. We will first evaluate the minimum
Spanning tree solution of the given tree
of figure 9.32 using Prim’s Algorithm
The Prim’s Algorithm computes the ff. Sequence of
partitions:
P0 = {(a),(b),(c),(d),(e),(f),(g)} Figure 9.33
P1 = {(a,c),(b),(d),(e),(f),(g )}
P2 = {(a,b,c),(d),(e),(f),(g) }
P3 = {(a,b,c,e),(d),(f),(g)}
P4 = {(a,b,c,d,e),(f),(g)}
P5 = {(a,b,c,d,e,g),(f)}
P6 = {(a,b,c,d,e,f,g)}
All the vertices already connected and thus we already
established the minimum spanning Tree. Figure 9.33
shows the spanning tree with a total weight of
3+6+7+3+2+8=29
b. This time we will determine the minimum
spanning tree solution using Kruskal’s
Algorithm.
The Kruskal’s Algorithm compute the ff. Sequence of
partitions:
P0 = {(a),(b),(c),(d),(e),(f),(g)} Figure 9.34
P1 = {(a),(b),(c),(d,g),(e),(f)}
P2 = {(a,c),(b),(d,g),(e),(f)}
P3 = {(a,c),(b),(d,e,g),(f)}
P4 = {(a,b,c,d,e),(f),(g)}
P5 = {(a,b,c,d,e,g),(f)}
P6 = {(a,b,c,d,e,f,g)}
After we already know established a tree we then
solve the total weight of the , minimum spanning
tree.
3+6+7+3+2+8=29
Theorem 9.15 : Let G be any weighted connected
graph. Each of Kruskal’s Algorithm and Prim’s
Algorithm yield a minimum spanning tree for G.
Theorem 9.16 : Let G be any weighted connected
graph and v a vertex of G Dijkstra’s Algorithm yields
a shortest path tree for G from v.
Figure 9.35
Enrichment Exercise:
1. Draw a forest F with c(G) = 3 and 8 vertices.
2. Construct a spanning tree of a cycle graph
C3 .
3. Find the number of spanning trees of the
complete graph K4 .
4. Determine the minimum spanning tree of
Figure 9.35 using Prim’s and Kruskal’s
Algorithms.
Weighted Trees
A. Weighted tree
We refer T a weighted tree if a nonnegative real
number is assigned to each terminal vertex.
This number is called weight of the terminal
vertices.
Example 1:
Consider the weighted tree on figure 9.36 and
figure 9.37; find an optimal binary tree
with weights 2,4,5,7,8,10.
Figure 9.36 Figure 9.37
Solution:
First we need to collect the given and apply the
summation of the weight of the rooted
tree.
b. Huffman Encoding trees
Computer store character data in binary from as
sequence of 1s and Os (bits), the usual approach to
used Fixed-
length codes wherein each symbol in the
message with the same number of bits.
The ASCII (American Standard Code for
Information Interchange) standard code used to
represent codes
each character (or symbol) as sequence of n=8,so that
each character has 8 bits to restore .This enables each
character to store the same amount of storage space.
On the other hand ,a Variable-length code represented
characters( or symbols) in different number of bits.
Lemma 9.2: T is an optimal tree for the n weights w1
< w2 < ... <wn, then there exist an optimal tree T in
which the terminal vertices weights w1 and w2 are
siblings at the maximal level.
Theorem 9.17: Let T be an optimal tree for the weights
w1+ w2+ w3,...,wn where w1 < w2 < w3 < ,..., <
wn. At the terminal vertex with weight w1 + w2 to
the children of this former terminal vertex. The new
binary tree T1 so constructed is then optimal for the
weights w1,w2,w3,...,wn.
Corollary 9.7: There is an optimal binary tree T where
the weight w1 and w2 are both of the same level.
Example 2: Construct an optimal prefix code for the
symbols a, b, c, d, e, f that occur with frequencies
5, 20, 32, 16, 13, 6.
Solution: First is to rearrange the frequencies in
order from lowest to highest as shown in Figure
9.38a. secondly ,we will combine weights 5 and 6
as reflected in figure 9.38b. thirdly, we merge the
weight 11 with 13 as noted in figure 9.38c.
Fourthly, we combine 16 and 20 as seen in figure
9.38d. Then , we connect 32 with 24 to obtained
56 as shown in figure 9.38e.lastly,we combine the
two sub-trees to produce a single tree as noted in
figure 9.36f.
Here is how the Huffman tree was generated:
initial vertices {(a 5),(b 20),(c 32),(d 16),
13),(f 6)} (e
Merge {({a f} 11),(b 20),(c 32),(d 16),(e
binary
vertex tree.
r a(r is bthe croot)d e f g h
L child a b - e - - - - -
1.
R child - c d f - g ‘- h -
2. Vertex r a b c d e f g
L Child a c - - - - - -
R Child b d e - - f g -
E. Write the left-child-right child representation of the
binary tree below.
Vertex r a b c d e f g h
1. L child
R child
2.
Vertex r a b c d e f g h
L child
R child
Exercises :
Section Exercise 9.4A&B
A. Determine the breadth-first traversal and the
three depth-first search traversal (in-order ,
pre-
order and post-order)
Breadth-first=
Inorder=
Preorder=
Postorder=
Exercises :
Section Exercise 9.4A&B
Breadth-first=
Inorder=
Preorder=
Postorder=
Breadth-first=
Inorder=
Preorder=
Exercises :
Section Exercise 9.4A&B
Breadth-first=
Inorder=
Preorder=
Postorder=
Section Exercise 9.4 C
A. Given the expression below, determine the infinix,prefix and
postfix notations and draw its binary tree representations.
a.Expression=(a+b)/(c*(d-(e^f)))
Infinix=
Prefix=
Postfix=
b.Expression =((A+)*c-(d-e)
Infinix=
Prefix=
d. Expression =((a^b)+(c+d))+((e*f) /(g+h))
Infinix=
Prefix=
Postfix=
c. Expression =((a-b)+c)*((d+e)*(f/g))
Infinix=
Prefix=
Postfix
=
Exercises :
Section Exercise 9.4C
C. Calculate the following expressions.
1. 36 2 5 3 16 4 / * - - / 18 + 4. / * 10 + 3 5 ^ + 3
12
Exercises :
Section Exercise 9.4C
2. 32 4 + 2 * 9 / 15 6 - + 5. * + / 15 5 7 – 8 2
Exercises :
Section Exercise 9.4C
3. 24 4 / 3 2 ^ + 6 4 * - 6. / + 8 / * 10 3 + 6 – 9 ^ 2 3
Exercises :
Section Exercise 9.5
A. Determine the minimum spanning tree of the following
weighted graphs using prim’s algorithm and provide the solution
on the sequence of partitions.
P0=
P1=
P2=
P3
=
P4=
Exercises :
Section Exercise 9.5a
P0=
P1=
P2=
P3=
P4=
Exercises :
Section Exercise 9.5a
P0=
P1=
P2=
P3=
P4=
P5=
Exercises :
Section Exercise 9.5b
B. Determine the minimum spanning tree of the following
weighted graphs using kruskal’s algorithm and provide the
solution on the sequence of partitions.
P 0=
P1=
P2=
P3=
P4=
P5=
P6=
Exercises :
Section Exercise 9.5b
P0=
P1=
P2=
P3=
P4=
P5=
Exercises :
Section Exercise 9.5b
P0=
P1=
P2=
P3=
P4=
P5=
Exercises :
Section Exercise 9.6
Construct the optimal prefix code for the symbols
with their corresponding frequencies and determine
the weight of the rooted trees.
Symbols a b c d e f g h
Frequency 2 4 6 8 13 15 19 27
Frequency 1 1 2 4 6 9 14 24 35