Sei sulla pagina 1di 3

Reverse Polish Notation

James Leong Mook Seng

Reverse Polish Notation(RPN) is needed to remove ambiguity from algebraic expressions process expression without the use of brackets process expression using stacks Mathematical operators have precedence (they have an order ). This is the hierarchy of the operators () brackets first * or / that comes first from the left hand side is done first, precedence level is same + or - that comes first from the left hand side is done first, precedence level is same An infix expression might be A * B, its corresponding postfix expression is AB*. Another example might be (A*B) + (C/D), its corresponding postfix expression is AB* + CD/ then finally AB*CD/+ RPN or postfix notation can be used to evaluate an expression. The following mathematical expression need to be evaluated: 4*3-7+8/4*3 Inserting brackets according to precedence from the L.H.S Process *, / from L.H.S (4 * 3) 7 + ((8 / 4 ) * 3 ) Process +,- from L.H.S ((4 * 3) 7) + ((8 / 4 ) * 3 ) Note: If BODMAS was used instead, Addition comes before Subtraction , expression would be (4 * 3) (7 + ((8 / 4 ) * 3 )) which is completely different. Converting this infix expression to postfix (from inner-most brackets to outer-most brackets) ((4 * 3) 7) + ((8 / 4 ) * 3 ) ((4 3 * ) 7) + ((8 4 / ) * 3 ) (4 3 * 7 ) + (8 4 / 3 * ) 43*78 4/ 3*+ Another way to derive the postfix expression is to use a binary tree using the infix expression. (from outermost operator to innermost operator). The operator is the root. Operand on the left of operator is inserted as the left child and operand on the right of operator is inserted as the right child. In the above example, - sign is the outermost operator, followed by + (left of -) and * (right of -) and so on. + * 7 / 3

4
1

Reverse Polish Notation

James Leong Mook Seng

Note: using either method, the same postfix expression will be obtained. The postfix expression is derived by traversing the binary tree using post-order method. The infix expression is derived by traversing the binary tree using in-order method. Convert the following infix expression into postfix expression (both methods) (1) 3 + 8 * 4 - 7 * 6 / 2 4 Result : 10

(2) 3 * 4 / 2 6 + 9 / 3 * 4

Result : 12

(3) 4 2 * 6 / 2 4 * 3 2

Result : -16

Use of RPN and a stack to evaluate an infix expression Infix expression is given by 6 * 2 + 3 4 * 3 / 3 7 + 5 (1) Change the infix expression into postfix one : (2) Use the following steps to make use of a stack to evaluate the postfix expression: PUSH the operands onto the stack from 62*3+43*3/-7-5+the postfix expression Whenever an operator comes then POP out the previous TWO operands and perform the operation B A If operator is + then Result is A + B and not B +A (bottom to up) PUSH the result back onto the stack

Expression is 62*3+43*3/-7-5+ 62 62* 62*3

62*3+

62*3+43
3

62*3+43*

62*3+43*3
3

2 6 Push 6,2 12 Pop 6,2 Multiply 6 *2 Push 12

3 12 Push 3 15 Pop 12, 3 Add 12 +3 Push 15

4 15 Push 4,3

12 15 Pop 4,3 Multiply 4 * 3 Push 12

12 15 Push 3

62*3+43*3/

62*3+43*3/-

62*3+43*3/-7

62*3+43*3/-7-

62*3+43*3/-7-5

62*3+43*3/-7-4+

4 15 11

7 11 4

5 4 Push 4 9 Pop 4,5 Add 4 +5 Push 9 2

Pop 12,expression 3 Pop ,4 3 4 * 3 Pop 11,7 Push 7 7 + 5 is evaluated The infix 6 * 15 2+ /3 to 9

Divide 12 /3 Subtract 15-4 Subtract 11-7 Push 4 Push 11 Push 4 question Evaluate the value of postfix expressions given in the previous

Reverse Polish Notation

James Leong Mook Seng

Use of a stack to convert a postfix expression into an infix expression The same steps are applied as in the previous example Postfix expression : gf*ab+e*/ gf gf* gf*ab
f g Push g,f (g*f) Pop g,f Multiply g * f Push (g*f) (g*f)/((a+b)*e) Pop (g*f) , ((a+b)*e) Divide (g*f) / ((a+b)*e) Push (g*f) / ((a+b)*e) b a (g*f) Push a,b (a+b) (g*f) Pop a,b Add a+ b Push (a+b) e (a+b) (g*f) Push e

gf*ab+

gf*ab+e

gf*ab+e*
((a+b)*e) (g*f) Pop (a+b) , e Multiply ((a+b) *e) Push ((a+b) *e)

gf*ab+e*/

The infix expression : (g*f) / ((a+b)*e)

Using stacks, convert the following postfix expressions into infix expressions (1) 4 a b * - d e + Answers : (4- (a * b)) / (d +e) (2) ab+de*/gf*((a+b)/(d*e)) (g*f) (3) bb*4a*c*-2a*/ ((b*b)-(4*a*c))/(2*a) (4) ab+de -*gf*hi/-/ ((a+b)*(d-e))/((g*f)-(h-i)) Evaluate the following infix expressions (1) 8 * 4 7 + 5 * 3 10 / 2 4 + 5 (2) 9 4 / 2 * 6 + 5 7 + 6 * 2 (3) 3*3 + 5 2 * 7 + 8 / 2 *4 2

Result : 36 7 14

Potrebbero piacerti anche