Download presentation
Presentation is loading. Please wait.
1
Section 9.3 by Andrew Watkins
Tree Traversal Section 9.3 by Andrew Watkins CSE 2813 Discrete Structures
2
CSE 2813 Discrete Structures
Traversal Algorithms A traversal algorithm is a procedure for systematically visiting every vertex of an ordered rooted tree An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered Three common traversals are: Preorder traversal Inorder traversal Postorder traversal CSE 2813 Discrete Structures
3
CSE 2813 Discrete Structures
Traversal Let T be an ordered rooted tree with root r. Suppose T1, T2, …,Tn are the subtrees at r from left to right in T. r T1 T2 Tn CSE 2813 Discrete Structures
4
CSE 2813 Discrete Structures
Preorder Traversal Step 1: Visit r r T1 T2 Tn Step 2: Visit T1 in preorder Step 3: Visit T2 in preorder Step n+1: Visit Tn in preorder CSE 2813 Discrete Structures
5
CSE 2813 Discrete Structures
Example A R E Y P M H J Q T M A J Y R H P Q T E CSE 2813 Discrete Structures
6
CSE 2813 Discrete Structures
Inorder Traversal Step 1: Visit T1 in inorder r T1 T2 Tn Step 2: Visit r Step 3: Visit T2 in inorder Step n+1: Visit Tn in inorder CSE 2813 Discrete Structures
7
CSE 2813 Discrete Structures
Example A R E Y P M H J Q T J A M R Y P H Q T E CSE 2813 Discrete Structures
8
CSE 2813 Discrete Structures
Postorder Traversal Step 1: Visit T1 in postorder r T1 T2 Tn Step 2: Visit T2 in postorder Step n: Visit Tn in postorder Step n+1: Visit r CSE 2813 Discrete Structures
9
CSE 2813 Discrete Structures
Example A R E Y P M H J Q T J A R P Q T H Y E M CSE 2813 Discrete Structures
10
Representing Arithmetic Expressions
Complicated arithmetic expressions can be represented by an ordered rooted tree Internal vertices represent operators Leaves represent operands Build the tree bottom-up Construct smaller subtrees Incorporate the smaller subtrees as part of larger subtrees CSE 2813 Discrete Structures
11
CSE 2813 Discrete Structures
Example (x+y)2 + (x-3)/(y+2) + 2 / + x y – x 3 + y 2 CSE 2813 Discrete Structures
12
CSE 2813 Discrete Structures
Infix Notation Traverse in inorder adding parentheses for each operation + – / 2 x y 3 ( ) ( ) ( ) x + y 2 + ( ) ( ) x – 3 / ( ) y + 2 CSE 2813 Discrete Structures
13
Prefix Notation (Polish Notation)
Traverse in preorder + – / 2 x y 3 + + x y 2 / – x 3 + y 2 CSE 2813 Discrete Structures
14
Evaluating Prefix Notation
In an prefix expression, a binary operator precedes its two operands The expression is evaluated right-left Look for the first operator from the right Evaluate the operator with the two operands immediately to its right CSE 2813 Discrete Structures
15
CSE 2813 Discrete Structures
Example + / / – + / / – + / / + / + / 3 CSE 2813 Discrete Structures
16
Postfix Notation (Reverse Polish)
Traverse in postorder + – / 2 x y 3 x y + 2 x 3 – y 2 + / + CSE 2813 Discrete Structures
17
Evaluating Postfix Notation
In an postfix expression, a binary operator follows its two operands The expression is evaluated left-right Look for the first operator from the left Evaluate the operator with the two operands immediately to its left CSE 2813 Discrete Structures
18
CSE 2813 Discrete Structures
Example / – / + / – / + – / + / + / + 3 CSE 2813 Discrete Structures
19
CSE 2813 Discrete Structures
Exercises 7, 9, 10, 12, 13, 15, 16, 17, 18, 23, 24 CSE 2813 Discrete Structures
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.