

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Math 210 Tree Traversal Handout
Main Idea: A traversal algorithm is a procedure for visiting every vertex of an ordered rooted tree. The three methods we will look at give instructions for visiting the vertices of am ordered rooted tree using a recursive procedure.
Example: Consider the following tree:
a
b c^ d
e f g h i
j (^) k (^) l m n (^) o p (^) q
r s t
Applying the procedure described above, the preorder traversal is:
Example: Applying inorder to the tree shown above gives the following:
Example: Applying postorder to the tree shown above gives the following:
Representing Computations Using Binary Trees: Notice that many mathematical operations (arithmetic, most logical connectives, most set operations) are binary operations (they combine two inputs into a single output). For this reason, we can represent computations using binary trees as follows: The leaves of the tree are the elements being combined. Higher levels in the tree are then labeled with the binary operation being used to combine pairs of elements.
Example: Consider the following computation: [x + (y · z)] ÷ [w · (s + t)]
The following tree represents this computation:
s
w
y z
x (^) +
t
Once we have a tree representing a computation, we can find a expression representing the computation based on the tree in three ways:
The prefix form for the computation tree shown above is: ÷ + x · yz · w + st
The infix form for the computation tree shown above is: x + y · z ÷ w · s + t
The postfix form for the computation tree shown above is: xyz · +wst + ·÷