📐 Infix · Postfix · Prefix notation, conversion & use

📍 Infix inf
✧ standard form
A + B  ·  (C - D) * E
operator between operands
human-friendly
Example: (3 + 4) × 5 → 35
needs parentheses & precedence
📎 Postfix post
✧ reverse Polish (RPN)
A B +  ·  C D - E *
operator after operands
no parentheses needed
Example: 3 4 + 5 × → 35
evaluated left‑to‑right using stack
🔖 Prefix pre
✧ Polish notation
+ A B  ·  * - C D E
operator before operands
no ambiguity
Example: × + 3 4 5 → 35
operator precedes operands
Same expression (infix): (A + B) * C A B + C * (postfix) * + A B C (prefix)

🔄 Detailed conversion: step‑by‑step

We'll convert the infix expression: (A + B) * C - D / E (with precedence: * and / before + and -)

📌 1. Infix → Postfix (Shunting‑yard algorithm)
Rules: Scan left‑to‑right. Operands → output. Operators → stack (higher precedence pops first). Parentheses: ( push, ) pop until (.
Step Symbol Stack Output Action
1((push (
2A(Aoutput operand
3+( +Apush +
4B( +A Boutput operand
5)A B +pop until ( → output +
6**A B +push *
7C*A B + Coutput operand
8--A B + C ** pops, push -
9D-A B + C * Doutput operand
10/- /A B + C * Dpush /
11E- /A B + C * D Eoutput operand
12endA B + C * D E / -pop all → / then -
Postfix result: A B + C * D E / -
📌 2. Infix → Prefix (parse tree / pre‑order)
Method: Build expression tree, then pre‑order traversal (root → left → right).
Tree: - (root) → left * → left +A B → right C → right /D E
Step Traverse Prefix output Explanation
1root --root operator
2left *- *left subtree
3left of *+- * +left of *
4left of +A- * + Aoperand
5right of +B- * + A Boperand
6right of *C- * + A B Coperand
7right of -/- * + A B C /right subtree
8left of /D- * + A B C / Doperand
9right of /E- * + A B C / D Eoperand
Prefix result: - * + A B C / D E
📌 3. Postfix ↔ Prefix (stack reversal)
Method: Reverse postfix, scan, and combine using a stack.
Postfix: A B + C * D E / -  →  Reverse: - / E D * C + B A
Step Symbol Stack (prefix) Action
1--push operator
2/- /push operator
3E- / Epush operand
4D- / E Dpush operand
5*pop D,E → / D E, push *combine
6Cpop / D E and C* / D E Ccombine
7+pop * / D E C and B+ B * / D E Ccombine
8B(already combined)push operand
9Apop + B * / D E C and A- A + B * / D E Cfinal combine
Prefix result: - * + A B C / D E
🧩 Summary for (A + B) * C - D / E: Infix: (A + B) * C - D / E Postfix: A B + C * D E / - Prefix: - * + A B C / D E

⚙️ What is it used for?

🔄 conversion: Infix → Postfix (Shunting‑yard) & Infix → Prefix (parse tree). Postfix & Prefix are operator‑precedence free — ideal for computer evaluation.
🧠 Why does it matter? Postfix & Prefix eliminate the need for parentheses and precedence rules — they are directly evaluable with a stack. Compilers often convert infix expressions to postfix or prefix during code generation. Also used in expression trees, reverse‑Polish calculators, and functional programming (prefix).
📌 quick comparison infix (A+B)×C → needs precedence postfix AB+C× → stack eval prefix ×+ABC → recursive eval