Expression Conversion
Infix · Postfix · Prefix — one stack does it allPeople write infix (a + b * c), but a computer prefers postfix (a b c * +) or
prefix (+ a * b c): no brackets and no priority rules are needed, because the order of the symbols says
what to do first. A stack converts between all of them. Pick one of the six conversions and watch it work.
- Infixa + b (operator in the middle)
- Postfixa b + (operator after)
- Prefix+ a b (operator before)
- Stackholds what has to wait
- Symbol being read
- Pushed / output
- About to pop
Ready
Symbols: letters, numbers (up to 3 digits), + − * / ^ and brackets (infix only), up to 15 · Keyboard: ← → step · Space play / pause · R reset
Every operation, in plain steps
These are exactly the steps the visualizer animates.
Why does a stack help?
Infix (a + b * c) needs brackets and priority rules, so it is hard for a computer to read. Postfix (a b c * +) and prefix (+ a * b c) put every operator next to its operands, so the order of the symbols already says what to do first. A stack remembers whatever has to WAIT: the operators that must wait for a higher-priority operator (infix → postfix / prefix), or the operands that must wait for their operator (the other four conversions).
int prec(char op) { // higher number = done first
if (op == '^') return 3; // works right to left
if (op == '*' || op == '/') return 2;
if (op == '+' || op == '-') return 1;
return 0;
}
char stack[50]; int top = -1; // push: stack[++top] = c; pop: c = stack[top--];
Infix → Postfix
- Read left to right. An operand goes straight to the output.
- "(" is pushed. ")" pops operators to the output until the "(".
- An operator first pops every operator of higher priority (or equal, if it goes left to right), then it is pushed.
- At the end, pop everything left on the stack to the output.
Infix → Prefix
- Reverse the expression and swap each "(" with ")".
- Convert it like infix → postfix, but equal priorities do NOT pop (only ^ does).
- Reverse the output.
- The result is the prefix expression.
Postfix → Infix
- Read left to right. An operand is pushed.
- An operator pops two pieces: the first pop is the RIGHT operand, the second the LEFT one.
- Push (left op right) back as ONE piece.
- At the end one piece is left: the infix expression.
Postfix → Prefix
- Read left to right. An operand is pushed.
- An operator pops two pieces: first = right operand, second = left operand.
- Push op left right back as ONE piece.
- At the end one piece is left: the prefix expression.
Prefix → Infix
- Read RIGHT to left. An operand is pushed.
- An operator pops two pieces: the first pop is the LEFT operand, the second the RIGHT one.
- Push (left op right) back as ONE piece.
- At the end one piece is left: the infix expression.
Prefix → Postfix
- Read RIGHT to left. An operand is pushed.
- An operator pops two pieces: first = left operand, second = right operand.
- Push left right op back as ONE piece.
- At the end one piece is left: the postfix expression.
Priority and order
- ^ comes first, then * and /, then + and −.
- Equal priority: + − * / go left to right; ^ goes right to left.
- Brackets override priority.
- Postfix and prefix need no brackets at all.
Pseudocode
Language-independent steps. The visualizer shows the same logic in C, Python and Java.
How fast is each operation?
n = number of symbols in the expression. Every symbol is read once, and pushed and popped at most once, so every conversion takes O(n) time. The stack may hold up to n pieces, so it needs O(n) extra space. (We count each symbol as one step. The conversions that join pieces into longer strings also copy those strings, which a real program pays a little extra for.)
| Operation | Best | Average | Worst | Speed |
|---|---|---|---|---|
| Push or pop | O(1) |
O(1) |
O(1) |
Instant |
| Any of the six conversions | O(n) |
O(n) |
O(n) |
Grows with n |
| Extra space (the stack) | O(1) |
O(n) |
O(n) |
Grows with n |
The same expression in all three notations
| 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 | a b − c − | − − a b c |
| a ^ b ^ c | a b c ^ ^ | ^ a ^ b c |
What waits on the stack?
| Conversion | Reads | On the stack | An operator… |
|---|---|---|---|
| Infix → Postfix | left to right | operators | waits until higher priorities have left |
| Infix → Prefix | left to right, on the reversed copy | operators | same, with the equal-priority rule flipped |
| Postfix → Infix / Prefix | left to right | pieces (operands) | pops two pieces and joins them |
| Prefix → Infix / Postfix | right to left | pieces (operands) | pops two pieces and joins them |
Check your understanding
Answer each question — you will see the explanation straight away.
What is the postfix form of a + b * c ?
* has higher priority, so b * c is done first: a b c * +. (+ a * b c is the PREFIX form.)
While converting a + b * c to postfix, the symbol * arrives and + is on top of the stack. What happens?
* has higher priority than +, so + does not leave. * waits on top of it, and will leave first.
Why does a closing bracket ")" pop operators from the stack?
The operators above the "(" belong to the bracketed part, which is now finished. The "(" itself is thrown away.
Converting postfix to infix, an operator pops two pieces. The FIRST one popped is its…
In postfix the right operand was pushed last, so it is on top. (In prefix, which is read backwards, the first pop is the LEFT operand.)
A prefix expression is read…
Reading from the right, the operands are pushed before their operator is seen, just as in postfix.
What is * + a b c (prefix) written as infix?
+ a b is a + b, and the * then multiplies that whole piece by c: (a + b) * c.
Stacks quiz
Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.