#1,175 – An Example of an Expression Tree

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:

1175-001

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:

1175-002

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
    • Operator: /
    • Right: 9