#1,175 – An Example of an Expression Tree
September 4, 2014 Leave a comment
You can assign a lambda expression to an instance of an expression tree so that you then have the corresponding expression represented as a data structure. Below is an example of how an expression is represented in an expression tree.
Suppose that we have the expression for converting Fahrenheit to Celsius, as:
We create an expression tree by decomposing the expression into a left side, operator and right side. We then further decompose for any sub-expression that can be decomposed. The resulting tree looks as follows:
We can interpret the expression tree as follows:
- Main expression: (f – 32) * 5 / 9
- Left: (f – 32) * 5
- Left: (f – 32)
- Left: f
- Operator: –
- Right: 32
- Operator: *
- Right: 5
- Left: (f – 32)
- Operator: /
- Right: 9
- Left: (f – 32) * 5