First time here? Checkout the FAQ!
x
menu search
brightness_auto
more_vert

What are expression trees? What are its advantages? Derive the expression tree for the following algebraic expression

(a + (b/c)) * ((d/e) - f)

thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike

1 Answer

more_vert
 
verified
Best answer

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.

  •  Therefore, all expression has to be broken down unambiguously into separate operations and put into their proper order.
  •  Hence, expression tree is useful which imposes an order on the execution of operations.
  • -Expression tree is a binary tree in which each internal node corresponds to operator and each leaf node corresponds to operand
  • Parentheses do not appear in expresion trees, but their intent remains intact in tree representation.


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)

thumb_up_off_alt 0 like thumb_down_off_alt 0 dislike
...