Skip to main content

Postfix To Infix Conversion


# Postfix Expression Stack
 Postfix : ab+c*ef+g/+
 Infix   : (((a+b)*c)+((e+f)/g))
 Postfix : abc*de-/+
 Infix   : (a+((b*c)/(d-e)))

Postfix notation, also known as Reverse Polish Notation (RPN), is a way of writing mathematical expressions where the operator comes after the operands.

To convert a postfix expression to infix notation, we can use a stack to keep track of the operands and operators.

Here's how the conversion works:

  1. Start scanning the postfix expression from left to right.
  2. If the current token is an operand (i.e., a number), push it onto the stack.
  3. If the current token is an operator, pop two operands from the stack, place them in parentheses and combine them with the operator in the middle. Then push the resulting expression back onto the stack.
  4. Repeat steps 2 and 3 until the end of the postfix expression is reached.

Let's take an example to illustrate the process:

To convert the postfix expression "ab+c*ef+g/+" to infix notation, we follow the steps outlined in the previous answer:

  1. Start scanning the postfix expression from left to right.
  2. If the current token is an operand (i.e., a letter), push it onto the stack.
  3. If the current token is an operator, pop two operands from the stack, place them in parentheses and combine them with the operator in the middle. Then push the resulting expression back onto the stack.
  4. Repeat steps 2 and 3 until the end of the postfix expression is reached.

Using these steps, we can convert the postfix expression "ab+c*ef+g/+" to infix notation as follows:

Step 1: Read "a" and "b" as operands, push them onto the stack.

Stack: [a, b]

Step 2: Read "+" as an operator, pop two operands from the stack, and combine them with the operator in the middle. Push the resulting expression back onto the stack.

Stack: ["(a + b)"]

Step 3: Read "c" as an operand, push it onto the stack.

Stack: ["(a + b)", c]

Step 4: Read "*" as an operator, pop two operands from the stack, and combine them with the operator in the middle. Push the resulting expression back onto the stack.

Stack: ["((a + b) * c)"]

Step 5: Read "e" and "f" as operands, push them onto the stack.

Stack: ["((a + b) * c)", e, f]

Step 6: Read "+" as an operator, pop two operands from the stack, and combine them with the operator in the middle. Push the resulting expression back onto the stack.

Stack: ["(((a + b) * c) + (e + f))"]

Step 7: Read "g" as an operand, push it onto the stack.

Stack: ["(((a + b) * c) + (e + f))", g]

Step 8: Read "/" as an operator, pop two operands from the stack, and combine them with the operator in the middle. Push the resulting expression back onto the stack.

Stack: ["((((a + b) * c) + (e + f)) / g)"]

The final result is the infix expression "((((a + b) * c) + (e + f)) / g)".

Here given code implementation process.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment