Infix to Postfix conversion rules using stack | Infix to Postfix conversion using stack | Step by Step | Example | DSA Course Playlist

βœ… Step-by-Step: Infix to Postfix Conversion

Rules:

  1. Operands (A-Z or numbers): Go directly to the output.
  2. Left Parenthesis (: Push to stack.
  3. Right Parenthesis ): Pop from stack to output until ( is found.
    • When you see a right parenthesis ) in the infix expression, you do the following:
      • Start popping operators from the stack.
      • Add each popped operator to the output.
      • Stop popping when you find a left parenthesis (.
      • Do NOT add the ( or ) to the output. Just discard both.
  4. Operators (+, -,* , /, ^):
    • Pop from the stack to output while the top of the stack has equal or higher precedence.
    • Then push the current operator.
      • When you see an operator (like +, -, *, /, or ^) in the infix expression:
      • Look at the top of the stack.
      • If the top of the stack has an operator with higher or equal precedence, pop it from the stack and add it to the output.
      • Repeat step 2 until:
        • The stack is empty,
        • Or the top of the stack is a left parenthesis (,
        • Or the top of the stack has lower precedence than the current operator.
      • Push the current operator to the stack.

πŸ”‘ Parentheses Are Not Operators

Even though parentheses control precedence, they are not operators, and so they are not part of the precedence comparison logic used for popping operators from the stack.

They are used only as markers to say:

β€œDon’t pop past this point!”

Operator Precedence (High to Low):

  1. ^ (Right associative)
  2. * and / (Left associative)
  3. + and - (Left associative)

πŸ”Ί Operator Precedence Table (High ➑ Low)

Precedence LevelOperatorsAssociativityNotes
1 (Highest)^Right-to-leftExponentiation
2*, /Left-to-rightMultiplication and Division
3 (Lowest)+, -Left-to-rightAddition and Subtraction

βœ… What is Associativity?

Associativity tells us the direction in which operators of the same precedence level are evaluated when they appear next to each other.

πŸ” Two Types of Associativity:

TypeMeaning
Left-to-rightEvaluate from left to right
Right-to-leftEvaluate from right to left

🧠 Why It Matters

Associativity only matters when you have two or more operators of the same precedence level next to each other.

πŸ” Examples:

1. Left-to-right associativity (for +, -, *, /):

  • Subtraction (-) is left-to-right, so:
    • 100 - 50 = 50, then 50 - 10 = 40

2. Right-to-left associativity (for ^):

  • Exponentiation is right-to-left, so:
    • First 3 ^ 2 = 9
    • Then 2 ^ 9 = 512

If you did it left-to-right (2 ^ 3 = 8, then 8 ^ 2 = 64) β€” that would be wrong.

πŸ’‘ Example: Infix β†’ Postfix

Infix: (A + B) * C
Steps:

  1. ( β†’ push
  2. A β†’ output β†’ A
  3. + β†’ push
  4. B β†’ output β†’ A B
  5. ) β†’ pop till ( β†’ A B +
  6. * β†’ push
  7. C β†’ output β†’ A B + C
  8. End β†’ pop * β†’ A B + C *

Postfix: A B + C *

πŸ“ Practice Problems

πŸ” Convert Infix to Postfix:

  1. (A + B) * (C - D)
  2. A + B * C
  3. A * (B + C) / D
  4. (A + B) * (C + D)
  5. A + B + C + D

βœ… Evaluate Postfix Expression (Assume A=2, B=3, C=4, D=5):

  1. A B + C *
  2. A B C * +
  3. A B + C D - *
  4. A B + C + D +

🧠 Challenge: Identify Expression Type

Label the following as Infix, Postfix, or Prefix:

  1. A B + C *
  2. + A B
  3. (A + B) * C
  4. * + A B C
  5. A B C * +

Answers:

πŸ“ Convert Infix to Postfix

Infix ExpressionPostfix Expression
(A + B) * (C - D)A B + C D - *
A + B * CA B C * +
A * (B + C) / DA B C + * D /
(A + B) * (C + D)A B + C D + *
A + B + C + DA B + C + D +

βœ… Evaluate Postfix Expression

Given: A = 2, B = 3, C = 4, D = 5

Postfix ExpressionStep-by-Step EvaluationResult
A B + C *(2 + 3) * 4 = 5 * 420
A B C * +3 * 4 = 12, then 2 + 1214
A B + C D - *(2 + 3) = 5, (4 - 5) = -1, 5 * -1-5
A B + C + D +2 + 3 = 5, 5 + 4 = 9, 9 + 5 = 1414

🧠 Identify Expression Type

ExpressionTypeReason
A B + C *PostfixOperators come after operands
+ A BPrefixOperators come before operands
(A + B) * CInfixOperators are between operands
* + A B CPrefixFirst operator is at start, all in prefix order
A B C * +PostfixOperands first, operators at the end

Leave a Comment

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

Scroll to Top