Академический Документы
Профессиональный Документы
Культура Документы
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.
ii.
iii.
iv.
v.
vi.
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
v5
v4
v3
v0
v1
v2
6
QM1063 CHAPTER 5
b
9
e
3
f
10
7
12
6
9
h
g
QM1063 CHAPTER 5
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.