Prefix/Infix/Postfix Notation

ACSL tests three ways of writing mathematical expressions. You need to convert between them and evaluate prefix/postfix expressions. This topic is very mechanical once you learn the methods - which means it’s free points if you practice.

The three notations

NotationOperator positionExample (2 + 3)
InfixBetween operands2 + 3
PrefixBefore operands+ 2 3
PostfixAfter operands2 3 +

The advantage of prefix and postfix: no parentheses needed and no ambiguity. The order of operations is built into the notation.

Operators you’ll see

SymbolOperation
+Addition
-Subtraction
*Multiplication
/Division
^ or ↑Exponentiation

Precedence (highest to lowest): ^ (exponents), then * / , then + -

Method 1: Fully parenthesize (infix to prefix/postfix)

This is the most reliable conversion method.

Example: Convert to postfix.

Step 1 - Add all implicit parentheses based on precedence:

Step 2 - For postfix, move each operator to after its closing parenthesis:

Step 3 - Remove parentheses:

For prefix, move each operator to before its opening parenthesis:

Method 2: Stack evaluation (evaluating postfix)

To evaluate a postfix expression, scan left to right:

  • Number? Push it onto the stack.
  • Operator? Pop two numbers, apply operator, push result.

Example: Evaluate

ReadStackAction
5[5]Push
8[5, 8]Push
3[5, 8, 3]Push
1[5, 8, 3, 1]Push
-[5, 8, 2]Pop 3 and 1, compute 3-1=2, push
/[5, 4]Pop 8 and 2, compute 8/2=4, push
+[9]Pop 5 and 4, compute 5+4=9, push

Answer: 9

Try it: evaluate a postfix expression

Step through and watch how the stack builds and collapses.

Code
1Expression: 5 8 3 1 - / +2Read 5: push 53Read 8: push 84Read 3: push 35Read 1: push 16Read -: pop 3 and 1, compute 3-1=2, push 27Read /: pop 8 and 2, compute 8/2=4, push 48Read +: pop 5 and 4, compute 5+4=9, push 9
Variables
NameValue
Stack(empty)
Step 1 of 8

Critical detail: When you pop two values for an operator, the first popped is the right operand and the second popped is the left operand. This matters for - and /:

  • Stack has […, A, B] and you see -: the result is A - B (not B - A)
  • Pop B first (right), then A (left), giving A - B

Method 3: Stack evaluation (evaluating prefix)

For prefix, scan right to left and use the same stack logic.

Example: Evaluate

Scan right to left:

ReadStackAction
1[1]Push
3[1, 3]Push
-[2]Pop 3 and 1, compute 3-1=2, push
8[2, 8]Push
/[4]Pop 8 and 2, compute 8/2=4, push
5[4, 5]Push
+[9]Pop 5 and 4, compute 5+4=9, push

Answer: 9

For prefix, operand order is reversed: the first popped is the left operand. So when scanning right-to-left with stack […, A, B] and you see -: pop B (left), then A (right), giving B - A.

Converting postfix/prefix to infix

Work through the expression using the stack method, but instead of computing, build sub-expressions.

Example: Convert to infix.

ReadStack
3[3]
4[3, 4]
*[(3*4)]
8[(3*4), 8]
2[(3*4), 8, 2]
/[(3*4), (8/2)]
+[((3*4)+(8/2))]
7[((3*4)+(8/2)), 7]
5[((3*4)+(8/2)), 7, 5]
-[((3*4)+(8/2)), (7-5)]
^[((3*4)+(8/2))^(7-5)]

Answer: ((3*4)+(8/2))^(7-5)

Quick mental tricks

Spotting the operator for the root of the expression:

  • In postfix, the last symbol is the main operator
  • In prefix, the first symbol is the main operator

This tells you the overall structure immediately.

For simple expressions (2 operators), you can often convert mentally:

  • Postfix means
  • Prefix means

Common mistakes

  1. Operand order with - and /. In (postfix), the answer is , not . The first number written is the left operand.
  2. Missing the precedence when parenthesizing. must become , not .
  3. Exponentiation is right-associative. = = , not .
  4. Spaces matter. 12 is the number twelve, not 1 and 2.

Contest strategy

  • Evaluation problems: use the stack method, ~30 seconds
  • Conversion problems: fully parenthesize first, ~45 seconds
  • Always verify by evaluating your converted expression - it should give the same result as the original