0% found this document useful (0 votes)
9 views29 pages

9 Trees

Uploaded by

oscar23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views29 pages

9 Trees

Uploaded by

oscar23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Trees

Slides adapted from Kenneth H Rosen


Trees 1

Definition: A tree is a connected undirected graph with no


simple circuits.

G1 and G2 are trees - both are connected and have no simple


circuits. Because e, b, a, d, e is a simple circuit, G3 is not a
tree. G4 is not a tree because it is not connected.

© 2019 McGraw-Hill Education


Trees 1

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.

© 2019 McGraw-Hill Education


Trees 2

Theorem: An undirected graph is a tree if and only if there is a unique


simple path between any two of its vertices.

© 2019 McGraw-Hill Education


Trees as Models
The organization of a computer file system into
directories, subdirectories, and files is naturally
represented as a tree.
Trees are used to represent the structure of
organizations.

© 2019 McGraw-Hill Education


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.

© 2019 McGraw-Hill Education


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.
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.
A vertex of a rooted tree with no children is called a leaf. Vertices that have
children are called internal vertices.
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.

© 2019 McGraw-Hill Education


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.
(ii) Find all internal vertices and all leaves.
(iii) What is the subtree rooted at G?

© 2019 McGraw-Hill Education


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.
(ii) Find all internal vertices and all leaves.
(iii) What is the subtree rooted at G?

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.
(ii) The internal vertices are a, b, c, g, h, and j. The
leaves are d, e, f, i, k, l, and m.
(iii) We display the subtree rooted at g.

© 2019 McGraw-Hill Education


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 = 2
is called a binary tree.

Example: Are the following rooted trees full m-ary trees for some
positive integer m?

© 2019 McGraw-Hill Education


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.
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.

© 2019 McGraw-Hill Education


Ordered Rooted Trees
Example: Consider the binary tree T.
I. What are the left and right children of d?
II. What are the left and right subtrees of c?

© 2019 McGraw-Hill Education


Properties of Trees
Theorem 2: A tree with n vertices has n − 1 edges.

Theorem 3: A full m-ary tree with i internal vertices has


n = mi + 1 vertices.

© 2019 McGraw-Hill Education


Counting Vertices in Full m-Ary Trees 2

Theorem 4: A full m-ary tree with


I. n vertices has i = (n − 1)/m internal vertices and l = [(m − 1)n
+ 1]/m leaves,
II. i internal vertices has n = mi + 1 vertices
and l = (m − 1)i + 1 leaves,
III. l leaves has n = (ml − 1)/(m − 1) vertices
and i = (l − 1)/ (m − 1) internal vertices.

© 2019 McGraw-Hill Education


Level of vertices and height of trees
• 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.

© 2019 McGraw-Hill Education


Balanced m-Ary Trees
Definition: A rooted m-ary tree of height h is
balanced if all leaves are at levels h or h − 1.

© 2019 McGraw-Hill Education


Balanced m-Ary Trees
Example: Which of the rooted trees shown
below is balanced?

T1 and T3 are balanced, but T2 is not because it


has leaves at levels 2, 3, and 4.
© 2019 McGraw-Hill Education
Tree Traversal

© 2019 McGraw-Hill Education


Tree Traversal
Procedures for systematically visiting every
vertex of an ordered tree are called traversals.
The three most commonly used traversals are
preorder traversal, inorder traversal, and
postorder traversal.

© 2019 McGraw-Hill Education


Preorder Traversal 1

Definition: Let T be an ordered rooted tree with


root r. If T consists only of r, then r is the preorder
traversal of T. Otherwise, suppose that T1, T2, …,
Tn are the subtrees of r from left to right in T. The
preorder traversal begins by visiting r, and
continues by traversing T1 in preorder, then T2 in
preorder, and so on, until Tn
is traversed in preorder.

© 2019 McGraw-Hill Education


Preorder Traversal 2

The preorder traversal begins by visiting r, and continues by traversing T1 in preorder, then
T2 in preorder, and so on, until Tn is traversed in preorder.

© 2019 McGraw-Hill Education


Inorder Traversal 1

Definition: Let T be an ordered rooted tree with root


r. If T consists only of r, then r is the inorder traversal
of T. Otherwise, suppose that T1, T2, …, Tn are the
subtrees of r from left to right in T. The inorder
traversal begins by traversing T1 in inorder, then
visiting r, and continues by traversing T2 in inorder,
and so on, until Tn is traversed in inorder.

© 2019 McGraw-Hill Education


Inorder Traversal 2
The inorder traversal begins by traversing T1 in inorder, then visiting r, and continues by
traversing T2 in inorder, and so on, until Tn is traversed in inorder.

© 2019 McGraw-Hill Education


Postorder Traversal 1

Definition: Let T be an ordered rooted tree with root


r. If T consists only of r, then r is the postorder
traversal of T. Otherwise, suppose that T1, T2, …, Tn
are the subtrees of r from left to right in T. The
postorder traversal begins by traversing T1 in
postorder, then T2 in postorder, and so on, after Tn is
traversed in postorder, r is
visited.

© 2019 McGraw-Hill Education


Postorder Traversal 2

The postorder traversal begins by traversing T1 in postorder, then T2 in postorder, and so


on, after Tn is traversed in postorder, r is. visited.

© 2019 McGraw-Hill Education


Expression Trees
Complex expressions can be represented using
ordered rooted trees.
Consider the expression ( ( x + y )  2 ) + ( ( x − 4 ) / 3) .
A binary tree for the expression can be built from
the bottom up, as is illustrated here.

© 2019 McGraw-Hill Education


Infix Notation
An inorder traversal of the tree representing an
expression produces the original expression when
parentheses are included except for unary
operations, which now immediately follow their
operands.
We illustrate why parentheses are needed with an
example that displays three trees all yield the same
infix representation. `

© 2019 McGraw-Hill Education


Prefix Notation
When we traverse the rooted tree representation
of an expression in preorder, we obtain the prefix
form of the expression. Expressions in prefix form
are said to be in Polish notation, named after theExample: We show
Polish logician Jan Łukasiewicz. the steps used to
Operators precede their operands in the prefix evaluate a particular
form of an expression. Parentheses are not needed prefix expression:
as the representation is unambiguous.
The prefix form of ((x + y) ↑ 2 ) + ((x − 4)/3) is
+ ↑ + x y 2 / − x 4 3.
Prefix expressions are evaluated by working from
right to left. When we encounter an operator, we
perform the corresponding operation with the two
operations to the right.
© 2019 McGraw-Hill Education
Postfix Notation
We obtain the postfix form of an expression Example: We
by traversing its binary trees in postorder. show the steps
Expressions written in postfix form are said used to evaluate a
to be in reverse Polish notation. particular postfix
Parentheses are not needed as the postfix expression.
form is unambiguous.
x y + 2 ↑ x 4 − 3 / + is the postfix
form of ((x + y) ↑ 2 ) + ((x − 4)/3).
A binary operator follows its two operands.
So, to evaluate an expression one works from
left to right, carrying out an operation
represented by an operator on its preceding
operands.
© 2019 McGraw-Hill Education

You might also like