2
What is an Expression tree? An expression tree for an arithmetic, relational, or logical expression is a binary tree in which: The parentheses in the expression do not appear. The leaves are the variables or constants in the expression. The non-leaf nodes are the operators in the expression: A node for a binary operator has two non-empty subtrees. A node for a unary operator has one non-empty subtree. The operators, constants, and variables are arranged in such a way that an inorder traversal of the tree produces the original expression without parentheses.

6
Why Expression trees? Expression trees are used to remove ambiguity in expressions. Consider the algebraic expression * Without the use of precedence rules or parentheses, different orders of evaluation are possible: ((2-3)*(4+5)) = -9 ((2-(3*4))+5) = -5 (2-((3*4)+5)) = -15 (((2-3)*4)+5) = 1 (2-(3*(4+5))) = -25 The expression is ambiguous because it uses infix notation: each operator is placed between its operands.

7
Why Expression trees? (contd.) Storing a fully parenthesized expression, such as ((x+2)-(y*(4-z))), is wasteful, since the parentheses in the expression need to be stored to properly evaluate the expression. A compiler will read an expression in a language like Java, and transform it into an expression tree. Expression trees impose a hierarchy on the operations in the expression. Terms deeper in the tree get evaluated first. This allows the establishment of the correct precedence of operations without using parentheses. Expression trees can be very useful for: Evaluation of the expression. Generating correct compiler code to actually compute the expression's value at execution time. Performing symbolic mathematical operations (such as differentiation) on the expression.

9
Prefix, Infix, and Postfix Forms A preorder traversal of an expression tree yields the prefix (or polish) form of the expression. In this form, every operator appears before its operand(s). An inorder traversal of an expression tree yields the infix form of the expression. In this form, every operator appears between its operand(s). A postorder traversal of an expression tree yields the postfix (or reverse polish) form of the expression. In this form, every operator appears after its operand(s). Prefix form: + a * - b c d Infix form: a + b - c * d Postfix form: a b c - d * + + *a d- cb

14
Infix to Postfix conversion (pseudo code) 1 Initialize stack to empty ; Initialize postFixQueue to empty ; 2while(infix expression has more tokens) 3{ // must be a valid infix expression 4 get current token ; 5 if(current token is an operand) 6 enqueue it in postFixQueue ; 7 else if(current token is a left parenthesis) 8 push it onto stack ; 9 else if(current token is a right parenthesis) 10 { 11 pop operators from the stack and enqueue them in postFixQueue until the top 12 of stack is a left parenthesis ; 13 pop and discard the left parenthesis ; 14 } 15 else if(current token is an operator) 16 { 17 while(stack is not empty AND top of stack is not a left parenthesis AND top 18 of stack is not a left-associative operator that has lower precedence than 19 the current token AND top of stack is not a right-associative operator that 20 has the same precedence as the current token) 21 pop operators from the stack and enqueue them in postFixQueue ; 22 push the current token onto the stack ; 23 } 24 } 25 while(stack is not empty) 26 { pop token from stack ; 27 enqueue token in postFixQueue ; 28 }

15
Constructing an expression tree from a postfix expression The pseudo code algorithm to convert a valid postfix expression, containing binary operators, to an expression tree: 1while(not the end of the expression) 2{ 3 if(the next symbol in the expression is an operand) 4 { 5 create a node for the operand ; 6 push the reference to the created node onto the stack ; 7 } 8 if(the next symbol in the expression is a binary operator) 9 { 10 create a node for the operator ; 11 pop from the stack a reference to an operand ; 12 make the operand the right subtree of the operator node ; 13 pop from the stack a reference to an operand ; 14 make the operand the left subtree of the operator node ; 15 push the reference to the operator node onto the stack ; 16 } 17 }