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

Updated 3 Nov 20265 min read
Infix, Postfix and Prefix Expressions: Stack Rules, Conversions and Worked Examples

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

2 + 3 * 4

Postfix

After its operands

2 3 4 * +

Prefix

Before its operands

+ 2 * 3 4

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.

Expression tree for 2 + 3 * 4 shows root +, multiplication child with leaves 3 and 4, infix, postfix, and prefix forms, and value 14.

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

A

Output operand

[]

A

+

Push operator

[+]

A

B

Output operand

[+]

A B

*

Push operator

[+, *]

A B

(

Push parenthesis

[+, *, (]

A B

C

Output operand

[+, *, (]

A B C

-

Push operator

[+, *, (, -]

A B C

D

Output operand

[+, *, (, -]

A B C D

)

Pop -, discard (

[+, *]

A B C D -

/

Pop *, then push /

[+, /]

A B C D - *

E

Output operand

[+, /]

A B C D - * E

End

Pop /, then +

[]

A B C D - * E / +

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

8

[8]

6

[8, 6]

7

[8, 6, 7]

3

[8, 6, 7, 3]

-

[8, 6, 4]

*

[8, 24]

2

[8, 24, 2]

/

[8, 12]

+

[20]

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

2

[2]

3

[2, 3]

7

[2, 3, 7]

-

[2, 4]

6

[2, 4, 6]

*

[2, 24]

/

[12]

8

[12, 8]

+

[20]

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.

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.