Expression trees
Introduction / Definition
An expression tree represents an expression structurally. Leaves are operands and internal nodes are operators. Traversal orders correspond to prefix, infix, and postfix notation. Expression trees are used in compilers and interpreters.
Syntax / Structure / Formula
operator(left,right)
Detailed Explanation
For (2+3)*4, '*' is the root, '+' is its left child, and 4 is its right child. Postorder traversal is useful for evaluation.
Example
Example: Evaluate (2+3)\*4 using the tree.
Output / Result
The result is 20.
Important Points / Notes
- Structure encodes precedence.
- Postorder is useful for evaluation.
Mini Practice Question
Draw the tree for 2*(3+4).
CheckpointTest Yourself