- 2071 Magh · 2+3 marks
Polish notation and postfix conversion
Short answer
Polish (prefix) places operator before operands; reverse Polish (postfix) places after, removing brackets. Postfix of (a+b*c+(d*e+f)*g) is abc*+de*f+g*+.
What is polish notation and reverse polish notation? Convert the following expression to postfix expression using Stacks: P: (a + b * c + (d * e + f) * g)
Polish and reverse Polish notation
- Polish notation (prefix): the operator is written before its operands, e.g.
+ab. It was introduced by the Polish logician Jan Łukasiewicz. - Reverse Polish notation (postfix): the operator is written after its operands, e.g.
ab+. - The usual form
a+b(operator between operands) is infix.
Both prefix and postfix need no brackets and no precedence rules, because the order of operators already fixes the order of evaluation. Example: (a+b)*c → prefix *+abc, postfix ab+c*.
Conversion of P: (a + b * c + (d * e + f) * g)
- Scan the infix expression left to right.
- Operand: copy it to the output.
(: push it.): pop operators to the output until(; discard both brackets.- Operator: pop operators of higher or equal precedence from the stack top (for
^only higher) to the output, then push the scanned operator. - At the end, pop all remaining operators to the output.
Precedence: ^ > * / > + -.
| Step | Symbol | Stack (bottom → top) | Postfix output |
|---|---|---|---|
| 1 | ( | ( | - |
| 2 | a | ( | a |
| 3 | + | (+ | a |
| 4 | b | (+ | ab |
| 5 | * | (+* | ab |
| 6 | c | (+* | abc |
| 7 | + | (+ | abc*+ |
| 8 | ( | (+( | abc*+ |
| 9 | d | (+( | abc*+d |
| 10 | * | (+(* | abc*+d |
| 11 | e | (+(* | abc*+de |
| 12 | + | (+(+ | abc*+de* |
| 13 | f | (+(+ | abc*+de*f |
| 14 | ) | (+ | abc*+de*f+ |
| 15 | * | (+* | abc*+de*f+ |
| 16 | g | (+* | abc*+de*f+g |
| 17 | ) | (empty) | abc*+de*f+g*+ |
| end | (pop all) | (empty) | abc*+de*f+g*+ |
Postfix: abc*+de*f+g*+
Check by the order of evaluation: b*c, then a+(b*c), then d*e, (d*e)+f, *g, and the final +.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Convert expression to prefix and postfix · Data Structure and Algorithm, chapter 2
- Importance of postfix; convert expression · Data Structure and Algorithm, chapter 2
- Convert expression to prefix and postfix · Data Structure and Algorithm, chapter 2
- Algorithm for infix to postfix conversion · Data Structure and Algorithm, chapter 2 · asked 3×
- Convert A+B-C*(D-E+F/G)/H to postfix · Data Structure and Algorithm, chapter 2 · asked 3×
- Evaluate postfix expression 574-*8/4+ · Data Structure and Algorithm, chapter 2