Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infix vs Postfix

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?

like image 299
Maverickgugu Avatar asked Aug 14 '10 03:08

Maverickgugu


People also ask

What is the difference between postfix and infix?

Infix expression is an expression in which the operator is in the middle of operands, like operand operator operand. Postfix expression is an expression in which the operator is after operands, like operand operator. Postfix expressions are easily computed by the system but are not human readable.

Which is better infix or postfix?

Postfix expression is simple to execute as a comparison to the infix expression it required more operation to execute. In the postfix expression, the overhead of brackets is not there while in the infix expression the overhead of brackets is there.

What is difference between infix and prefix?

Infix : An expression is called the Infix expression if the operator appears in between the operands in the expression. Simply of the form (operand1 operator operand2). Prefix : An expression is called the prefix expression if the operator appears in the expression before the operands.


1 Answers

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.

like image 53
Borealid Avatar answered Oct 16 '22 23:10

Borealid