0% found this document useful (0 votes)
254 views

Expression Tree

Expression trees represent mathematical expressions in a hierarchical structure to avoid ambiguity. They can be constructed from postfix or prefix notation and traversed to evaluate the expression or convert between notations. Key points are: 1) Expression trees impose order of operations without parentheses and allow unambiguous evaluation. 2) Postfix notation is produced by a postorder traversal, prefix by preorder, and infix by inorder. 3) Algorithms can convert between infix and postfix notations and build trees from postfix or prefix forms.

Uploaded by

Gaurav Doshi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
254 views

Expression Tree

Expression trees represent mathematical expressions in a hierarchical structure to avoid ambiguity. They can be constructed from postfix or prefix notation and traversed to evaluate the expression or convert between notations. Key points are: 1) Expression trees impose order of operations without parentheses and allow unambiguous evaluation. 2) Postfix notation is produced by a postorder traversal, prefix by preorder, and infix by inorder. 3) Algorithms can convert between infix and postfix notations and build trees from postfix or prefix forms.

Uploaded by

Gaurav Doshi
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

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


Constructing an expression tree from a prefix expression

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.

Expression Tree Examples


Expression (a+3) Expression Tree + a 3 a+3 Inorder Traversal Result

+
3+(4*5-(9+6)) 3 3+4*5-9+6 + 5 log 9 6

*
4

log(x)
x ! n! n

log x

n!

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.

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.

Evaluating an Expression tree


Assuming that t is a valid expression tree, a pseudo code algorithm for evaluating the expression tree is:
1 2 3 4 5 6 7 8 9 10 evaluate(ExpressionTree t){ if(t is a leaf) return value of t's operand ; else{ operator = t.element ; operand1 = evaluate(t.left) ; operand2 = evaluate(t.right) ; return(applyOperator(operand1, operator, operand2) ; } }

+ 2 Order of evaluation: 3 1 2 (2 + ((5 1) * 3))

* 3

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 b

* d c

Prefix, Infix, and Postfix Forms (contd.)


Expression (a + b) Postfix forms +ab Infix forms a+b Postfix forms ab+

a - (b * c)

-a*bc

a-b*c

abc*-

log (x)

log x

log x

x log

n!

!n

n!

n!

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.
Expression Expression Tree 2-3*4+5 Infix form

+
2 3 * *

5
4 +

2-3*4+5

(2 - 3) * (4 + 5)

2-3*4+5

2
2

3
-

4
+ * 5 4

5
2-3*4+5

2 - (3 * 4 + 5)

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) ) 345*+
Using normal mathematical operator precedence

a/b^cd*ea*c^3^4
Not using normal mathematical operator precedence

abc^/de*ac34^^*--

((a / (b ^ c)) ((d * e) (a * (c ^ (3 ^ 4) ) ) ) )

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) ) 345*+
Using normal mathematical operator precedence

a/b^cd*ea*c^3^4
Not using normal mathematical operator precedence

abc^/de*ac34^^*--

( (a / (b ^ c)) ( (d * e) (a * (c ^ (3 ^ 4) ) ) ) )

Infix to Postfix conversion (pseudo code)


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 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 ; else if(current token is a left parenthesis) 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 ; pop and discard the left parenthesis ; } else if(current token is an operator) { while(stack is not empty AND top of stack is not a left parenthesis AND top of stack is not a left-associative operator that has lower precedence than the current token AND top of stack is not a right-associative operator that has the same precedence as the current token) pop operators from the stack and enqueue them in postFixQueue ; push the current token onto the stack ; } } while(stack is not empty) { pop token from stack ; enqueue token in postFixQueue ; }

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:

1 while(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 }

Constructing an expression tree from a prefix expression


The pseudo code algorithm to convert a valid prefix expression, containing binary operators, to an expression tree:

INFIX EXPRESSION (1+3)*(6-4) PREFIX EXPRESSION *+13-64 Read the next arithmetic operator or numeric value. Create a node containing the operator or numeric value. If the node contains an operator Recursively build the sub trees corresponding to the operators operand Else The node is a leaf node

You might also like