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
- expression-evaluation
- stack
Time Complexity: O(n)
Space Complexity: O(n)
View Postfix to Prefix Visualization