Infix, Prefix and Postfix Expressions
In Infix expressions, the operator is placed between the operands (e.g., A + B), while in Prefix expressions, the operator precedes the operands (e.g., +AB), and in Postfix expressions, the operator follows the operands (e.g., AB+)..
Infix, Prefix and Postfix Expressions
What are these?
The three terms, infix prefix, and postfix will be dealt with individually later. In general, these are the notations to write an expression. Mathematical expressions have been taught to us since childhood. Writing expressions to add two numbers for subtraction, multiplication, or division. They were all expressed through certain expressions. That's what we're learning today: different expressions.
Infix:
This is the method we have all been studying and applying for all our academic life. Here the operator comes in between two operands. And we say, two is added to three. For eg: 2 + 3, a * b, 6 / 3 etc.
< operand 1 >< operator >< operand2 >
Prefix:
This method might seem new to you, but we have vocally used them a lot as well. Here the operator comes before the two operands. And we say, Add two and three. For e.g.: + 6 8, * x y, - 3 2 etc.
< operator >< operand 1 >< operand2 >
Postfix:
This is the method that might as well seem new to you, but we have used even this in our communication. Here the operator comes after the two operands. And we say, Two and three are added. For e.g.: 5 7 +, a b *, 12 6 / etc.
< operand 1 >< operand2 >< operator >
To understand the interchangeability of these terms, please refer to the table below.
Infix | Prefix | Postfix |
---|---|---|
a * b | * a b | a b * |
a - b | - a b | a b - |
So far, we have been dealing with just two operands, but a mathematical expression can hold a lot more. We will now learn to change a general infix mathematical expression to its prefix and postfix relatives. But before that, it is better to understand why we even need these methods.
Why These Methods?
When we evaluate a mathematical expression, we have a rule in mind called BODMAS, which defines the order of operator precedence: brackets, orders, division, multiplication, addition, and subtraction. However, what would you do when faced with evaluating a 1000-character long expression, or even longer? You would likely try to automate the process.
There is one issue: computers don’t follow BODMAS; instead, they have their own operator precedence. This is where postfix and prefix notations come into play. In programming, postfix notation is often used to align with the precedence order of machines.
Consider the expression a * ( b + c ) * d;
. Since computers evaluate expressions from left to right, we need to convert this infix expression to its postfix form.
The postfix form is a b c + * d *
. You might wonder how we arrived at this. Please refer to the illustration below.
Converting Infix, Prefix, and Postfix Expressions
We have successfully reached the point where we can guide the machine on how to handle mathematical expressions. The next step is converting infixes to postfixes and prefixes.
Converting Infix to Prefix
Consider the expression: x - y * z
.
-
Parenthesize the Expression: The infix expression must be parenthesized by following operator precedence and associativity. The expression becomes: [ ( x - ( y * z ) ) ]
-
Convert Innermost Parentheses: Start with the innermost parentheses and convert them into prefix: [ ( x - ( y * z ) ) \rightarrow ( x - [ * y z ] ) ]
-
Continue Converting: Keep converting from the innermost to the outer parentheses: [ ( x - [ * y z ] ) \rightarrow [ - x * y z ] ]
-
Final Prefix Expression: The final prefix expression is: [
- x * y z ]
Converting Infix to Postfix
Consider the same expression: x - y * z
.
-
Parenthesize the Expression: Parenthesize the expression as done previously: [ ( x - ( y * z ) ) ]
-
Convert Innermost Parentheses: Start with the innermost parentheses and convert them into postfix: [ ( x - ( y * z ) ) \rightarrow ( x - [ y z * ] ) ]
-
Continue Converting: Keep converting from the innermost to the outer parentheses: [ ( x - [ y z * ] ) \rightarrow [ x y z * - ] ]
-
Final Postfix Expression: The final postfix expression is: [ x y z * - ]
p - q - r / a
to Prefix
Example: Converting The expression follows these conversions to become a prefix expression:
- Conversion Steps: [ p - q - r / a \rightarrow ( ( p - q ) - ( r / a ) ) \rightarrow ( [ - p q ] - [ / r a ] ) \rightarrow - - p q / r a ]
Quick Quiz
Convert the following infix expression into its postfix form:
- (( p - q ) * ( m - n ))
Note: You cannot change the expression. For example, (( p - q ) * ( m - n )) cannot be altered to something like (( p - ( q * m ) - n )).
Conversion Steps:
- (( p - q ) * ( m - n ) \rightarrow ( ( p - q ) * ( m - n ) ) \rightarrow ( [p q - ] [m n - ] ) \rightarrow p q - m n -)