Posted on by Kalkicode
Code Tools

Prefix to infix conversion


# Prefix Expression Stack
Example
 Prefix : +*ab^cd
 Infix  : ((a*b)+(c^d))
 Prefix : -+*^%adcex*y^ab
 Infix  : (((((a%d)^c)*e)+x)-(y*(a^b)))

To convert the prefix expression "+*ab^cd" to infix notation, we can follow these steps:

  1. Starting from the left-hand side of the expression, we encounter the "+" operator first. We then take the two operands to its right, which are "*ab" and "^cd".

  2. We continue evaluating the sub-expressions from left to right. For the sub-expression "*ab", we encounter the "*" operator and take the two operands to its right, "a" and "b", and combine them using the multiplication operator "*". This gives us the sub-expression "a*b".

  3. For the sub-expression "^cd", we encounter the "^" operator and take the two operands to its right, "c" and "d", and combine them using the exponentiation operator "^". This gives us the sub-expression "c^d".

  4. We can now substitute the sub-expressions back into the original expression to get:

    (a*b) + (c^d)

Therefore, the infix notation for the prefix expression "+*ab^cd" is "(a*b) + (c^d)".

Steps

Converting a prefix notation expression to infix notation involves rearranging the order of the operands and operators. Here is an algorithm to do this:

  1. Start from the rightmost element of the prefix expression.
  2. If the current element is an operand, push it onto a stack.
  3. If the current element is an operator, pop two operands from the stack and combine them with the operator in infix notation. Add parentheses around the resulting expression.
  4. Continue the above steps until all elements have been processed.
  5. The final expression on the stack is the infix notation equivalent of the prefix expression.

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