Postfix to Prefix Visualization & Animation

Converts postfix to prefix by pushing operands and combining on operators using a stack; O(n).

## What is it? Convert a postfix expression (Reverse Polish Notation, e.g. `AB+C*`) to prefix notation (e.g. `*+ABC`). Both formats use no parentheses but in different positions. ## How it works - Scan postfix expression left to right using a stack - **Operand** → push to stack - **Operator** → pop two operands (`op2` then `op1`), form `operator + op1 + op2`, push the result back - At the end, the stack contains the prefix expression ## When to use - Expression format conversion in compiler stages - Understanding the duality between prefix and postfix representations ## Key Points - Note the operand pop order: pop `op2` first, then `op1` (they were pushed left-to-right) - O(n) time and space - The result at the top of the stack is the complete prefix expression

Category: algorithms

Difficulty: intermediate

Time Complexity: O(n)

Space Complexity: O(n)

Postfix to Prefix

intermediate

Converts postfix to prefix by pushing operands and combining on operators using a stack; O(n).

Init
empty
Stack
i
a
[0]
b
[1]
+
[2]
c
[3]
*
[4]
Initialize: postfix="ab+c*". Scan left to right, build prefix strings on stack.