Before understanding the infix, prefix, and postfix let’s understand why this came into practice and what are its uses.
What is the use of these notations?
Infix notation is easy to read for humans, whereas prefix or postfix notation is easier to parse for a machine(computers).
The big advantage in prefix or postfix notation is that there never arise any questions like operator precedence. For example, consider the infix expression 1 # 2 $ 3. Now, we don’t know what those operators mean, so there are two possible corresponding postfix expressions: 1 2 # 3 $ and 1 2 3 $ #. Without knowing the rules governing the use of these operators, the infix expression is essentially worthless. Or, to put it in more general terms: it is possible to restore the original (parse) tree from a prefix or postfix expression without any additional knowledge, but the same isn’t true for infix expressions.
When you write an arithmetic expression such as A * B, the form of the expression provides you the information so that you can interpret it correctly. In this case, we know that variable A is being multiplied by variable B since the multiplication operator ( * ) appears between them in the expression. This type of notation is referred to as infix since the operator is in between the two operands that it’s working on.
Let’s understand infix, prefix, and postfix expression with a clear example
What is infix expression?
The typical mathematical form of expression that we encounter generally is known as infix notation. In infix form, an operator is written in between two operands.
Eg: An expression in the form of A + B is in infix form.
- The traditional method of writing mathematical expressions is called 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 expressions are easy to understand and evaluate for human beings. However, the computer finds it difficult to parse – Information is needed about operator precedence and associativity rules, and brackets that override these rules.
- Hence we have postfix and prefix notations which make the computer take less effort to solve the problem.
What is postfix expression?
In postfix expression, an operator is written after its operands. This notation is also known as “Reverse Polish notation”.
Eg: The above expression can be written in the postfix form as A B +
- 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 a parenthesis-free expression. For evaluation, we evaluate it from left to right.
What is prefix expression?
In prefix expression, The operator is written before the operands.
Eg: The above expression can be written in the postfix form as + A B
- 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 the 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 polish notation.
Some more examples:-
|Infix expression||Prefix expression||Postfix expression|
|A + B * C + D||+ + A * B C D||A B C * + D +|
|(A + B) * (C + D)||* + A B + C D||A B + C D + *|
|A * B + C * D||+ * A B * C D||A B * C D * +|
|A + B + C + D||+ + + A B C D||A B + C + D +|
We can convert the infix expression to prefix as well as postfix and vice versa.
This was a small introduction to the infix, prefix, and postfix expressions.