Understanding Tree Traversal: Preorder, Inorder, and Postorder Notations
This guide covers the fundamentals of tree traversal techniques, specifically focusing on Preorder, Inorder, and Postorder notations. Explore how each traversal method processes nodes—Preorder visits the root first, Inorder processes the left subtree, the root, and then the right subtree, while Postorder visits the left and right subtrees before the root. The guide also distinguishes between infix, prefix, and postfix notations, highlighting the importance of parentheses in infix expressions. Examples are provided to demonstrate the evaluation of expressions in different notations.
Understanding Tree Traversal: Preorder, Inorder, and Postorder Notations
E N D
Presentation Transcript
Pre/post fix notation and order • See handout . a . b c . d e f g h i j k
Preorder transversal- root first, then left to right a . b c . d e f g h i j k
Inorder transversal- left tree, then root, then rest of trees- left to right a b c d e f g h i j k
Postorder transversal- left to right, then root last a . b c . d e f g h i j k
Infix, prefix, and postfix • See handout • Infix order is ambiguous – so parentheses are required • Prefix and postfix are not– so parentheses are unnecessary • Prefix- “Polish notation” • Read from R to L • Operations are on the L • Postfix • Read from L to R • Operations are on the R
Example– transverse in 3 ways, and evaluate each notation + * 3 2 5 2 • Preorder- prefix • Inorder-infix • Postorder-postfix
Example- prefix • Evaluate a prefix expression + - * 2 3 5 / 2 3 4
Ex- postfix 7 2 3 * - 4 9 3 / +
Example– transverse in 3 ways, and evaluate each notation + * 5 2 3 4 • Preorder- prefix • Inorder-infix • Postorder-postfix - *
Find the value of each prefix expression (R to L,operations on the L) • - * 2 / 8 4 3 • - * 3 3 * 4 2 5
Find the value of each prefix expression (R to L,operations on the L) • + - 3 2 2 3 / 6 – 4 2 • * + 3 + 3 3 + 3 3 3
Find the value of each postfix expression (L to R, operations on R) • 5 2 1 - - 3 1 4 + + *
Find the value of each postfix expression (L to R, operations on R) • 9 3 / 5 + 7 2 - * • 3 2 * 2 5 3 – 8 4 / * -