250 likes | 403 Views
Section 9.3. Tree Traversal. Universal Address System. In ordered rooted trees, vertices may be labeled according to the following scheme: choose a root node and label it 0 each of root’s k children are labeled, left to right, as 1, 2, … , k
E N D
Section 9.3 Tree Traversal
Universal Address System • In ordered rooted trees, vertices may be labeled according to the following scheme: • choose a root node and label it 0 • each of root’s k children are labeled, left to right, as 1, 2, … , k • for each vertex v at level n with label A, label its kv children left to right as A.1, A.2, … A.k
Traversal Algorithms • Traversal: procedure for visiting each vertex in an ordered tree for data access • Three most commonly used traversal algorithms are: • preorder • inorder • postorder
Preorder Traversal • In preorder traversal, the root vertex is visited first • Then the left subtree is visited using a preorder traversal • Then the right subtree is visited using a preorder traversal • Gives same ordering of vertices as the universal address system
Pre-order traversal in action Original tree: Results: K K I K I R W R K W O O O O D D
Inorder traversal • From the root vertex, proceed to the left subtree and perform an inorder traversal • Return to root and access the data there • Traverse the right subtree using inorder traversal
In-order traversal in action Original tree: Results: K K I I R W K R K W O O O O D D
Postorder traversal • From root node, proceed to left subtree and perform postorder traversal • Perform postorder traversal of right subtree • Access data at root vertex
Post-order traversal in action K Original tree: Results: W K I I R O D O K W O R O D K
Infix, prefix and postfix notation • Ordered rooted trees (especially ordered binary trees) are useful in representing complicated expressions (e.g. compound propositions, arithmetic expressions) • A binary expression tree is a tree used to represent such an expression
Example 1 • Create an ordered tree to represent the expression (x+y)2 + (x-4)/3 • operands are represented as leaves • operators are represented as roots of subtrees
Example 1 Subtrees of binary expression tree for (x+y)2 + (x-4)/3: Complete binary expression tree for (x+y)2 + (x-4)/3: + / \ x y - / \ x 4 + / \ ^ / \ / \ + 2 - 3 / \ / \ x y x 4 ^ / \ + 2 / \ x y / \ - 3 / \ x 4
Traversing binary expression tree • Inorder traversal of binary expression tree produces original expression (without parentheses), in infix order • Preorder traversal produces a prefix expression • Postorder traversal produces a postfix expression
Prefix expressions • The prefix version of the expression (x+y)2 + (x-4)/3 is: + ^ + x y 2 / - x 4 3 • Evaluating prefix expressions: • Read expression right to left • When an operator is encountered, apply it to the previous operand (if unary) or operands (if binary) , placing the result back into the expression where the subexpression had been
Example 2 + * / 4 2 3 9 // original expression + * 2 3 9 // 4/2 evaluated + 6 9 // 2*3 evaluated 15 // 6+9 evaluated
Example 3 * - + 4 3 5 / + 2 4 3 // original expression * - + 4 3 5 / 6 3 // 2+4 evaluated * - + 4 3 5 2 // 6/3 evaluated * - 7 5 2 // 4+3 evaluated * 2 2 // 7-5 evaluated 4 // 2*2 evaluated
Postfix expressions • Also known as reverse Polish expressions • Like infix, they are evaluated left to right • Like prefix, they are unambiguous, not requiring parentheses • To evaluate: • read expression left to right; as soon as an operator is encountered, perform the operation and place the result back in the expression
Postfix expressions • Simple expression: • Original Expression: A + B • Postfix Equivalent: A B + • Compound expression with parentheses: • original: (A + B) * (C - D) • postfix: A B + C D - * • Compound expression without parentheses: • original: A + B * C - D • postfix: A B C * + D -
Example 4 6 3 / 4 2 * + // original expression 2 4 2 * + // 6/3 evaluated 2 8 + // 4*2 evaluated 10 // 2+8 evaluated
Example 5 5 4 * 10 2 - 2 / + 3 * // original expression 20 10 2 - 2 / + 3 * // 5*4 evaluated 20 8 2 / + 3 * // 10-2 evaluated 20 4 + 3 * // 8/2 evaluated 24 3 * // 20+4 evaluated 72 // 24*3 evaluated
Using rooted trees to represent compound propositions • Works exactly the same way as arithmetic expressions • Innermost expression is bottom left subtree, with proposition(s) as leaf(s) and operator as root • Root vertex is operator of outermost expression • Using various traversal methods, can produce infix, prefix and postfix versions of compound proposition
Example 6 Find the ordered rooted tree representing the compound proposition ((p q) (p q) Complete binary expression tree: Subtrees: / \ p q | p | q / \ | / \ / \ | | p q p q / \ | | p q | / \ p q
Example 6 / \ | / \ / \ | | p q p q Preorder traversal yields the expression: p q p q Postorder traversal yields the expression: p q p q
Section 9.3 Tree Traversal