Infix, Postfix and Prefix Notations/Expressions


Infix, Postfix and Prefix notations are most common ways of writing expressions.

Infix notation:

Example: (A+B)

Infix notation is commonly used in arithmetic formula or statements, the operators are written in-between their operands. An expression such as A * ( B + C ) / D is solved as:

  • First add B and C.
  • Multiply the result by A
  • Divide result by D to give the final answer.

Infix notation needs order of precedence for binary operators. The precedence for main binary operators is mentioned below

  1. ^
  2. / *
  3. + –

Note: brackets ( ) are used to override these rules.

Click here to read about How to evaluate the Infix expression.


Postfix Notation (Reverse Polish Notation):

Example: A B+

Operators are used after their operands for example to add 3 and 4, instead of writing 3 + 4 which is infix expression, postfix expression will be 3 4 +. The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Postfix expression of example above will be A B C + * D /. Operators act on values immediately to the left of them. This is also called as Polish postfix notation or simply postfix notation.

Click here to read about How to evaluate the Postfix expression. (Coming Soon)


Prefix Notation (Polish Notation):

Example: + A B

Operators are used before their operands for example to add 3 and 4, instead of writing 3 + 4 which is infix expression, prefix expression will be + 3 4. The expressions given above are equivalent to / * A + B C D . Operators are evaluated left to right. Operators act on the two nearest values on the right. Also known as normal Polish notation, Polish prefix notation or simply prefix notation

Click here to read about How to evaluate the Prefix expression. (Coming Soon)

Reference: Wiki and Here

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

%d bloggers like this: