✅ Step-by-Step: Infix to Prefix Conversion
📘 Goal: Convert an expression like (A + B) * C
into * + A B C
🔧 Rules (Similar to Postfix, but slightly different):
- Reverse the infix expression
- Swap every
(
with)
and vice versa.
- Swap every
- Convert the reversed infix to postfix
- (Use the same rules we learned earlier)
- Reverse the resulting postfix
- That gives the prefix expression!
💡 Example: Infix → Prefix
Infix Expression:(A + B) * C
🔹 Step 1: Reverse the Infix Expression
Original: (A + B) * C
Reversed: C * (B + A)
(Swapped (
with )
and reversed order)
🔷 Step 2: Convert Reversed Infix to Postfix
Now convert C * ( B + A )
to postfix:
C
→ output →C
*
→ push →*
(
→ pushB
→ output →C B
+
→ pushA
→ output →C B A
)
→ pop till(
→C B A +
*
remains →C B A + *
🔹 Step 3: Reverse the Postfix → Prefix
Postfix: C B A + *
Reverse: * + A B C
✅ Final Answer:
Prefix Expression: * + A B C
📌 Summary of Steps:
Step | Action |
---|---|
1 | Reverse the infix expression |
2 | Convert to postfix |
3 | Reverse the postfix to get prefix |