Computers evaluate expressions such as A + B × C. Writing them in a form without brackets, and using a stack, makes evaluation simple.
Infix, Prefix and Postfix
In infix notation, the operator is written between its operands, as in A + B. This is how people write, but it needs rules of precedence and brackets to be clear: in A + B × C, the multiplication is done first. In prefix notation, also called Polish notation after Jan Łukasiewicz, the operator comes before its operands: + A B. In postfix, or reverse Polish notation (RPN), the operator comes after its operands: A B +. Prefix and postfix need no brackets and no precedence rules, because the order of the symbols fixes the order of operations. A + B × C is A B C × + in postfix and + A × B C in prefix.
Definition: Postfix notation. A way of writing expressions in which each operator follows its operands, such as A B +, so that no brackets or precedence rules are needed.
Precedence and Associativity
The usual precedence, from highest, is: ^ (exponent), then × and /, then + and −. Operators of equal precedence are applied left to right, except ^, which is applied right to left. Brackets override precedence.
Conversion of Infix to Postfix
The stack algorithm scans the infix expression from left to right. An operand is added directly to the output. A left bracket is pushed onto the stack. A right bracket causes operators to be popped to the output until the matching left bracket, which is discarded. An operator first causes operators of higher or equal precedence (for left-associative operators) on the top of the stack to be popped to the output, and is then pushed. At the end, all remaining operators are popped to the output. This is part of Dijkstra's shunting-yard algorithm, and compilers use it.
Table 3.2: Forms of an expression
| Infix | Postfix | Prefix |
|---|---|---|
| A + B | A B + | + A B |
| A + B × C | A B C × + | + A × B C |
| (A + B) × C | A B + C × | × + A B C |
| A × (B + C) / D | A B C + × D / | / × A + B C D |
Example 3.2: Infix to postfix conversion
Problem: Convert A + B × (C − D) / E to postfix.
Solution:
- A to output. + pushed. B to output. × pushed (higher than +).
- ( pushed. C to output. − pushed. D to output.
- ) pops − to output and removes (. Output: A B C D −.
- / pops × (equal precedence), then / is pushed. Output: A B C D − ×.
- E to output. At the end, pop / and +.
Result: The postfix form is A B C D − × E / +.
#include <stdio.h>
#include <ctype.h>
char st[50]; int top = -1;
int prec(char c) {
return c == '^' ? 3 : (c == '*' || c == '/') ? 2 :
(c == '+' || c == '-') ? 1 : 0;
}
int main(void) {
const char *in = "A+B*(C-D)/E";
for (const char *p = in; *p; p++) {
char c = *p;
if (isalnum((unsigned char)c)) putchar(c);
else if (c == '(') st[++top] = c;
else if (c == ')') {
while (st[top] != '(') putchar(st[top--]);
top--;
} else {
while (top >= 0 && prec(st[top]) >= prec(c))
putchar(st[top--]);
st[++top] = c;
}
}
while (top >= 0) putchar(st[top--]);
putchar('\n');
return 0;
}
Output
ABCD-*E/+
Common mistakes
- Pushing an operator without first popping those of higher or equal precedence.
- Putting brackets into the postfix output.
- Treating ^ as left-associative.
Key takeaways
- Infix puts the operator between, prefix before, postfix after the operands.
- Postfix and prefix need no brackets.
- Precedence: ^ highest, then × and /, then + and −.
- The conversion uses a stack of operators.
Practice questions
- Write A × B + C in postfix.
- Why is postfix useful for computers?
- What happens on a right bracket in the conversion?
Answers to the practice questions
- A B × C +.
- It needs no brackets or precedence rules and can be evaluated in one scan with a stack.
- Operators are popped to the output until the matching left bracket, which is discarded.
Sources
- Official BCA syllabus Data Structures, course code 25BCA402DS02, Unit III
