What Are Infix, Prefix, and Postfix Expressions?
Arithmetic expressions contain operands, such as numbers or variables, and
operators, such as +, -, *, and /.
The position of an operator relative to its operands determines the notation used by the expression.
The three common forms are infix, prefix, and postfix. Understanding these forms is important when learning stacks, expression evaluation, parsing, and compiler fundamentals.
For the same mathematical operation, A + B, the three forms are:
Infix: A + B,
Prefix: + A B,
and Postfix: A B +.
The Three Expression Notations
| Notation | Example | Operator Position |
|---|---|---|
| Infix | A + B |
Between operands |
| Prefix | + A B |
Before operands |
| Postfix | A B + |
After operands |
Infix Expression
An infix expression places the operator between its operands. This is the notation developers commonly use when writing arithmetic expressions in programming languages.
A + B
10 - 4
5 * 3
Infix expressions are easy for humans to read, but they can require additional rules to determine exactly which operation should be performed first.
For example:
5 + 3 * 2
The multiplication is performed before addition because multiplication has higher precedence:
5 + (3 * 2)
= 5 + 6
= 11
Prefix Expression
A prefix expression places the operator before its operands. Prefix notation is also called Polish notation.
For example, the infix expression:
A + B
becomes:
+ A B
A more complex expression can also be represented without parentheses when the operator and operand structure is known.
Infix:
(A + B) * C
Prefix:
* + A B C
Postfix Expression
A postfix expression places the operator after its operands. It is also called Reverse Polish Notation (RPN).
The infix expression:
A + B
becomes:
A B +
For:
(A + B) * C
the postfix form is:
A B + C *
One Expression in All Three Forms
Consider the expression:
(A + B) * (C - D)
The same expression can be represented in three different forms:
| Notation | Expression |
|---|---|
| Infix | (A + B) * (C - D) |
| Prefix | * + A B - C D |
| Postfix | A B + C D - * |
All three forms represent the same calculation. The difference is the position of the operators.
Why Prefix and Postfix Do Not Need Parentheses
Infix expressions often require parentheses because operators can have different precedence levels. Prefix and postfix expressions encode the evaluation order through the position of the operators.
For example:
Infix:
A + B * C
Postfix:
A B C * +
Prefix:
+ A * B C
The postfix form clearly indicates that B * C must be evaluated before the addition.
Similarly, the prefix form places the multiplication operator with its operands before the addition
structure is completed.
Operator Precedence
Infix expressions depend heavily on operator precedence. Operators with higher precedence are generally evaluated before operators with lower precedence.
| Operator | Typical Precedence | Example |
|---|---|---|
*, / |
Higher | A * B |
+, - |
Lower | A + B |
Parentheses can override the normal precedence rules:
A + B * C
A + (B * C)
(A + B) * C
Operator Associativity
When two operators have the same precedence, associativity determines the order in which they are evaluated.
For example, subtraction is generally left-associative:
20 - 5 - 3
= (20 - 5) - 3
= 12
Associativity becomes especially important when converting infix expressions to prefix or postfix notation.
Converting Infix to Postfix
Converting an infix expression to postfix is one of the most common stack applications. A stack temporarily stores operators while operands are added to the output.
Consider:
A + B * C
The postfix result is:
A B C * +
Basic Conversion Rules
- If the token is an operand, add it to the output.
- If the token is an opening parenthesis, push it onto the operator stack.
- If the token is a closing parenthesis, pop operators until the matching opening parenthesis is found.
- If the token is an operator, compare its precedence with the operator at the top of the stack.
- Pop higher-precedence operators before pushing the current operator.
- After processing all tokens, pop the remaining operators into the output.
Infix to Postfix Example
Consider:
A + B * C
| Token | Output | Operator Stack |
|---|---|---|
A |
A |
Empty |
+ |
A |
+ |
B |
A B |
+ |
* |
A B |
+ * |
C |
A B C |
+ * |
Finally, the operators are popped from the stack:
A B C * +
JavaScript Infix to Postfix Implementation
The following implementation demonstrates the stack-based conversion process for common arithmetic operators and parentheses.
function infixToPostfix(expression) {
const output = [];
const stack = [];
const precedence = {
"+": 1,
"-": 1,
"*": 2,
"/": 2
};
for (const token of expression.replace(/\s+/g, "")) {
if (/[a-zA-Z0-9]/.test(token)) {
output.push(token);
continue;
}
if (token === "(") {
stack.push(token);
continue;
}
if (token === ")") {
while (stack.length > 0 && stack[stack.length - 1] !== "(") {
output.push(stack.pop());
}
stack.pop();
continue;
}
while (
stack.length > 0 &&
stack[stack.length - 1] !== "(" &&
precedence[stack[stack.length - 1]] >= precedence[token]
) {
output.push(stack.pop());
}
stack.push(token);
}
while (stack.length > 0) {
output.push(stack.pop());
}
return output.join(" ");
}
console.log(infixToPostfix("A + B * C"));
// A B C * +
console.log(infixToPostfix("(A + B) * C"));
// A B + C *
Converting Infix to Prefix
Infix expressions can also be converted to prefix notation. One common approach is to reverse the expression, adjust parentheses, convert the result using stack-based precedence rules, and then reverse the resulting postfix expression.
For example:
Infix:
(A + B) * C
Prefix:
* + A B C
Another approach is to directly construct an expression tree and perform a preorder traversal. Both approaches are useful for understanding expression conversion.
Evaluating Postfix Expressions
Once an infix expression has been converted to postfix, it can be evaluated with a single stack.
Consider:
5 3 2 * +
The evaluation is:
3 * 2 = 6
5 + 6 = 11
Therefore, the final result is 11.
Evaluating Prefix Expressions
Prefix expressions can also be evaluated with a stack. Unlike postfix evaluation, the expression is generally scanned from right to left.
For example:
* + 5 3 2
represents:
(5 + 3) * 2 = 16
Infix vs Prefix vs Postfix
| Feature | Infix | Prefix | Postfix |
|---|---|---|---|
| Operator position | Between operands | Before operands | After operands |
| Example | A + B |
+ A B |
A B + |
| Parentheses often needed | Yes | No | No |
| Human readability | High | Lower | Lower |
| Common stack use | Conversion and parsing | Evaluation and conversion | Evaluation |
Expression Trees
Another useful way to understand these three notations is through an expression tree. Operators become internal nodes, while operands become leaf nodes.
Consider:
(A + B) * C
The root operator is *. Its left side contains A + B, while its right
side contains C.
Different tree traversals produce the different expression notations:
- Inorder traversal: Infix expression
- Preorder traversal: Prefix expression
- Postorder traversal: Postfix expression
| Traversal | Expression Form | For (A + B) * C |
|---|---|---|
| Inorder | Infix | A + B * C with grouping preserved as needed |
| Preorder | Prefix | * + A B C |
| Postorder | Postfix | A B + C * |
Why Stacks Matter in Expression Conversion
When converting expressions, operators cannot always be written to the output immediately. An operator may need to wait until another operator with higher or equal precedence has been handled.
The operator stack provides temporary storage for these pending operations. This is one of the clearest examples of how the LIFO property of a stack solves a practical algorithmic problem.
Common Mistakes
- Confusing the position of operators in prefix and postfix notation.
- Ignoring operator precedence while converting infix expressions.
- Ignoring associativity when operators have equal precedence.
- Forgetting to process remaining operators at the end of conversion.
- Removing parentheses incorrectly during conversion.
- Reversing operands incorrectly during prefix or postfix evaluation.
- Assuming infix expressions always require parentheses around every operation.
Complexity of Expression Conversion
A standard stack-based infix-to-postfix conversion processes each token a limited number of times.
Therefore, for an expression containing n tokens, the typical complexity is:
| Operation | Complexity |
|---|---|
| Time | O(n) |
| Space | O(n) |
Where These Expressions Are Used
Prefix, postfix, and infix expressions are important beyond textbook algorithms. They are connected to how programming languages represent, parse, and evaluate expressions.
- Compilers: parsing and transforming expressions.
- Interpreters: evaluating expressions according to language rules.
- Calculators: processing mathematical operations.
- Expression parsers: converting tokens into executable structures.
- Data structures interviews: testing stack and precedence concepts.
- Expression trees: representing operations and operands structurally.
Key Takeaways
- Infix places the operator between its operands.
- Prefix places the operator before its operands.
- Postfix places the operator after its operands.
- Prefix notation is also called Polish notation.
- Postfix notation is also called Reverse Polish Notation.
- Infix expressions rely on precedence, associativity, and parentheses.
- Prefix and postfix expressions encode operation order through operator placement.
- Stacks are commonly used when converting infix expressions to prefix or postfix notation.
- Postfix expressions can be evaluated from left to right using an operand stack.
- Prefix expressions can be evaluated from right to left using an operand stack.
- Expression trees connect inorder, preorder, and postorder traversals with infix, prefix, and postfix notation.