codeintuition-logo

Understanding prefix to postfix conversion


Just as reversing the postfix notation of an expression does not result in its prefix notation, reversing the prefix notation does not convert it to the postfix notation. This is because reversing the string does not respect the associativity of operators and their pairing with operands. Moreover, nested expressions, where the evaluated result of a sub-expression is used as an operand for evaluating the remaining expression, rely on the correct order of operators and operands. Consider the example below

Loading Image

An example of a prefix expression.

Now, consider reversing the prefix expression in an attempt to generate the postfix expression. This will generate an incorrect postfix notation that does not evaluate the same as the original prefix notation. This is because, in the prefix notation, an operator must precede its operands, and simply reversing does not consider this pairing and associativity of the operator.

Liking the course? Check our discounted plans to continue learning.