Download Analog and digital C - Expression tree and more Study notes Digital & Analog Electronics in PDF only on Docsity!
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.
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:
• 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.
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
b c
Prefix, Infix, and Postfix Forms (contd.)
Expression Postfix forms Infix forms Postfix forms
(a + b) + a b a + b a b +
a - (b * c) - a * b c a - b * c a b c * -
log (x) log x log x x log
n!! n n! n!
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:
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
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:
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
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-
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