Presentation on theme: "Chapter 10, Section 10.3 Tree Traversal These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed.,"— Presentation transcript:
Chapter 10, Section 10.3 Tree Traversal These class notes are based on material from our textbook, Discrete Mathematics and Its Applications, 6 th ed., by Kenneth H. Rosen, published by McGraw Hill, Boston, MA, 2006. They are intended for classroom use only and are not a substitute for reading the textbook.
Universal Address Systems To totally order the vertices of on orered rooted tree: 1.Label the root with the integer 0. 2.Label its k children (at level 1) from left to right with 1, 2, 3, …, k. 3.For each vertex v at level n with label A, label its k v children, from left to right, with A.1, A.2, A.3, …, A.k v. This labeling is called the universal address system of the ordered rooted tree.
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 The three common traversals are: –Preorder traversal –Inorder traversal –Postorder traversal
Traversal Let T be an ordered rooted tree with root r. Suppose T 1, T 2, …,T n are the subtrees at r from left to right in T. r T1T1 T2T2 TnTn
Preorder Traversal r T1T1 T2T2 TnTn Step 1: Visit r Step 2: Visit T 1 in preorder Step 3: Visit T 2 in preorder. Step n+1: Visit T n in preorder
Inorder Traversal Step 1: Visit T 1 in inorder Step 2: Visit r Step 3: Visit T 2 in inorder. Step n+1: Visit T n in inorder r T1T1 T2T2 TnTn
Example AREYPMHJQT A R EY P M HJ QT Tree: Visiting sequence:
The Inorder Traversal of T In which order does an inorder traversal visit the vertices in the ordered rooted tree T shown to the left? Inorder: Visit leftmost tree, visit root, visit other subtrees left to right.
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
Example (x+y) 2 + (x-3)/(y+2) + x y 2 – x 3 + y 2 / +
Infix Notation + – + / + 2 x y x 3 y 2 Traverse in inorder adding parentheses for each operation x + y () 2 () + x – 3() / y + 2() () ()
Prefix Notation (Polish Notation) Traverse in preorder: x + y 2 + x – 3 / y + 2 + – + / + 2 x y x 3 y 2
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
Postfix Notation (Reverse Polish) Traverse in postorder x + y 2 + x – 3 / y + 2 + – + / + 2 x y x 3 y 2
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 Evaluating Postfix Notation