Download presentation
Presentation is loading. Please wait.
1
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about exactly what operations are performed on stacks to both evaluate RPN expressions (which you DO NOT have to do in lab - but should know about), and transform infix expressions to postfix (which you DO have to do for lab).
2
Postfix notation Given the standard infix notation: 7*5-3 The RPN expression is: 75*3- We made it like this by placing operators last (after the operands)
3
Evaluating RPN expressions RPN expressions are best evaluated using a stack. Stacks are LIFO (last in first out)
4
Evaluation example Expression: 7 5 * 3 - 32 empty top -1 7 5 7 01 pop 7*5 35 pop push 35 3 push 35-3 pop push 32 pop 010 push
5
Operator placement It makes a big difference how operators are placed: 7*5-3 = 32 RPN: 75*3- = 32 RPN: 753*- = -8 same as infix 7-(5*3) RPN: 75-3* = 6 same as infix (7-5)*3
6
Converting infix expressions To convert an infix expression to a postfix expression means building a stack of operators. The operators have priority levels highest: */, next highest: +-, lowest (
7
Converting infix to postfix Expression: 7 * 5 - 3 empty top -1 ** 00,-1 pop - check priority push - 3 pop -1,00 push 75 * -
8
Another example: 6*7*8*9-3 Expression: 6*7*8*9-3 empty top 0 ** 12 pop * check priority push * pop 210 push 6789***3- *** * * pop * * * - 0
9
Things to notice In the previous example, the subtraction operator could not be pushed on to the stack until all operators of higher priority had been popped off. THE END
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.