5. Tree
s
Definition: A tree is a connected undirected graph with no simple
circuits.
Definition: An undirected graph is a tree if and only if there is a
unique simple path between any two of its vertices. A tree cannot
contain multiple edges or loops.
Definition: An undirected graph is a tree if and only if there is a
unique simple path between any two of its vertices.
6. Trees
Example: Which of these graphs are trees?
Solution: G and G are trees - both are connected and have no
simple circuits. G is not a tree because e, b, a, d, e is a simple
circuit,. G is not a tree because it is not connected.
7. FOREST
Definition: A forest is a graph that has no simple circuit, but is not
connected. Each of the connected components in a forest is a tree.
9. Trees as Models
• Trees are used as models in computer science, chemistry, geology,
botany, psychology, and many other areas.
• Trees were introduced by the mathematician Cayley in in
his work counting the number of isomers of saturated
hydrocarbons. The two isomers of butane are:.
Arthur Cayley
( )
10. Trees as Models
• The organization of a computer file system into directories,
subdirectories, and files is naturally represented as a tree.
11. Trees as Models
• Trees are used to represent the structure of organizations.
12. Applications of Trees
•Game Trees
Trees can be used to analyze certain types of games
such as tic-tac-toe, nim, checkers, and chess.
15. Prefix
code
Definition: A code that has the property that
the code of a character is never a prefix of the
code of another character.
• A prefix code can be represented using a
binary tree, where the characters are the
labels of the leaves in the tree.
• The edges of the tree are labeled so that an
edge leading to a left child is assigned a 0 and
an edge leading to a right child is assigned a
1.
• The bit string used to encode a character is
the sequence of labels of the edges in the
unique path from the root to the leaf that has
this character as its label.
• For instance, the tree in Figure 5 represents
the encoding of e by 0, a by 10, t by 110, n by
1110, and s by 1111.
16. Decision Trees
Definition: A rooted tree where each vertex represents a
possible outcome of a decision and the leaves represent the
possible solutions of a problem.
• Rooted trees can be used to model problems in which a series
of decisions leads to a solution.
• The possible solutions of the problem correspond to the paths
to the leaves of this rooted tree.
17. Example : A decision tree that orders the elements of
the list a, b, c.
18. Rooted Trees
Definition: A rooted tree is a tree in which one vertex has been
designated as the root and every edge is directed away from the
root.
• An unrooted tree is converted into different rooted trees when
different vertices are chosen as the root.
19. Rooted Tree Terminology
• Terminology for rooted trees is a mix from botany and genealogy (such as
this family tree of the Bernoulli family of mathematicians).
20. Rooted Tree Terminology
• If v is a vertex of a rooted tree other than the root, the parent of v is the unique
vertex u such that there is a directed edge from u to v. When u is a parent of v, v
is called a child of u. Vertices with the same parent are called siblings.
21. Rooted Tree Terminology
• The ancestors of a vertex 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. The
subtree rooted at u includes all the descendants of u, and all edges that connect
between them.
22. Rooted Tree Terminology
• A vertex of a rooted tree with no children is called a leaf. Vertices that have
children are called internal vertices.
23. Terminology for Rooted Trees
Example: In the rooted tree T (with
root a):
(i) Find the parent of c, the children
of g, the siblings of h, the
ancestors of e, and the
descendants of b.
Solution:
(i) The parent of c is b. The children
of g are h, i, and j. The siblings
of h are i and j. The ancestors of
e are c, b, and a. The
descendants of b are c, d, and e.
24. Terminology for Rooted Trees
Example: In the rooted tree T
(with root a):
(i) Find all internal
vertices and all leaves.
Solution:
(i) The internal vertices are
a, b, c, g, h, and j. The
leaves are d, e, f, i, k, l,
and m.
25. Terminology for Rooted Trees
(i) What is the subtree
rooted at g?
Solution:
(i) We display the subtree
rooted at g.
26. Level of vertices and height of trees
• When working with trees, we often want to have rooted trees where the
sub trees at each vertex contain paths of approximately the same length.
• To make this idea precise we need some definitions:
• The level of a vertex v in a rooted tree is the length of the unique path from the
root to this vertex.
• The height of a rooted tree is the maximum of the levels of the vertices.
27. Level of vertices and height of trees
Example:
(i) Find the level of each vertex
in the tree to the right.
(ii) What is the height of the
tree?
Solution:
(iii)The root a is at level .
Vertices b, j, and k are at level .
Vertices c, e, f, and l are at level .
Vertices d, g, i, m, and n are at level
. Vertex h is at level .
(ii) The height is , since is the
largest level of any vertex.
28. m-ary Rooted Trees
Definition: 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 = is called a binary tree.
Example: Are the following rooted trees full m-ary trees for
some positive integer m?
29. Solution:
• T is a full binary tree because each of its internal
vertices has two children.
vertices has three children.
In T each internal vertex has five children,
so T is a full -ary tree.
• T is a full -ary tree because each of its
internal
•
•
T is not a full m-ary tree for any m because some of its
internal vertices have two children and others have
three children.
30. Balanced m-Ary Trees
Definition: A rooted m-ary tree of height h is balanced if all leaves are at
levels h or h .
Example: Which of the rooted trees shown below is balanced?
Solution: T1 and T3 are balanced, but T2 is not because it has leaves at
levels , , and .
31. Ordered Rooted Trees
Definition: An ordered rooted tree is a rooted tree where the children of
each internal vertex are ordered.
• We draw ordered rooted trees so that the children of each internal
vertex are shown in order from left to right.
32. Binary Trees
Definition: A binary tree is an ordered rooted where each internal vertex has at most
two children. If an internal vertex of a binary tree has two children, the first is called
the left child and the second 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 this vertex.
33. Example:
Consider the binary tree T.
(i) What are the left and right children of d?
(ii) What are the left and right sub trees of c?
Solution:
(i) The left child of d is f and the right child is g.
(ii) The left and right subtrees of c are displayed in (b) and (c).
34. Properties of Trees
• A tree with n vertices has n edges.
• A full m-ary tree with i internal vertices has n = mi
vertices.
• A full m-ary tree with:
l
(ii) i
l
(iii) l
(i) n vertices has i = (n − 1)/m internal vertices and
= [(m − 1)n + 1]/m leaves,
n
mi
(
i l
• There are at most leaves in an m-ary tree of
height h.
35. Binary Search Tree
Definition: A binary tree in which the vertices are labeled with
items so that a label of a vertex is greater than the labels of all
vertices in the left subtree of this vertex and is less than the
labels of all vertices in the right subtree of this vertex.
• Searching for items in a list is one of the most important tasks
that arises in computer science.
• Our primary goal is to implement a searching algorithm that
finds items efficiently when the items are totally ordered. This
can be accomplished through the use of a binary search tree.
36. Example : Form a binary search tree for the words mathematics,
physics, geography, zoology, meteorology, geology, psychology,
and chemistry (using alphabetical order).