5

Had this question in the interview yesterday. Which is better to use? Infix(with parenthesis) or Postfix? State with reason..

I only could tell them that:

  1. it is easier for the compilers to process postfix expression for arithmetic evaluations and operator precedence.
  2. More memory is used for storing and processing the parenthesis.

Please throw some light on whether I am right on this?

1
  • 2
    The question is meaningless without some definition of 'better'. Infix is natural for humans, postfix for computers. Yet another pointless interview question. The memory used for parentheses exists only in the source file BTW.
    – user207421
    Mar 15, 2017 at 2:39

3 Answers 3

9

Postfix doesn't require any operator-driven order of operations; it's always explicit. So for a stack-based compiler, it's very easy to implement, and for humans, it's easy to understand the order of operations.

On the other hand, infix doesn't require you to read all the verbs at the end :-P. Humans work on infix or even prefix; "add A to B" makes more sense than "A and B: add them".

But, this question is fundamentally subjective. Postfix will always be better for the computer system, but infix has its merits.

2
  • Postfix doesn't require any operator precedence. Not the same thing as 'order of operations' at all. Postfix is nothing but an order of operators.
    – user207421
    Mar 15, 2017 at 2:41
  • The analogy between postfix and English grammar is very enlightening.
    – yuqli
    Jan 27, 2020 at 19:22
1

Memory is not usually an issue here. Compilers spend tons of memory on other things. Postfix usually entails an operand stack, which involves a certain amount of memory (still small) at runtime.

Postfix is useful for low-level interpretable code, such as Java, Forth, Postscript. These all have hardware or super-lightweight software implementations.

Infix is better for readability.

Of course, there's no way to know how the issue affects your field without knowing what kind of job it is.

0

The similarity between Infix, Postfix and Prefix:

  • Order of operands is same in the three notations(but the order of operators changes, hence the names also)

Differences between Infix, Postfix and Prefix:

  • Infix is more human-readable. That's why it is very commonly used in mathematics books.
  • Infix has to add more information to remove ambiguity. So, for example, we use parenthesis to give preference to lower precedence operator, but in postfix we have order of operators. (as mentioned by @EJP here
  • Postfix and Prefix are more machine-readable. So for instance, in postfix, every time you encounter a number, put it on a stack, every time you encounter an operator, pop the last two elements from the stack, apply the operation and push the result back.

Thanks

2
  • Infix also tells what the exxression has to do. It if didn't, it would be impossible to convert it to postfix.
    – user207421
    Mar 15, 2017 at 2:40
  • @EJP : Thanks for helping me correct the response. :)
    – kinshuk4
    Mar 20, 2017 at 12:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.