Infix, Postfix and Prefix Expressions: Stack Rules, Conversions and Worked Examples
Learn how one expression becomes infix, postfix and prefix form. Follow the operator stack, build the expression tree and evaluate both stack notations safely.
KnowledgeGate Team
Exam prep & CS education

Infix, postfix and prefix can represent the same expression, but the operator moves and the stack scan direction changes. Parsing, conversion and evaluation apply to the same expression. Spaces keep 12 as one token rather than two digits.
Infix, postfix and prefix represent the same expression
An operand is a value or variable such as 2 or A. An operator, such as + or *, acts on operands. An expression combines them to produce a value.
The three notations differ only in operator placement:
Notation | Operator position | Example |
|---|---|---|
Infix | Between operands |
|
Postfix | After its operands |
|
Prefix | Before its operands |
|
All three evaluate to 14, because multiplication binds before addition. Infix needs precedence rules and sometimes parentheses. Correctly tokenised postfix and prefix already encode evaluation order.
A stack temporarily holds operators during conversion, then operands and intermediate values during evaluation. Stacks and Queues: Operations and Uses starts with push/pop rules, queue behaviour and a first postfix trace. Prefix notation and expression trees extend that foundation. The CS Fundamentals for Exams & Placements collection connects this topic to related concepts.

Fix precedence and associativity before touching the stack
The precedence rules are:
Priority | Operators | Associativity |
|---|---|---|
1, highest | Parentheses | Evaluate the enclosed expression |
2 |
| Right to left |
3 |
| Left to right |
4 |
| Left to right |
A question's stated grammar or operator rules take priority. Associativity matters at equal precedence: 8 - 3 - 2 = (8 - 3) - 2 = 3, while 8 - (3 - 2) = 7. For exponentiation, 2 ^ 3 ^ 2 = 2 ^ (3 ^ 2) = 512, but (2 ^ 3) ^ 2 = 64.
The expression is:
A + B * (C - D) / E
It parses as A + ((B * (C - D)) / E). With A=8, B=6, C=7, D=3 and E=2, its value is 8 + ((6 * (7 - 3)) / 2) = 8 + (24 / 2) = 20.
Convert infix to postfix step by step
Scan left to right. Send operands to the output and push (. On ), pop through the matching (, then discard the parentheses. Before pushing an operator, pop higher-precedence operators and equal-precedence operators when the incoming one is left-associative. Finally, empty the stack.
For A + B * (C - D) / E, the stack top is shown at the right:
Token | Action | Operator stack (bottom to top) | Output |
|---|---|---|---|
| Output operand |
|
|
| Push operator |
|
|
| Output operand |
|
|
| Push operator |
|
|
| Push parenthesis |
|
|
| Output operand |
|
|
| Push operator |
|
|
| Output operand |
|
|
| Pop |
|
|
| Pop |
|
|
| Output operand |
|
|
End | Pop |
|
|
The result is A B C D - * E / +. Substitute values only now: 8 6 7 3 - * 2 / +. Operands retain their left-to-right order during conversion.
Derive prefix from the parsed expression tree
The fixed parse has + at the root, with left leaf A and right child /. The / node has left child * and right leaf E. The * node has left leaf B and right child -, whose leaves are C and D.
Read this tree in preorder, root-left-right, to obtain prefix:
+ A / * B - C D E
Read it in postorder, left-right-root, to recover postfix:
A B C D - * E / +
Their agreement is a self-check. Reverse-and-swap shortcuts can mishandle equal-precedence operators unless associativity is adjusted, so the parsed tree is safer.
Evaluate postfix and prefix without reversing operands
For postfix, scan left to right and push operands. At an operator, pop the right operand first and the left operand second, calculate, then push the result.
Token | Stack after processing, top at right |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For prefix + 8 / * 6 - 7 3 2, scan right to left. Here, the first pop is the left operand and the second is the right operand.
Token scanned | Stack after processing, top at right |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The cross-check is complete: infix, postfix and prefix each evaluate to 20.
![Side-by-side stack timelines compare postfix and prefix evaluation, showing maximum stack [8,6,7,3], intermediate results 4, 24, 12, final 20, and opposite pop orders.](https://cdn.knowledgegate.ai/blog-assets/blog_asset_1784650670749_g7ukos.jpg)
Traps that produce plausible but wrong answers
Wrong precedence: Treating A + B * C as (A + B) * C produces A B + C *. The correct postfix is A B C * +. Check each form against its tree.
Reversed operands: For 9 3 /, pop 3 then 9, but calculate 9 / 3 = 3, not 3 / 9. Similarly, 9 3 - gives 6.
Broken tokenisation or validity: 12 3 4 * + means 12 + (3 * 4) = 24, not separate operands 1 and 2. Stack underflow or multiple values left at the end marks an invalid expression.
How questions test the concept, and the next practice step
Questions ask you to convert notation, evaluate an expression, identify an invalid form, or find maximum stack depth. Our postfix example has 9 tokens, 5 operand pushes and 4 operator applications. Its maximum depth is 4, immediately after reading 3.
Continue with DSA Using Java or Zero to Hero. Test valid stack sequences, expression conversion, queue simulations and circular queues with Stacks and Queues MCQs: 12 Solved (GATE). The practice bank has 50+ questions specifically on infix, postfix and prefix expressions.
The short version
Parse first, using the given precedence and associativity rules.
Convert with the correct scan direction and stack rule.
Evaluate with the correct operand order, then confirm that every notation produces the same value.
Keep learning
Infix, Postfix and Prefix MCQs: 12 Solved Questions with Step-by-Step Explanations
Solve 12 expression-notation MCQs in increasing difficulty, from basic stack use to conversions, associativity and maximum operand-stack depth.
Infix to Postfix Conversion and Evaluation for GATE: Stack Applications Solved
Learn the two stack procedures behind expression conversion and evaluation. Complete traces show what happens to every operator, operand and parenthesis.
Regular Expressions in Theory of Computation: Rules, Conversions and Worked Examples
Learn what classical regular expressions denote, construct one from a language condition, convert it through NFA and DFA, and check equivalence with worked examples.
Evaluation of Expressions MCQs: 12 Solved Questions with Stack Traces
Solve 12 expression MCQs step by step. Trace postfix and prefix evaluation, nesting depth, precedence and notation conversion without reversing operands.





