Download in order traversal

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Section10.3: Tree Traversal
Introduction
Ordered rooted trees are often used to store information. We need procedures for
visiting each vertex of an ordered rooted tree to access data. We will describe
several important algorithms for visiting all the vertices of an ordered rooted tree.
Ordered rooted trees can also be used to represent various types of expressions,
such as arithmetic expressions involving numbers, variables, and operations. The
different listing of ordered rooted trees used to represent expressions are useful in
the evaluation of these expressions.
Universal Address Systems
Procedure for traversing all vertices of an ordered rooted tree rely on the ordering of
children. In ordered rooted trees, the children of an internal vertex are shown from
left to right in the drawing representing these directed graphs.
Procedure:
1)
Label the root with the integer 0. then label its k children (at level 1) from left to
right with 1,2,3,…k.
2)
For each vertex v at level n with label A, label its k v children, as they are drawn
from left to right, with A.1,A.2,… A.k v
Example1: construct an ordered rooted tree using universal address system. The
lexicographic ordering of the labeling is
0<1<1.1<1.2<1.3<2<3<3.1<3.1.1<3.1.2<3.1.2.1<3.1.2.2<3.1.2.3<3.1.2.4<3.1.3<3.2<4<
4.1<5<5.1<5.1.1<5.2<5.3
Solution:
Example2:Construct the universal address system for the given ordered tree. Then use
this to order its vertices using lexicographic order of their labels.
Solution:
0<1<1.1<1.2<2<3
0<1<1.1<1.1.1<1.1.1.1<1.1.1.1.1<1.1.1.1.2<1.1.1.2<1.1.2<1.2<2
0<1<1.1<1.2<1.2.1<1.2.1.1<1.2.1.2<1.2.2<1.2.3<1.2.3.1<1.2.3.2<1.2.3.2.1<1.2.3.2.2<1.
2.3.3<2<2.1
Traversal Algorithms:
Definition: procedures for systematically visiting every vertex of an ordered rooted tree
are called traversal algorithms.
We will describe 3 of the most commonly used such algorithms:
1)
Preordered traversal
2)
In ordered traversal
3)
Post ordered traversal
Read definition (1) page 712.
Remark: the pre ordered traversal of an ordered rooted tree gives the same ordering of
the vertices as the ordering obtained using a universal address system.
Example2: in which order does a pre order traversal visit the vertices in the ordered
rooted tree T.
solution:
Example: determine the order in which a pre order traversal visits the vertices of the
given ordered rooted tree.
1)
Solution: 1)
2)
2)
In order Traversal
Read definition2 page 714
Example: in which order does an in order traversal visit the vertices of the ordered
rooted tree T.
T:
in order traversal: visit left most sub tree, visit root, visit
other sub trees left to right.
Solution:
Example: in which order are the vertices of the ordered rooted tree T visited using an in
order traversal?
solution:
Post order traversal
Example4 page715:
In which order does a post order traversal visit the vertices of the ordered rooted tree T.
post order traversal visit sub trees left to right, visit root.
Solution:
Example: In which order are the vertices of the ordered rooted tree T visited using post
ordered rooted tree T visited using post order traversal.
Solution:
2)
Remark: note that both the pre order traversal and the post order traversal encode the
structure of an ordered rooted tree when the number of children of each vertex is
specified. That is an ordered rooted tree is uniquely determined when we specify a
list of vertices generated by a pre order traversal or by a post order traversal of the
tree, together with the number of children of each vertex. In particular, both a pre
order traversal and post order traversal encode the structure of a full ordered in-ary
tree.
In fix, prefix, and post fix Notation.
1)
We can represent complicated expressions, such as compound proposition,
combinations of sets, and arithmetic expressions using ordered rooted trees.
2)
Consider the representations of an arithmetic expression involving +,-,*,/ and ↑.
We will use parentheses to indicate the order of operations. An ordered rooted
tree can be used to represent such expressions, where:
i)
The internal vertices represent operations.
ii)
The leaves represent the variables or numbers.
iii)
Each operation operates on its left and right sub trees (in that order).
Example5 page719
What is the ordered rooted tree that represents the expression ((x+y)↑2)+((x-1)/3)?
Solution: the binary tree for this expression can be built from bottom up.
Infix form: to make expressions unambiguous it is necessary to include parentheses in
the in order traversal whenever we encounter an operation. The fully parenthesized
expression obtained in this way is said to be in infix form.
Prefix form: we obtain the prefix form of an expression when we traverse its rooted tree
in pre order. Expressions written in prefix form are said to be in polish notation. An
expression in prefix notation is unambiguous so no parentheses are needed in such
an expression.
Example6 page720: what is the prefix form for ((x+y)↑2)+((x-4)/3)?
Solution: + ↑ + x y 2 / - x 4 3
(from left to right): + ↑ + x y 2 / - x 4 3
Example7 page720: what is the value of the prefix expression + - * 2 3 5 / ↑ 2 3 4?
Solution: + - * 2 3 5 / ↑ 2 3 4
(binary) start from right from first operation(↑):
23 4
+-*235/

2 3 8
+ - * 2 3 5 /84

8
2
4
235 2
+- *

2*3 6
6 52
+ 

6 51

12

1 2  3
Evaluating a prefix expression so the value of this expression is3.
Post fix form: we obtain the post fix form of an expression by traversing its binary tree in
post order.
Expressions written in postfix form are said to be reverse polish notation. (parentheses
are not needed).
example8 page 720: what is the post fix form of the expression ((x+y)↑2)+((x-4)/4)?
Answer: x y + 2 ↑ x 4 – 3 / +
xy  2  
x 4

3/ 



leftsubtree
rightsubtree
Example9: what is the value of the postfix expression 723*-4↑93/+?
72
3 *  4  9 3 /

start from left to right
2.3 6
7
6  4  9 3 /

7  6 1
1
4  9 3 /
14 1
19
3/ 

9 / 33
1
3
1 3 4
Example10: fin the ordered rooted tree representing the compound proposition
 p  q  p  q . Then use this rooted tree to find the prefix, postfix and infix form of
this expression.
Solution: the rooted tree for this compound proposition is constructed from bottom up.
Constructing the rooted tree for a compound proposition
Example: the prefix, postfix, and infix forms of this operation are found by traversing this
rooted tree in preorder, post order, and in order (including parentheses), respectively:
From Rooted Tree:
Prefix (preorder traversing):
   pq  pq
Postfix (post order traversal):
pq  pq   
Infix (in order traversal):
 p  q  p  q