Download presentation
Presentation is loading. Please wait.
Published byAntoine Askins Modified over 9 years ago
1
Expression Trees What is an Expression tree? Expression tree implementation Why expression trees? Evaluating an expression tree (pseudo code) Prefix, Infix, and Postfix forms Infix to Postfix conversion Constructing an expression tree from a postfix expression
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.
3
Expression Tree Examples Inorder Traversal ResultExpression TreeExpression a + 3(a+3) 3+4*5-9+63+(4*5-(9+6)) log xlog(x) n ! + 3a + -3 * 54 + 69 log x ! n
4
Expression tree implementation The class hierarchy of Expression trees is: Comparable Container Tree AbstractObject AbstractContainer AbstractTree BinaryTree ExpressionTree
5
Expression tree implementation (contd.) 1 public class ExpressionTree extends BinaryTree{ 2 public ExpressionTree(Object obj, ExpressionTree left, ExpressionTree right){ 3 super(obj, left, right) ; 4 } 5 public ExpressionTree(){ super(null, null, null) ; } 6 7 public ExpressionTree(Object obj){ 8 this(obj, new ExpressionTree(), new ExpressionTree()) ; 9 } 10 11 public void attachTreeToLeft(ExpressionTree tree){ 12 left = tree ; 13 } 14 15 public void attachTreeToRight(ExpressionTree tree){ 16 right = tree ; } 17 //... 18 }
6
Why Expression trees? Expression trees are used to remove ambiguity in expressions. Consider the algebraic expression 2 - 3 * 4 + 5. 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.
8
Evaluating an Expression tree Assuming that t is a valid expression tree, a pseudo code algorithm for evaluating the expression tree is: 1 evaluate(ExpressionTree t){ 2 if(t is a leaf) 3 return value of t's operand ; 4 else{ 5 operator = t.element ; 6 operand1 = evaluate(t.left) ; 7 operand2 = evaluate(t.right) ; 8 return(applyOperator(operand1, operator, operand2) ; 9 } 10} + *2 3- 15 Order of evaluation: 3 1 2 (2 + ((5 – 1) * 3))
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
10
Prefix, Infix, and Postfix Forms (contd.) Postfix formsInfix formsPostfix formsExpression a b +a + b+ a b(a + b) a b c * -a - b * c- a * b ca - (b * c) x loglog x log (x) n ! ! nn !
11
Prefix, Infix, and Postfix Forms (contd.) A prefix or postfix form corresponds to exactly one expression tree. An infix form may correspond to more than one expression tree. It is therefore not suitable for expression evaluation. Infix formExpression TreeExpression 2 - 3 * 4 + 5 (2 - 3) * (4 + 5) 2 - 3 * 4 + 5 2 - (3 * 4 + 5) - +2 5* 43 - +5 2* 34 * +- 3254
12
Infix to Postfix conversion (manual) An Infix to Postfix manual conversion algorithm is: 1. Completely parenthesize the infix expression according to order of priority you want. 2. Move each operator to its corresponding right parenthesis. 3. Remove all parentheses. Examples: 3 + 4 * 5(3 + (4 * 5) )3 4 5 * + a / b ^ c – d * e – a * c ^ 3 ^ 4 a b c ^ / d e * a c 3 4 ^ ^ * - - ((a / (b ^ c)) – ((d * e) – (a * (c ^ (3 ^ 4) ) ) ) ) Using normal mathematical operator precedence Not using normal mathematical operator precedence
13
Infix to Prefix conversion (manual) An Infix to Postfix manual conversion algorithm is: 1 Completely parenthesize the infix expression according to order of priority you want. 2 Move each operator to its corresponding left parenthesis. 3Remove all parentheses. Examples: 3 + 4 * 5(3 + (4 * 5) )3 4 5 * + a / b ^ c – d * e – a * c ^ 3 ^ 4 a b c ^ / d e * a c 3 4 ^ ^ * - - ( (a / (b ^ c)) – ( (d * e) – (a * (c ^ (3 ^ 4) ) ) ) ) Using normal mathematical operator precedence Not using normal mathematical operator precedence
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 }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.