Logo

Welcome to:
Infix -> Prefix / Postfix
Prefix -> Infix / Prefix Converter
Postfix -> Infix / Prefix Converter
👋🏻

Select Expression Type:

Begin by entering an infix expression below ✨



Infix to Postfix / Prefix Rules

📝 Rules:

Infix to Postfix & Prefix Conversion Rules

Infix to Postfix Conversion

  • Initialize: Create an empty stack for operators and an empty list for the output.
  • Read the expression: From left to right, read one token (operand, operator, or parenthesis) at a time.
  • Operand: If the token is an operand (number or variable), append it to the output list.
  • Operator:
    • While there is an operator at the top of the stack with greater precedence, pop operators from the stack to the output list.
    • Push the read operator onto the stack.
  • Parentheses:
    • If the token is a left parenthesis `(`, push it onto the stack.
    • If the token is a right parenthesis `)`, pop from the stack to the output list until a left parenthesis `(` is encountered. Discard the left parenthesis.
  • End of Expression: After reading the entire expression, pop any remaining operators from the stack to the output list.
  • Output: The output list now contains the postfix expression.

Infix to Prefix Conversion

  • Reverse the expression: Reverse the input infix expression.
  • Swap parentheses: Swap the left and right parentheses.
  • Apply Postfix Algorithm: Apply the infix to postfix conversion rules to the reversed expression.
  • Reverse the result: Finally, reverse the output to get the prefix expression.

Precedence and Associativity

  • Precedence: Operators with higher precedence are evaluated before operators with lower precedence. For example, `*` and `/` have higher precedence than `+` and `-`.
  • Associativity:
    • Left associative operators: `+`, `-`, `*`, `/`, `%`
    • Right associative operators: `^`

Example Conversion

Infix to Postfix

Infix: A * (B + C) / D

  1. Read `A`: Operand, add to output: A
  2. Read `*`: Operator, push to stack: *
  3. Read `(`: Parenthesis, push to stack: * (
  4. Read `B`: Operand, add to output: A B
  5. Read `+`: Operator, push to stack: * ( +
  6. Read `C`: Operand, add to output: A B C
  7. Read `)`: Parenthesis, pop and add to output until `(` is found: A B C +
  8. Read `/`: Operator, pop `*` and add to output, then push `/` to stack: A B C + * /
  9. Read `D`: Operand, add to output: A B C + * D /

Postfix: A B C + * D /

Infix to Prefix

Infix: A * (B + C) / D

  1. Reverse: D / (C + B) * A
  2. Swap parentheses: D / )C + B( * A
  3. Convert to postfix:
    • Read `D`: Operand, add to output: D
    • Read `/`: Operator, push to stack: /
    • Read `)` (left parenthesis): Push to stack: / )
    • Read `C`: Operand, add to output: D C
    • Read `+`: Operator, push to stack: / ) +
    • Read `B`: Operand, add to output: D C B
    • Read `(` (right parenthesis): Pop and add to output until `)` is found: D C B +
    • Read `*`: Operator, push to stack: * /
    • Read `A`: Operand, add to output: D C B + A
  4. Reverse the result: * A / + B C D

Prefix: * A / + B C D


Prefix to Infix / Postfix Rules

📝 Rules:

Prefix to Infix & Postfix Conversion Rules

Prefix to Infix Conversion

  • Read the Prefix expression from right to left.
  • If the character is an operand, push it onto the stack.
  • If the character is an operator, pop two operands from the stack.
  • Form a string by concatenating the two operands and the operator between them.
  • Push the resulting string back onto the stack.
  • Repeat until the entire Prefix expression has been read.
  • The stack now contains the Infix expression.

Prefix to Postfix Conversion

  • Read the Prefix expression from right to left.
  • If the character is an operand, push it onto the stack.
  • If the character is an operator, pop two operands from the stack.
  • Concatenate the two operands and the operator in the order: operand1 operand2 operator.
  • Push the resulting string back onto the stack.
  • Repeat until the entire Prefix expression has been read.
  • The stack now contains the Postfix expression.

Example Conversion

Prefix to Infix

Prefix: * + A B - C D

  1. Read `D`: Operand, push to stack: D
  2. Read `C`: Operand, push to stack: C D
  3. Read `-`: Operator, pop two operands (`C` and `D`), form string: (C - D), push to stack
  4. Read `B`: Operand, push to stack: B (C - D)
  5. Read `A`: Operand, push to stack: A B (C - D)
  6. Read `+`: Operator, pop two operands (`A` and `B`), form string: (A + B), push to stack
  7. Read `*`: Operator, pop two operands (`(A + B)` and `(C - D)`), form string: ((A + B) * (C - D)), push to stack

Infix: (A + B) * (C - D)

Prefix to Postfix

Prefix: * + A B - C D

  1. Read `D`: Operand, push to stack: D
  2. Read `C`: Operand, push to stack: C D
  3. Read `-`: Operator, pop two operands (`C` and `D`), form string: CD-, push to stack
  4. Read `B`: Operand, push to stack: B CD-
  5. Read `A`: Operand, push to stack: A B CD-
  6. Read `+`: Operator, pop two operands (`A` and `B`), form string: AB+, push to stack
  7. Read `*`: Operator, pop two operands (`AB+` and `CD-`), form string: AB+CD-* , push to stack

Postfix: AB+CD-*


Postfix to Infix / Prefix Rules

📝 Rules:

Postfix to Infix & Prefix Conversion Rules

Postfix to Infix Conversion

  • Read the Postfix expression from left to right.
  • If the character is an operand, push it onto the stack.
  • If the character is an operator, pop two operands from the stack.
  • Form a string by concatenating the two operands and the operator between them.
  • Push the resulting string back onto the stack.
  • Repeat until the entire Postfix expression has been read.
  • The stack now contains the Infix expression.

Postfix to Prefix Conversion

  • Read the Postfix expression from left to right.
  • If the character is an operand, push it onto the stack.
  • If the character is an operator, pop two operands from the stack.
  • Concatenate the operator and the two operands in the order: operator operand1 operand2.
  • Push the resulting string back onto the stack.
  • Repeat until the entire Postfix expression has been read.
  • The stack now contains the Prefix expression.

Example Conversion

Postfix to Infix

Postfix: AB+C*

  1. Read `A`: Operand, push to stack: A
  2. Read `B`: Operand, push to stack: A B
  3. Read `+`: Operator, pop two operands (`A` and `B`), form string: (A + B), push to stack
  4. Read `C`: Operand, push to stack: (A + B) C
  5. Read `*`: Operator, pop two operands (`(A + B)` and `C`), form string: ((A + B) * C), push to stack

Infix: (A + B) * C

Postfix to Prefix

Postfix: AB+C*

  1. Read `A`: Operand, push to stack: A
  2. Read `B`: Operand, push to stack: A B
  3. Read `+`: Operator, pop two operands (`A` and `B`), form string: +AB, push to stack
  4. Read `C`: Operand, push to stack: +AB C
  5. Read `*`: Operator, pop two operands (`+AB` and `C`), form string: *+ABC, push to stack

Prefix: *+ABC



📝 Fibonnaci Series:




Frequently Asked Questions

Frequently Asked Questions

An infix expression is an expression that we use every day. An infix expression consists of a single letter or an operator followed by one infix string and another infix string. For example, A, A + B, (A + B) + (C – D). As a result, we have operators between operands.