Infix, Prefix and Postfix Expression with example

Infix, Postfix and Prefix notations are the ways of writing and evaluating Arithmetic & Algebraic expressions.

Infix, prefix, and postfix are ways to write math expressions. Infix is what we use every day, like 2 + 3. Prefix, also called Polish notation, puts the operator before the numbers, like + 2 3. Postfix, or Reverse Polish notation, puts the operator after the numbers, like 2 3 +.

Infix is easy to read for us, but computers find prefix and postfix easier to deal with. They don’t need extra rules to figure out the order of operations.

Let’s look at an example with addition and multiplication, which we usually solve with the rule “multiplication first.” In infix, it’s 2 + 3 * 4. In prefix, it’s + 2 * 3 4, telling us to multiply 3 and 4 before adding 2. In postfix, it’s 2 3 4 * +, which does the same.

These different ways help computers and calculators do math more efficiently. While we prefer infix for its readability, prefix and postfix make processing expressions straightforward for machines.

1). Infix notation: A + B

Infix notation is the most familiar format, where operators are placed between operands. For example, the simple arithmetic operation “2 + 3” is in infix notation. While easy for humans to understand, infix expressions can be complex for computers to evaluate due to the need to consider operator precedence and parentheses to dictate the order of operations.

When we write any arithmetic expression in infix notation, operators are written in-between their operands.

For example

(A+B) or A * ( B + C ) / D is in infix notation.


An expression such as A * ( B + C ) / D is usually taken to mean something like: “First add B and C together, then multiply the result by A, then divide by D to give the final answer.”

Infix notation needs extra information to make the order of evaluation of the operators clear: rules built into the language about operator precedence and associativity, and brackets ( ) to allow users to override these rules. For example, the usual rules for associativity say that we perform operations from left to right, so the multiplication by A is assumed to come before the division by D.

Similarly, the usual rules for precedence say that we perform multiplication and division before we perform addition and subtraction.

2). Postfix notation (“Reverse Polish notation”): A B +

Postfix notation, or Reverse Polish notation, places the operator after the operands. Thus, “2 + 3” in infix becomes “2 3 +” in postfix. Like prefix, postfix notation removes the need for parentheses and clarifies the order of operations, simplifying the computational evaluation process.

When we write any arithmetic expression in Postfix notation, operators are written after their operands.

For example

A B C + * D / is in postfix notation

The order of evaluation of operators is always left-to-right, and brackets cannot be used to change this order. Because the “+” is to the left of the “*” in the example above, the addition must be performed before the multiplication.
Operators act on values immediately to the left of them. For example, the “+” above uses the “B” and “C”.

We can add (totally unnecessary) brackets to make this explicit:
( (A (B C +) *) D /)

Thus, the “*” uses the two values immediately preceding: “A”, and the result of the addition. Similarly, the “/” uses the result of the multiplication and the “D”.

3). Prefix notation (“Polish notation”): + A B

Prefix notation, or Polish notation, places the operator before the operands. For instance, the infix expression “2 + 3” becomes “+ 2 3” in prefix. One of its main advantages is the elimination of the need for parentheses, as the order of operations is inherently clear, making it easier for computers to parse and evaluate.

When we write any arithmetic expression in Prefix notation, operators are written before their operands.

For example

/ * A + B C D is in Prefix notation

As for Postfix, operators are evaluated left-to-right and brackets are superfluous. Operators act on the two nearest values on the right. I have again added (totally unnecessary) brackets to make this clear:
(/ (* A (+ B C) ) D)

Although Prefix “operators are evaluated left-to-right”, they use values to their right, and if these values themselves involve computations then this changes the order that the operators have to be evaluated in. In the example above, although the division is the first operator on the left, it acts on the result of the multiplication, and so the multiplication has to happen before the division (and similarly the addition has to happen before the multiplication).


Because Postfix operators use values to their left, any values involving computations will already have been calculated as we go left-to-right, and so the order of evaluation of the operators is not disrupted in the same way as in Prefix expressions.

In all three versions, the operands occur in the same order, and just the operators have to be moved to keep the meaning correct. (This is particularly important for asymmetric operators like subtraction and division: A – B does not mean the same as B – A; the former is equivalent to A B – or – A B, the latter to B A – or – B A).

Example of Infix, Postfix and Prefix

InfixPostfixPrefixNotes
A * B + C / DA B * C D / ++ * A B / C Dmultiply A and B,
divide C by D,
add the results
A * (B + C) / DA B C + * D // * A + B C Dadd B and C,
multiply by A,
divide by D
A * (B + C / D)A B C D / + ** A + B / C Ddivide C by D,
add B,
multiply by A

Point to be consider while Parsing Expression

We need to take care of operator precedence and associativity to parse any arithmetic expression.

What is Precedence?

To describe precedence we can say it as priority of operator mean which operator will compute oprend first. Suppose we have two different operators which is in between of operands. Now which operator will take the operand first to perform evaluation, is decided by the precedence of an operator over others.

For example −

a*b+c

Multiplication operator has higher precedence than the addition operator. So a * b will be evaluated first and then (result of a*b) + c will be evaluated.

Associativity

When in an expression there are two operators with the same precedence then we will see the Associativity of operators. Associativity is of two types left Associativity and right Associativity.

For example, in expression a – b + c, both – and + have the same precedence, then which part of the expression will be evaluated first, is determined by the associativity of those operators. Here, both – and + are left-associative, so the expression will be evaluated as (a – b) + c.

Precedence and associativity determines the order of evaluation of an expression.

Operator precedence and associativity table (highest to lowest)

S. No.OperatorPrecedenceAssociativity
1Exponentiation ^HighestRight Associative
2Multiplication ( ∗ ) & Division ( / )Second HighestLeft Associative
3Addition ( + ) & Subtraction ( − )LowestLeft Associative

The above table shows the Precedence and associativity of operators.

Applications

  • Compilers and Interpreters: Many compilers and interpreters convert infix expressions to prefix or postfix form as an intermediate step to simplify the process of generating machine code or interpreting the expressions.
  • Calculators: Some advanced calculators use postfix notation for calculations, allowing users to input expressions without worrying about parentheses.

Conclusion

Infix, prefix, and postfix notations each serve their purpose in mathematics and computer science. While infix is naturally more readable for humans, prefix and postfix notations offer computational advantages, making them preferable in programming and for use in calculators. Understanding these different notations and their applications is crucial for anyone diving into the fields of computing and mathematics, bridging the gap between human readability and machine efficiency.