Infix to prefix conversion example – Infix to prefix conversion using stack – DSA Course Playlist

✅ 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):

  1. Reverse the infix expression
    • Swap every ( with ) and vice versa.
  2. Convert the reversed infix to postfix
    • (Use the same rules we learned earlier)
  3. 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 → *
  • ( → push
  • B → output → C B
  • + → push
  • A → 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:

StepAction
1Reverse the infix expression
2Convert to postfix
3Reverse the postfix to get prefix

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top