Understanding prefix to infix conversion
Prefix expressions may seem very different from the infix expressions we are used to, but these two notations can be converted back and forth into each other. To convert a prefix expression into an infix, we need to traverse through the expression and place the operator between the operands for all operand pairs. As we will see shortly, this process creates small infix expressions from the initial operators and operands that then serve as operands themselves to form bigger infix expressions. At the end of the process, the entire prefix expression is converted into an infix expression.
To convert a prefix expression to an infix expression, we follow the same idea as its evaluation with only a slight difference. On finding an operator, instead of evaluating the results using the most recent operands, we create an infix expression from the operator and operands and save it on a stack as the operand in a subsequent iteration.
Consider we are given the prefix notation of a mathematical expression as a string given below, where every operand is a single-digit number.
Liking the course? Check our discounted plans to continue learning.