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
- Read `A`: Operand, add to output:
A
- Read `*`: Operator, push to stack:
*
- Read `(`: Parenthesis, push to stack:
* (
- Read `B`: Operand, add to output:
A B
- Read `+`: Operator, push to stack:
* ( +
- Read `C`: Operand, add to output:
A B C
- Read `)`: Parenthesis, pop and add to output until `(` is found:
A B C +
- Read `/`: Operator, pop `*` and add to output, then push `/` to stack:
A B C + * /
- Read `D`: Operand, add to output:
A B C + * D /
Postfix: A B C + * D /
Infix to Prefix
Infix: A * (B + C) / D
- Reverse:
D / (C + B) * A
- Swap parentheses:
D / )C + B( * A
- 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
- Read `D`: Operand, add to output:
- 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
- Read `D`: Operand, push to stack:
D
- Read `C`: Operand, push to stack:
C D
- Read `-`: Operator, pop two operands (`C` and `D`), form string:
(C - D)
, push to stack - Read `B`: Operand, push to stack:
B (C - D)
- Read `A`: Operand, push to stack:
A B (C - D)
- Read `+`: Operator, pop two operands (`A` and `B`), form string:
(A + B)
, push to stack - 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
- Read `D`: Operand, push to stack:
D
- Read `C`: Operand, push to stack:
C D
- Read `-`: Operator, pop two operands (`C` and `D`), form string:
CD-
, push to stack - Read `B`: Operand, push to stack:
B CD-
- Read `A`: Operand, push to stack:
A B CD-
- Read `+`: Operator, pop two operands (`A` and `B`), form string:
AB+
, push to stack - 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*
- Read `A`: Operand, push to stack:
A
- Read `B`: Operand, push to stack:
A B
- Read `+`: Operator, pop two operands (`A` and `B`), form string:
(A + B)
, push to stack - Read `C`: Operand, push to stack:
(A + B) C
- 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*
- Read `A`: Operand, push to stack:
A
- Read `B`: Operand, push to stack:
A B
- Read `+`: Operator, pop two operands (`A` and `B`), form string:
+AB
, push to stack - Read `C`: Operand, push to stack:
+AB C
- Read `*`: Operator, pop two operands (`+AB` and `C`), form string:
*+ABC
, push to stack
Prefix: *+ABC
📝 Fibonnaci Series: