Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

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:

After completing this module, you should be able to:

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

The concept of trees are used in everyday life

Some common applications of it are:

1. Tournament tree, used to describe the outcome of a series of games

2. Much of the tree terminology derives from family trees.

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:

The graph is an undirected graph and contains no


cycles, thus it is a Tree

DISCRETE STRUCTURES 1 86
Trees

The graph is not a tree because it contains the cycle {a,


b}, {b. e}, {e. a}

The graph is not connected, so it cannot be a tree,


however, each component of the graph is a tree

When graph is a tree, we write T instead of a G to emphasize Tree structure.

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

Which of these graphs are trees?

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.

A graph may have more than one spanning tree.

Any two spanning trees for a graph have the same number of edges.

If a graph is a tree, then its only spanning tree is itself.

Example:

Find the Spanning Tree of the


graph.

Solution:

The graph has six vertices {a, b, c, d, e, f} and nine


edges. Following the formula that a tree of
vertices n has n-1 edges, 6-1-5, therefore, the
tree should have 5 edges.

We then delete an edge in each cycle.

The number of edges to be deleted is equals to |E|-|V|+1

E-V+1

9-6+1=4 edges

Four edges should be deleted namely

{a,f}, {b,e}, {f,e}, {e,d}

Since, |E|-|V|+1

Then in every Tree T = (V,E)

|V| = |E|+1

A tree is not a tree if it has |E|  |V|

DISCRETE STRUCTURES 1 88
Trees

Exercise

Find all the spanning trees of the graph.

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:

The tree is directed but not rooted

DISCRETE STRUCTURES 1 89
Trees

Example:

Rooted Tree with root R

Suppose that T is a rooted tree.

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

If u is the parent of v, v is called a child of u

Vertices with the same parent are called siblings.

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 descendants of a vertex v are those vertices that have v as an ancestor.

A vertex of a rooted tree is called a leaf if it has no children.

Vertices that have children are called internal vertices.

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.

m-ary Rooted Trees

 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

Full 3-ary tree because each of its internal vertices has


three children.

Not a full m-ary tree for any m because some of its


internal vertices have two children and others have
three children.

Ordered Rooted Tree

 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:

Not Binary Tree

DISCRETE STRUCTURES 1 91
Trees

Example:

What are the left and right children of D in the binary?

Solution:

The left child of D is F and the right child is G.

The root of the Tree is A, and B and C are the left


child and right child respectively of the parent A.

B and C are siblings, they are the left child and


right child respectively of A.

The nodes are siblings if they are left child and


right child of the same parent.

The root of the binary tree has level 0.

The level of any other node in the three is one more than the level of its parent.

Binary Tree Traversal

 A tree traversal is a specific order in which to visit the nodes of a tree.

There are 3 common tree traversals.

1. inorder: traversal is left, root, right


2. preorder: traversal is root, left, right
3. postorder: traversal is left, right, root

This order is applied recursively.

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

Given the Tree, perform the inorder,


preorder, and postorder traversal.

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

You might also like