Infix expressions, like A + B
, have operators between operands, making them easy for humans to understand but challenging for machines to evaluate. Postfix notation eliminates the need for parentheses by placing operators after their operands, simplifying computation. In this class we will see how to convert infix to postfix with various examples.
Rules for Infix to Postfix Conversion
- Operands: Directly append operands (like
A
,B
, or1
) to the output. - Operators: Use a stack to manage operators based on their precedence and associativity.
- Precedence:
^
(highest),*
and/
(medium),+
and-
(lowest). - Associativity: Most operators are left-to-right associative, except
^
, which is right-to-left.
- Precedence:
- Parentheses:
- Push opening parentheses
(
onto the stack. - Pop from the stack until you find a matching
)
when encountering a closing parenthesis.
- Push opening parentheses
Step-by-Step Examples of Infix to Postfix Conversion
Example 01: 2 + 1
- Operand
2
→ Add to output:Output: 2
- Operator
+
→ Push to stack:Stack: +
- Operand
1
→ Add to output:Output: 2 1
- End of expression → Pop stack:
Output: 2 1 +
Postfix Result: 2 1 +
Example 02: 3 + 2 + 1
- Operand
3
→ Add to output:Output: 3
- Operator
+
→ Push to stack:Stack: +
- Operand
2
→ Add to output:Output: 3 2
- Operator
+
→ Push+ to stack
:Stack: ++
- Operand
1
→ Add to output:Output: 3 21 ++
- End of expression → Pop stack:
Output: 3 21++
Postfix Result: 3 21++
Example 03: 4 - 3 + 2 + 1
- Operand
4
→ Add to output:Output: 4
- Operator
-
→ Push to stack:Stack: -
- Operand
3
→ Add to output:Output: 4 3
- Operator
+
→ Pop-
from stack (same precedence):Output: 4 3 -
→ Push+
:Stack: +
- Operand
2
→ Add to output:Output: 4 3 - 2
- Operator
+
→Output: 4 3 - 2
→ Push+
:Stack: ++
- Operand
1
→ Add to output:Output: 4 3 - 2 1
- End of expression → Pop stack:
Output: 4 3 - 2 1+ +
Postfix Result: 4 3 - 2 1++
Example 04: 1 - (2 + 3) * 4
- Operand
1
→ Add to output:Output: 1
- Operator
-
→ Push to stack:Stack: -
(
→ Push to stack:Stack: - (
- Operand
2
→ Add to output:Output: 1 2
- Operator
+
→ Push to stack:Stack: - ( +
- Operand
3
→ Add to output:Output: 1 2 3
)
→ Pop stack until(
:Output: 1 2 3 +
→ Remove(
:Stack: -
- Operator
*
→ Push to stack:Stack: - *
- Operand
4
→ Add to output:Output: 1 2 3 + 4
- End of expression → Pop stack:
Output: 1 2 3 + 4 * -
Postfix Result: 1 2 3 + 4 * -
Example 05: (5 / 6) - (4 * (3 + 2))
(
→ Push to stack:Stack: (
- Operand
5
→ Add to output:Output: 5
- Operator
/
→ Push to stack:Stack: ( /
- Operand
6
→ Add to output:Output: 5 6
)
→ Pop stack until(
:Output: 5 6 /
→ Remove(
:Stack:
- Operator
-
→ Push to stack:Stack: -
(
→ Push to stack:Stack: - (
- Operand
4
→ Add to output:Output: 5 6 / 4
- Operator
*
→ Push to stack:Stack: - ( *
(
→ Push to stack:Stack: - ( * (
- Operand
3
→ Add to output:Output: 5 6 / 4 3
- Operator
+
→ Push to stack:Stack: - ( * ( +
- Operand
2
→ Add to output:Output: 5 6 / 4 3 2
)
→ Pop stack until(
:Output: 5 6 / 4 3 2 +
→ Remove(
:Stack: - ( *
)
→ Pop stack until(
:Output: 5 6 / 4 3 2 + *
→ Remove(
:Stack: -
- End of expression → Pop stack:
Output: 5 6 / 4 3 2 + * -
Postfix Result: 5 6 / 4 3 2 + * -
Benefits of Learning Infix to Postfix Conversion
- Simplifies Expression Evaluation: Postfix expressions can be evaluated using a simple stack algorithm.
- Optimized for Machines: No need to process parentheses or operator precedence.