What are expression trees? What are its advantages? Derive the expression tree for the following algebraic expression
(a + (b/c)) * ((d/e) - f)
Expression Tree: Compilers need to generate assembly code in which one
operation is executed at a time and the result is retained for other
operations.
Construction of Expression Tree:
Now for constructing expression tree we use a stack. We loop through input expression and do following for every character.
1) If character is operand push that into stack
2) If character is operator pop two values from stack make them its child and push current node again.
At the end only element of stack will be root of expression tree.
Advantage:
1. Expression trees are using widely in LINQ to SQL, Entity Framework extensions where the runtime needs to interpret the expression in a different way (LINQ to SQL and EF: to create SQL, MVC: to determine the selected property or field).
2. Expression trees allow you to build code dynamically at runtime instead of statically typing it in the IDE and using a compiler.
(a + (b/c)) * ((d/e) - f)