Skip to content
StackDecode
Stacks & queuesAnimated

Infix to Postfix Conversion

Shunting-yard: an operator stack orders operators by precedence.

Time

O(n)

Extra space

O(n)

Use it for

Parsing arithmetic expressions.

How it works

  1. Scan the expression. Operands (letters, numbers) go straight to the output.
  2. Operators wait on a stack. Before pushing an operator, pop any operators on top with higher or equal precedence into the output (^ is right-associative).
  3. An opening bracket is pushed; a closing bracket pops operators to the output until the matching opening bracket.
  4. At the end pop everything left. The output is the postfix expression.

Watch it run

Loading animation…