codeintuition-logo

Understanding postfix to prefix conversion


Both the prefix and postfix notation may seem just the opposite of each other but it is important to note that simply reversing the postfix notation does not convert it to the prefix notation. This is because certain operators like division and modulo operators also follow associativity rules, meaning that the relative order of operands affects the results. 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 postfix expression.

Now, consider reversing the postfix expression in an attempt to generate the prefix expression. This will generate an incorrect prefix notation that does not evaluate the same as the original postfix notation. This is because, in the postfix notation, an operator must follow 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.