What's an Expression Tree?
Expression Tree
Expression Trees are binary trees whose parent nodes are operators and children nodes are operands of which the operators will execute on. Refer to the Expression Tree Visualizer for the Expression Tree representation of the expression (8 - 2 * 3 + 7).
What's so special about it?
There are three basic ways to traverse binary trees: Preorder, Inorder, and Postorder. Sound familiar? Yup, for expression trees, preorder traversal outputs prefix notation, inorder outputs infix, postorder outputs postfix!
How do these three traversals work?
All these traversals use recursion, which is straightforward for a computer but often difficult for humans to grasp. Hence, I've provided buttons to help you visualize each traversal in action!
Preorder:
- Put current node in result
- Go to left child, repeat
- Go to right child, repeat
Inorder:
- Go to left child, repeat
- Put current node in result
- Go to right child, repeat
Postorder:
- Go to left child, repeat
- Go to right child, repeat
- Put current node in result
Expression Tree Visualizer
it's interactive! (scroll to zoom / drag to pan)!
Result:
Run Visualizer: