Download presentation
Presentation is loading. Please wait.
Published byAbner Wood Modified over 9 years ago
1
1 EXPRESSION TREES In this lecture we will discuss Expression trees as a method for storing & evaluating mathematical expressions
2
2 zResources: zBarrons Chapter 10 p.341 plus M/C #s 9, 10, 11 zAP Java Text Chapter 19 p.822-826 zHandouts: z1.ExpressionTree.java
3
3 zExpression tree: yAlgebraic Expression with parentheses and a defined precedence of operations yA nested structure that can be represented by a Binary (expression) Tree
4
4 zExample: Operator + - * / / \ 1 st 2 nd operand operand za + b can be represented as:+ / \ a b
5
5 zEach node of the Expression tree contains a “TOKEN” which is either an operator (+ - * /) or an operand ( some #) zThe operand can be a variable or a number
6
6 zExample: (a + 1) (bc + 5 – c) can be represented as:
7
7 zExample: * / \ + - / \ / \ a 1 + c / \ * 5 / \ b c
8
8 zOperators are represented by nodes with children zOperands are represented by leaves
9
9 zEvaluating Expressions Represented by Trees: xThe evaluation of an expression tree includes calculating the value of an expression when given values for the operands’ variables. xRecursion is the easiest way to implement an evaluation function for expression trees.
10
10 zHere is the process: If a node is an operand, we fetch its value If a node is an operator, we apply that operator to the results of an evaluation of its left and right subtrees
11
11 * ---- operator / \ b c ----- operands xCODE EXAMPLE “ExpressionTree.java”: Walk thru the code’s evaluate function *** Assume operands & operators are represented by character strings in tree nodes ***
12
12 String value = (String) node.getValue (); double leftVal, rightVal; if (value.equals ("+")) { leftVal = evaluate (node.getLeft ()); rightVal = evaluate(node.getRight ()); return leftValue + rightValue; } else return Double.parseDouble (value);
13
13 // IN SPVM TreeNode exprTree; // Convert the expression into a tree. exprTree = (TreeNode) TreeUtil.createExpressionTree (expr); result = evaluate (exprTree); System.out.println ("Result = " + result);
14
14 (a + 1) (bc + 5 – c) * / \ + - / \ / \ a 1 + c / \ * 5 / \ b c
15
15 PREFIX, POSTFIX & INFIX Notations zINFIX Notation: yAn inorder traversal of an expression tree printed out fully parenthesized yConventional algebraic notation where an operator is placed BETWEEN the operands x + y yRequires recursive evaluation of subtrees
16
16 zINFIX Notation: Infix Notation: ( a + 1 ) *((( b * c ) + 5 ) - c)
17
17 zPrefix and Postfix: zConvenient for evaluating expressions: they do not use parenthesis do not need to take into account order of precedence order of operations can be reconstructed from the expression
18
18 zPrefix and Postfix: yThese notations for an algebraic expression can be generated by traversing the expression in preorder (for prefix) and postorder (for postfix)
19
19 zPREFIX Notation: (N L R) yPlace the operator BEFORE the operands +xy zHowever, there is no need for parenthesis as there is only 1 way to correctly place them zPrefix notation is also called Polish Notation Prefix Notation: * + a 1 - + * b c 5 c
20
20 zPOSTFIX Notation: (L R N) zPlace the operator AFTER the operands xy+ zAgain, there is no need for parenthesis as there is only 1 way to correctly place them zPostfix notation is also called Reverse Polish Notation Postfix Notation: a 1 + b c * 5 + c - *
21
21 Evaluation of an Expression using Prefix and Postfix zPrefix & Postfix allow us to evaluate an algebraic expression in a single, sequential swipe
22
22 zPOSTFIX Notation can be evaluated: yProceed from LEFT to RIGHT using a temporary STACK for holding unused operands and intermediate results
23
23 zPOSTFIX Notation can be evaluated... yAlgorithm: going from LEFT to RIGHT consider the next token if it is an operand (#) push its value on the stack if it is an operator: pop the second operand pop the first operand perform the operation push the result on the stack zwe will be left with one value on the stack ---- the result of the evaluation
24
24 zPOSTFIX Notation can be evaluated... yEXAMPLE: a 1 + b c * 5 + c - * Lets Draw the Stack’s evolution showing the state of the stack AFTER each token is encountered
25
25 zPREFIX Notation can be evaluated... yProceed from RIGHT to LEFT using a temporary STACK for holding unused operands and intermediate results
26
26 zPREFIX Notation can be evaluated... xAlgorithm: going from RIGHT to LEFT consider the next token if it is an operand (#) push its value on the stack if it is an operator: pop the first operand pop the Second operand perform the operation push the result on the stack zwe will be left with one value on the stack ---- the result of the evaluation
27
27 zNOTE:the operands appear in the same order in infix, prefix and postfix notations. Only the position of the operators is different. Infix Notation: ( a + 1 ) * ((( b * c ) + 5 ) - c) Prefix Notation: * + a 1 - + * b c 5 c Postfix Notation: a 1 + b c * 5 + c - *
28
28 Build an expression tree from a postfix or prefix notation zInstead of numbers, we push references to nodes on the stack, instead of performing an operation, we link the operator node to the nodes of the operands.
29
29 yAlgorthim: going from LEFT to RIGHT consider the next token Create a new node for the token If it is an operand: Set the LEFT and RIGHT children to null Push the reference to the new node on the stack yIf it is an operator: Pop a reference from a stack Set the new node’s RIGHT child to it Pop another reference from the stack Set the new node’s LEFT child to it Push the reference to the new node on the stack
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.