Expressions are used to represent calculations by combining operands and operators. Based on the position of the operator, expressions are mainly classified as infix, prefix, and postfix notation. These notations differ in how operators are placed and how expressions are evaluated.
- Infix: Operator is placed between operands.
- Prefix: Operator is placed before operands.
- Postfix: Operator is placed after operands.
For example, the expression A + B can be represented in three different ways:
- Infix: A + B
- Prefix: + A B
- Postfix: A B +
Prefix and postfix notations are commonly used in expression evaluation, compilers, calculators, and stack-based algorithms.
Infix Expressions
In an infix expression, the operator is written between its operands. It is the standard notation used in mathematics and most programming languages.
Rules of Infix Expressions
- Operators are placed between operands.
- Operator precedence determines which operation is performed first.
- Parentheses can be used to explicitly specify the order of evaluation.
- Operators with the same precedence are evaluated according to their associativity.
Operator Precedence Rules
The commonly used order is:
| Precedence | Operators | Associativity |
|---|---|---|
| Highest | () | Left to right |
| High | *, /, % | Left to right |
| Low | +, - | Left to right |
Example
2 + 3 * 4
First 3 * 4 is evaluated because multiplication has higher precedence.
Result: 2 + 12 = 14
public class Main {
public static void main(String[] args) {
int result = 2 + 3 * 4;
System.out.println("Result: " + result);
}
}
Output
Result: 14
Prefix Expressions
In a prefix expression, also called Polish notation, the operator is placed before its operands. The order of operations is determined by the position of the operators, so parentheses are generally not required.
Rules of Prefix Expressions
- Operators are placed before their operands.
- Parentheses are generally not required.
- Prefix expressions are commonly evaluated from right to left using a stack.
- The position of the operator determines the operation and its operands.
Example
Input: + 2 3
Result: 2 + 3Infix: (2 + 3) * 4
Prefix: * + 2 3 4
The prefix expression explicitly represents that 2 + 3 is performed before multiplication by 4.
import java.util.Stack;
public class Main {
public static int evaluatePrefix(String expression) {
String[] tokens = expression.split(" ");
Stack<Integer> stack = new Stack<>();
for (int i = tokens.length - 1; i >= 0; i--) {
String token = tokens[i];
if (token.matches("-?\\d+")) {
stack.push(Integer.parseInt(token));
} else {
int first = stack.pop();
int second = stack.pop();
switch (token) {
case "+" -> stack.push(first + second);
case "-" -> stack.push(first - second);
case "*" -> stack.push(first * second);
case "/" -> stack.push(first / second);
}
}
}
return stack.pop();
}
public static void main(String[] args) {
String expression = "* + 2 3 4";
System.out.println("Result: " + evaluatePrefix(expression));
}
}
Output
Result: 20
Postfix Expressions
In a postfix expression, also called Reverse Polish Notation (RPN), the operator is placed after its operands.
Rules of Postfix Expressions
- Operators are placed after their operands.
- Parentheses are generally not required.
- Postfix expressions are commonly evaluated from left to right using a stack.
- When an operator is encountered, the required operands are taken from the stack.
Example
Input: 2 3 +
Output: 2 + 3Infix: (2 + 3) * 4
Postfix: 2 3 + 4 *
The postfix expression clearly indicates that 2 + 3 must be evaluated before multiplication by 4.
import java.util.Stack;
public class Main {
public static int evaluatePostfix(String expression) {
String[] tokens = expression.split(" ");
Stack<Integer> stack = new Stack<>();
for (String token : tokens) {
if (token.matches("-?\\d+")) {
stack.push(Integer.parseInt(token));
} else {
int second = stack.pop();
int first = stack.pop();
switch (token) {
case "+" -> stack.push(first + second);
case "-" -> stack.push(first - second);
case "*" -> stack.push(first * second);
case "/" -> stack.push(first / second);
}
}
}
return stack.pop();
}
public static void main(String[] args) {
String expression = "2 3 + 4 *";
System.out.println("Result: " + evaluatePostfix(expression));
}
}
Output
Result: 20
Converting Infix to Prefix
Converting an infix expression to prefix notation is a common stack-based problem in Data Structures and Algorithms.

- First, reverse the given infix expression, such as A+(B*C) -> (C*B)+A.
- Convert the reversed expression into postfix notation: (C*B)+A -> CB*A+.
- Reverse the resulting postfix expression: CB*A+ -> +A*BC.
- The final result is the Prefix Notation: +A*BC.
Infix Vs Prefix Vs Postfix Expressions
| Feature | Infix | Prefix | Postfix |
|---|---|---|---|
| Operator Position | Between operands | Before operands | After operands |
| Example | A + B | + A B | A B + |
| Parentheses | May be required | Not required | Not required |
| Operator Precedence | Required | Not required for evaluation | Not required for evaluation |
| Associativity | Required | Not required for evaluation | Not required for evaluation |
| Evaluation Direction | Based on precedence and associativity | Right to left | Left to right |
| Common Evaluation Method | Parsing rules or conversion | Stack | Stack |
| Human Readability | Easy | Less familiar | Less familiar |
| Common Usage | Mathematics and programming | Expression processing | Expression processing and RPN |
Example of (2 + 3) * 4 | (2 + 3) * 4 | * + 2 3 4 | 2 3 + 4 * |