Infix to Postfix Conversion and Evaluation
What is an Infix Expression?
An infix expression is the way we normally write mathematical expressions:
-
The operator is between the operands.
Examples of Infix expressions:
-
A + B
-
5 * 3
5 + 3 * 2
-
(A + B) * (C - D)
-
(7 + 3) * (5 - 2)
✅ This is natural for humans because we understand precedence (multiplication first, then addition) and use parentheses.
What is a Postfix Expression?(also called Reverse Polish Notation)
A postfix expression places the operator after its operands.
-
No parentheses are needed.
-
No precedence rules needed — order of operations is built-in.
Examples of Postfix expressions:
-
A B +
→ (meansA + B
) -
5 3 *
→ (means5 * 3
) -
A B + C D - *
→ (means(A + B) * (C - D)
) -
7 3 + 5 2 - *
→ (means(7 + 3) * (5 - 2)
)
✅ Computers prefer postfix because it’s very easy to evaluate using a stack.
🧠Quick Comparison:
Feature | Infix | Postfix |
---|---|---|
Operator position | Between operands | After operands |
Need for parentheses? | Yes (sometimes) | No |
Operator precedence important? | Yes | No |
Example | A + B | A B + |
Human or Machine friendly? | Human | Machine |
Infix | Postfix |
---|
(A + B) * (C - D) / (E + F) | A B + C D - * E F + / |
(5 + 2) * (8 - 3) / (4 + 1) | 5 2 + 8 3 - * 4 1 + / |
((A + B) * (C + D)) / ((E + F) * (G + H)) | A B + C D + * E F + G H + * / |
A + B * (C ^ D - E) ^ (F + G * H) - I | A B C D ^ E - F G H * + ^ * + I - |
Algorithm for Infix to Postfix Conversion
We use a stack to hold operators and parentheses during the conversion.
Steps:
-
Initialize an empty stack for operators and an empty postfix expression.
-
Scan the infix expression from left to right.
-
For each symbol in the infix expression:
-
If the symbol is an operand (letter or number), add it to the postfix expression.
-
If the symbol is
(
, push it onto the stack. -
If the symbol is
)
:-
Pop from the stack and append to the postfix expression until a
(
is encountered. -
Pop and discard the
(
.
-
-
If the symbol is an operator (
+
,-
,*
,/
,^
):-
While the stack is not empty, and the top of the stack has higher or equal precedence than the current operator:
-
Pop from the stack and append to the postfix expression.
-
-
Push the current operator onto the stack.
-
-
-
After the entire infix expression is scanned:
-
Pop all remaining operators from the stack and append them to the postfix expression
Input | Stack | Postfix |
---|---|---|
( | ( | |
A | ( | A |
+ | ( + | A |
B | ( + | A B |
) | A B + | |
* | * | A B + |
( | * ( | A B + |
C | * ( | A B + C |
- | * ( - | A B + C |
D | * ( - | A B + C D |
) | * | A B + C D - |
(end) | A B + C D - * |
We use a stack:
Algorithm Evaluation of Postfix Expression
-
Scan the postfix expression left to right.
-
For each symbol:
-
If it's an operand (number), push it onto the stack.
-
If it's an operator:
-
Pop the top two elements from the stack.
-
Apply the operator (second popped item operator first popped item).
-
Push the result back into the stack.
-
-
-
At the end, the stack contains only one element — the final result.
Step-by-step:
Symbol | Stack | Explanation |
---|---|---|
7 | 7 | Push 7 |
8 | 7, 8 | Push 8 |
+ | 15 | 7 + 8 = 15 |
3 | 15, 3 | Push 3 |
2 | 15, 3, 2 | Push 2 |
+ | 15, 5 | 3 + 2 = 5 |
/ | 3 | 15 / 5 = 3 |
✅ Final result: 3
Example Problems:
Infix | Postfix | Result |
---|
(5 * (6 + 2)) - (12 / 4) | 5 6 2 + * 12 4 / - | 37 |
(7 + 8) / (3 + 2) | 7 8 + 3 2 + / | 3 |
-
Always push operands.
-
Always pop two operands for an operator.
-
Order matters: second popped value is first operand, first popped value is second operand.
-
Use a stack to keep things simple.
-
Operands (digits 0-9) are pushed onto the stack.
-
Operators cause two elements to be popped, the operation applied, and the result pushed back.
-
At the end, the result is at the top of the stack.
-
This code assumes only single-digit numbers.
-
For multi-digit numbers, you can modify it to read space-separated tokens.
-
Operators supported are
+
,-
,*
,/
, and^
(power).
Comments
Post a Comment