Why do we need Prefix and Postfix notations?

Technology CommunityCategory: StacksWhy do we need Prefix and Postfix notations?
VietMX Staff asked 3 years ago

Infix notation is easy to read for humans, whereas pre-/postfix notation is easier to parse for a machine. The big advantage in pre-/postfix notation is that there never arise any questions like operator precedence.

For example, consider the infix expression 1 # 2 $ 3. Now, we don’t know what those operators mean, so there are two possible corresponding postfix expressions: 1 2 # 3 $ and 1 2 3 $ #. Without knowing the rules governing the use of these operators, the infix expression is essentially worthless.

Also. in case of infix expression , the time required for evaluation is O(n2). The reason is precedence of operators. Ex : a+b*c. In this we won’t directly evaluate a+b. First we traverse the entire expression and find the operator having highest precedence . In our case it is *, thus first we evaluate b*c and thus again follow the same procedure again , till we complete evaluate the expression.

But this is not so in case of postfix or prefix expression. The time required for evaluation of postfix expression is O(n).