You are on page 1of 9

QM1063 CHAPTER 5

Chapter 5
TREES
5.1 Introduction
Trees are particularly useful in computer science, where
they are employed in a wide range of algorithms.
Defination 1:
A tree is a connected undirected graph with no simple
circuit / without any circles.
A graph with n vertices is a tree if and only if it has n 1
edges and no cycles.

Example 1
Which of the graphs shown in figure 2 are trees? (Susanna,
705)

QM1063 CHAPTER 5

Example 2
Determine whether a graph G has ten vertices and twelve
edges is a tree or not?

Definition 2
Let T be a tree. If T has only one or two vertices, then each
is called a terminal vertex. If T has at least three vertices,
then a vertex of degree 1 in T is called a terminal vertex
(leaf) and a vertex of degree greater 1 in T is called an
internal vertex (branch vertex).
Example 3
Find all terminal vertices and all internal vertices in the
following tree.

QM1063 CHAPTER 5

Definition 3
A rooted tree is a tree in which one vertex is distinguished
from the others and it is called the root. The level of a
vertex is the number of edges along the unique path
between it and the root. The height of a rooted tree is the
maximum level to any vertex of the tree. Given any root of
any internal vertex v of a rooted tree, the children of v are
all those vertices that are adjacent to v. If w is a child of v,
then v is called parent of w, two vertices that are both
children of the same parent are called siblings. Given
vertices v and w, if v lies on the unique path between w and
the root, the v is an ancestor of w and w is a descendant of
v.

QM1063 CHAPTER 5

Example 4
(a) Consider the tree shown below with root a:
a

i.

What is the level of n?

ii.

What is the height of this rooted tree?

iii.

What are the children of n?

iv.

What is the parent of g?

v.

What are the siblings of j?

vi.

What are the descendants of f?

QM1063 CHAPTER 5

Definition 4
A binary tree is a rooted tree in which every parent has at
most two children. Each child in a binary tree is designed
either a left child or a right child (but not both), and every
parent has at most one left child and one right child. A full
binary tree is a binary tree in which each parent has
exactly two children.
Example 5
Draw a binary
a b c d / e 2 .

tree

to

represent

the

expression

Example 6
Construct a binary search tree for the strings math,
computer, power, north, zoo, donkey, and book

QM1063 CHAPTER 5

5.2 Applications of Trees


There are numerous important applications trees included:
Network optimization with minimum spinning tree.
Problem solving with backtracking in decision
trees.
Data compression with prefix codes in Huffman
coding trees.

5.3 Spanning Trees


Definition 5
A tree T is a spanning tree of a graph G if T is a subgraph
of G that contains all of the vertices of G.
Example 7
(a)

Find all spanning trees for the graph below:

v5

v4

v3

v0

v1

v2
6

QM1063 CHAPTER 5

5.4 Minimal Spanning Trees


Definition 6
Let G be a weighted graph. A minimal spanning tree of G
is a spanning tree of G with minimum weight that is has the
smallest possible sum weights of its edges.
How can we find a minimum spanning tree?
8

b
9

e
3

f
10

7
12

6
9

h
g

QM1063 CHAPTER 5

The algorithm to find a minimal spanning tree is known


as Prims Algorithm.
This algorithm builds tree by iteratively adding edges
until a minimal spanning tree is obtained.
The algorithm begins with a single vertex. Then at each
iteration, it adds to the current tree a minimum-weight
edges that does not complete a cycle.

Prims Algorithm
This algorithm finds a minimal spanning tree in a
connected, weighted graph.
Begin by choosing any edges with smallest weight
and putting it into the spanning tree,
Successively add to the tree edges of minimum
weight that are incident to a vertex already in the tree
and not forming a simple circuit with those edges
already in the tree,
Stop when (n-1) edges have been added.

QM1063 CHAPTER 5

Kruskals Algorithm
Kruskals algorithm is identical to Prims algorithm, except
that it does not demand new edges to be incident to a vertex
already in the tree.
Both algorithm are guaranteed to produce a minimum
spanning tree of a connected weighted graph.

You might also like