Week 12 - Trees
Week 12 - Trees
Week 12 - Trees
9 TREES
Trees are particularly useful in computer science, where they are employed in a wide range of
algorithms. For instance, trees are used to construct efficient algorithms for locating items in a list.
Social Networking used trees to represent or identify “common friends” between two or more users.
Company organizational structure, Table of Contents of a book, descendants and ancestors of a person,
even the computer file system is implemented using file structure. Even our everyday decision make use
of tree structure. The information store using trees naturally forms a hierarchy. This module will teach
you the procedures for building trees containing every vertex of a graph and how to traverse trees
which allows systematic search of solutions.
Objectives:
1. Describe trees.
2. Construct trees based on the given vertices and edges.
3. Differentiate the different kinds of trees.
4. Explain the different kinds of trees.
5. Analyze and apply tree to solve a problem.
DISCRETE STRUCTURES 1 85
Trees
Introduction
Tree
Let G = {V. E} be a loop-free undirected graph. The graph G is called a Tree if G is connected and
contains no cycles.
Example:
DISCRETE STRUCTURES 1 86
Trees
If a, b are distinct vertices in a tree T = (V. E), then there is a unique path that connects these
vertices.
Since T is connected, there is at least one path in T that connects a and b. If there were more,
then from two such paths, some of the edges would form a cycle. But T has no cycles.
Exercise
DISCRETE STRUCTURES 1 87
Trees
Spanning Tree
if G = (V E) is an undirected graph, then G is connected if, and only if, G has a spanning tree.
A spanning tree for a graph G is a subgraph of G that contains every vertex of G and is a tree.
Every connected graph has a spanning tree.
Any two spanning trees for a graph have the same number of edges.
Example:
Solution:
E-V+1
9-6+1=4 edges
Since, |E|-|V|+1
|V| = |E|+1
DISCRETE STRUCTURES 1 88
Trees
Exercise
The following statements are equivalent to a loop-free undirected graph G (V, E).
1. G is a tree.
2. G is connected, but the removal of any edge from G disconnects G into two subgraphs
that are trees.
3. G contains no cycles, and |V| = |E| + 1.
4. G is connected, and |V| = |E| + 1.
Rooted Trees
If G is a directed graph, then G is called a directed tree if the undirected graph associated with G
is a tree.
When G is a directed tree, G is called a rooted tree if there is a unique vertex r, called the root,
in G with the in degree of r = id(r) = 0, and for all other vertices v, the in-degree of v = id(v) = 1.
Example:
DISCRETE STRUCTURES 1 89
Trees
Example:
If v is a vertex in T other than the root, the parent of v is the unique vertex u such that there
is a directed edge from u to v
The ancestors of a vertex other than the root are the vertices in the path from the root to
this vertex, excluding the vertex itself and including the root
The root is an internal vertex unless it is the only vertex in the graph, in which case it is a leaf.
If a is a vertex in a tree, the subtree with a as its root is the subgraph of the tree
consisting of a and its descendants and all edges incident to these descendants.
A rooted tree is called an m-ary tree if every internal vertex has no more than m children.
The tree is called a full m-ary tree if every internal vertex has exactly m children.
An m-ary tree with m = 2 is called a binary tree.
Example:
Full binary tree because each of its internal vertices has two children.
DISCRETE STRUCTURES 1 90
Trees
An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered.
Ordered rooted trees are drawn so that the children of each internal vertex are shown in order
from left to right.
Binary Tree
In an ordered binary tree (usually called just a binary tree), if an internal vertex has two children,
the first child is called the left child and the second child is called the right child.
The tree rooted at the left child of a vertex is called the left subtree of this vertex, and the tree
rooted at the right child of a vertex is called the right subtree of the vertex.
Example:
DISCRETE STRUCTURES 1 91
Trees
Example:
Solution:
The level of any other node in the three is one more than the level of its parent.
DISCRETE STRUCTURES 1 92
Trees
Example:
Inorder Traversal
To visit a tree, first, visit its left subtree, then visit the
root, and then visit the right subtree.
Solution:
FDGBEAJHCKIML
Preorder traversal
To visit a tree, first, visit its root, then visit the left
subtree, and then visit the right subtree.
Solution:
ABDFGECHJIKLM
Postorder traversal
To visit a tree, first, visit its left subtree, then visit the right subtree, and then visit the root.
Solution:
FGDEBAJHKMLIC
Exercise
DISCRETE STRUCTURES 1 93
Trees
References:
1. Kenneth H. Rosen. Discrete Mathematics and Its Applications, 7th Edition. McGrawHill, 2012
2. Gary Weiss Damian Lyons, et al., Fundamentals of Discrete Structures, 2nd edition, Pearson
Learning Solutions, 2012.
3. Susanna S. Epp, Discrete Mathematics with Applications, Brooks Cole; 4th edition, 2011.
4. James L. Hein, Discrete Structures, Logic, and Computability, 3rd edition, Jones & Bartlett
Publishers, 3rd edition, 2009.
5. Kolman, B., Busby, R. C., Ross, S. C. Discrete Mathematical Structures, 6th Edition. Prentice Hall,
2008.
DISCRETE STRUCTURES 1 94