0
39kviews
Explain infix, postfix and prefix expressions with examples.

Mumbai University > COMPS > Sem 3 > Data Structures

Marks: 6 M

Year: May 2015

1 Answer
2
2.9kviews

Infix, prefix and postfix are three different but equivalent notations of writing algebraic expressions. Let us discuss what they and how are they different from each other and how to obtain it.

a. Infix Notation:

  • The traditional method of our writing of mathematical expressions is called as the infix expressions.
  • It is of the form <operand><operator><operand>.
  • As the name suggests, here the operator is fixed inside between the operands. e.g. A+B here the plus operator is placed inside between the two operators, (A*B)/Q.
  • Such expression are easy to understand and evaluate for human beings. However computer finds it difficult to parse - Information is needed about operator precedence and associativity rules, and brackets which override these rules.
  • Hence we have postfix and prefix notations which make the computer take less effort to solve the problem.

b. Postfix Notation:

  • The postfix expression as the name suggests has the operator placed right after the two operands.
  • It is of the form <operand><operand><operator>
  • In the infix expressions, it is difficult to keep track of the operator precedence whereas here the postfix expression itself determines the precedence of operators (which is done by the placement of operators)i.e the operator which occurs first operates on the operand.
  • E.g. PQ-C/, here – operation is done on P and Q and then / is applied on C and the previous result.
  • A postfix expression is parenthesis-free expression. For evaluation, we evaluate it from left-to-right.
Infix expression Postfix expression
(P+Q)*(M-N) PQ+MN-*
(P+Q) / (M-N) - (A*B) PQ+MN-/AB*-

c. Prefix Notation:

  • The prefix expression as the name suggests has the operator placed before the operand is specified.
  • It is of the form < operator > < operand > < operand >.
  • It works entirely in same manner as the postfix expression.
  • While evaluating a prefix expression, the operators are applied to the operands immediately on the right of the operator.
  • For evaluation, we evaluate it from left-to-right. Prefix expressions are also called as polish notation.
Infix expression Postfix expression
(P+Q)*(M-N) *+PQ-MN
(P+Q) / (M-N) - (A*B) -/+PQ-MN*AB
Please log in to add an answer.