Topic Wise Lesson Plane
Application of Stacks-Infix-to-Postfix Transformation
Infix to Postfix Transformation
1. Expression Notations
-
Infix: Operator is between operands
Example:A + B -
Postfix (Reverse Polish): Operator comes after operands
Example:AB+
2. Operator Precedence
| Operator | Precedence |
|---|---|
^ |
Highest |
* / |
Medium |
+ - |
Lowest |
3. Rules for Infix → Postfix Conversion
-
If the symbol is an operand, add it to the postfix expression.
-
If the symbol is
(, push it onto the stack. -
If the symbol is
), pop from the stack and add to postfix until(is removed. -
If the symbol is an operator:
-
Pop operators from the stack with higher or equal precedence
-
Then push the current operator onto the stack.
-
-
After scanning the entire infix expression, pop all remaining operators from the stack and add to postfix.
4. Algorithm
InfixToPostfix(expression):
create empty stack
create empty postfix string
for each symbol in expression:
if symbol is operand:
add to postfix
else if symbol == '(':
push onto stack
else if symbol == ')':
pop from stack and add to postfix until '(' is popped
else if symbol is operator:
while stack not empty and
precedence(top) >= precedence(symbol):
pop and add to postfix
push symbol onto stack
while stack not empty:
pop and add to postfix
return postfix
5. Example Conversion
Infix Expression:
A + B * C
Step-by-Step:
| Symbol | Stack | Postfix |
|---|---|---|
| A | — | A |
| + | + | A |
| B | + | AB |
| * | + * | AB |
| C | + * | ABC |
| End | — | ABC*+ |
Postfix Expression:
ABC*+
6. Another Example (with parentheses)
Infix:
(A + B) * C
Postfix:
AB+C*
7. Advantages of Using Stack
-
Handles operator precedence
-
Eliminates parentheses
-
Efficient and easy to implement
-
Widely used in expression evaluation