Docsity

Tree Traversal Algorithms, Summaries of Discrete Mathematics

Toggle sidebar
bg1
Math 210
Tree Traversal Handout
Main Idea: Atraversal 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.
1. Visiting Vertices in Preorder:
Let Tbe an ordered rooted tree with root r. If Thas only one vertex, then ris the preorder traversal of the tree
T. Otherwise, let T1, T2, ..., Tnbe the subtrees associated with each child of rordered from left to right. In preorder
traversal, we begin by visiting the root r. We then traverse the subtree T1using preorder, followed by T2, and so on,
until we finish visiting Tn. Note that each subtree is considered as a separate tree, so when applying preorder to a
subtree, we begin by visiting its root, and then applying preorder traversal to each of its subtrees. [The mantra here
is “root first, then traverse subtrees from left to right”]
Example: Consider the following tree:
a
bc d
ef g h i
jkl m nopq
r s t
Applying the procedure described above, the preorder traversal is:
2. Visiting Vertices in Inorder:
Let Tbe an ordered rooted tree with root r. If Thas only one vertex, then ris the inorder traversal of the tree
T. Otherwise, let T1, T2, ..., Tnb e the subtrees associated with each child of rordered from left to right. In inorder
traversal, we traverse the subtree T1using inorder, and then visit the root of T1, followed by T2and then its root,
and so on, until we finish visiting Tn, and its root, and then visiting the root r. Note that each subtree is considered
as a separate tree, so when applying inorder to a subtree, we apply the procedure for inorder traversal to each of its
subtrees. [The mantra here is “subtree root last, traverse subtrees from left to right”]
Example: Applying inorder to the tree shown above gives the following:
3. Visiting Vertices in Postorder:
Let Tbe an ordered rooted tree with root r. If Thas only one vertex, then ris the postorder traversal of the tree
T. Otherwise, let T1, T2, ..., Tnbe the subtrees associated with each child of rordered from left to right. In postorder
traversal, we traverse the subtree T1using postorder, followed by T2, and so on, until we finish visiting Tn, and then
visiting the root r. Note that each subtree is considered as a separate tree, so when applying postorder to a subtree,
we apply the procedure for postorder traversal to each of its subtrees. [The mantra here is “each root last, traverse
subtrees from left to right”]
Example: Applying postorder to the tree shown above gives the following:
pf2

Partial preview of the text

Download Tree Traversal Algorithms and more Summaries Discrete Mathematics in PDF only on Docsity!

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.

  1. Visiting Vertices in Preorder: Let T be an ordered rooted tree with root r. If T has only one vertex, then r is the preorder traversal of the tree T. Otherwise, let T 1 , T 2 , ..., Tn be the subtrees associated with each child of r ordered from left to right. In preorder traversal, we begin by visiting the root r. We then traverse the subtree T 1 using preorder, followed by T 2 , and so on, until we finish visiting Tn. Note that each subtree is considered as a separate tree, so when applying preorder to a subtree, we begin by visiting its root, and then applying preorder traversal to each of its subtrees. [The mantra here is “root first, then traverse subtrees from left to right”]

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:

  1. Visiting Vertices in Inorder: Let T be an ordered rooted tree with root r. If T has only one vertex, then r is the inorder traversal of the tree T. Otherwise, let T 1 , T 2 , ..., Tn be the subtrees associated with each child of r ordered from left to right. In inorder traversal, we traverse the subtree T 1 using inorder, and then visit the root of T 1 , followed by T 2 and then its root, and so on, until we finish visiting Tn, and its root, and then visiting the root r. Note that each subtree is considered as a separate tree, so when applying inorder to a subtree, we apply the procedure for inorder traversal to each of its subtrees. [The mantra here is “subtree root last, traverse subtrees from left to right”]

Example: Applying inorder to the tree shown above gives the following:

  1. Visiting Vertices in Postorder: Let T be an ordered rooted tree with root r. If T has only one vertex, then r is the postorder traversal of the tree T. Otherwise, let T 1 , T 2 , ..., Tn be the subtrees associated with each child of r ordered from left to right. In postorder traversal, we traverse the subtree T 1 using postorder, followed by T 2 , and so on, until we finish visiting Tn, and then visiting the root r. Note that each subtree is considered as a separate tree, so when applying postorder to a subtree, we apply the procedure for postorder traversal to each of its subtrees. [The mantra here is “each root last, traverse subtrees from left to right”]

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:

  1. The prefix form [or “Polish” form] for a computation tree is found by traversing the tree using preorder.

The prefix form for the computation tree shown above is: ÷ + x · yz · w + st

  1. The infix form [or “Parenthesized” form] for a computation tree is found by traversing the tree using inorder.

The infix form for the computation tree shown above is: x + y · z ÷ w · s + t

  1. The postfix form [or “Reverse Polish” form] for a computation is found by traversing the tree using postorder.

The postfix form for the computation tree shown above is: xyz · +wst + ·÷