Infix to Prefix Converter
Convert an infix expression to prefix notation and follow each conversion step.
Infix Postfix
Watch tokens move into the stack and output. Play it through or explore one step at a time.
Ready to convert
Read left to right. Operands go to output; operators wait on the stack.
Space play / pause ยท โ โ stepยท Scroll over the player to step ยท Esc deselect
Step-by-Step Conversion ยท Stack-Based
Read left to right with an operator stack and an expression stack. To reduce an operator, pop the right fragment, then the left, and push operator + left + right. Stacks below run from bottom to top; | separates expression fragments.
| Token | Action | Operator stack | Expression stack |
|---|---|---|---|
| A | Push operand A onto the expression stack. | โ | A |
| + | Push operator + after resolving higher-priority operations. | + | A |
| B | Push operand B onto the expression stack. | + | A | B |
| * | Push operator * after resolving higher-priority operations. | + * | A | B |
| C | Push operand C onto the expression stack. | + * | A | B | C |
| End | Pop *, then right and left fragments; push * B C. | + | A | * B C |
| End | Pop +, then right and left fragments; push + A * B C. | โ | + A * B C |
Visual Breakdown
Read the tree in prefix order: visit the operator first, then its left subtree, then its right subtree. The numbers on the nodes show that reading order. Expand the diagram to pan and zoom.
Prefix reading order: + A * B C
Examples
| Infix | Prefix |
|---|---|
| A + B * C | + A * B C |
| (A + B) * C | * + A B C |
| A - B - C | - - A B C |
| A ^ B ^ C | ^ A ^ B C |
| price * quantity + tax | + * price quantity tax |
Key Terms
| Term | Label | Details |
|---|---|---|
| Infix | Input notation | The operator sits between its operands. |
For example, | ||
| Prefix | Output notation | The operator comes before its operands. |
| Precedence | Operator priority | Which operator binds more tightly. |
| Associativity | Grouping rule | How equal-priority operators group. |
Frequently Asked Questions
Enter an infix expression. This tool uses two stacks: one for operators and one for prefix fragments. It applies parentheses, precedence and associativity, then combines each operator with its left and right fragments. For example, A + B * C becomes + A * B C.
Spaces separate tokens, so variable names and multi-digit numbers stay unambiguous. For example, total + 12 becomes + total 12.
Use variable names such as A, price or value_1, decimal numbers, binary operators + - * / ^, and parentheses. The tool does not support unary signs, function calls, implicit multiplication or scientific notation. To express a negative value, write (0 - 5).
It converts notation without evaluating the expression. Variable names and numbers remain unchanged, and ^ represents exponentiation.