1 / 15

Expression Trees

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. What is an Expression tree?.

merrill
Download Presentation

Expression Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. + + ! log a 3 3 - x n * + 4 5 9 6 Expression Tree Examples

  4. Expression tree implementation • The class hierarchy of Expression trees is: Comparable AbstractObject Container AbstractContainer Tree 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. + 2 * - 3 5 1 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){ • if(t is a leaf) • 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 } • } Order of evaluation: 3 1 2 (2 + ((5 – 1) * 3))

  9. + a * - d b c 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 * +

  10. Prefix, Infix, and Postfix Forms (contd.)

  11. * - + - - 2 3 4 5 5 2 + + * * 2 5 4 3 4 3 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.

  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. 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

  14. Infix to Postfix conversion (pseudo code) 1 Initialize stack to empty ; Initialize postFixQueue to empty ; • while(infix expression has more tokens) • { // must be a valid infix expression • get current token ; • if(current token is an operand) • enqueue it in postFixQueue ; 7 else if(current token is a left parenthesis) 8 push it onto stack ; • else if(current token is a right parenthesis) • { • pop operators from the stack and enqueue them in postFixQueue until the top • 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: • while(not the end of the expression) • { 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 }

More Related