Infix to Postfix Conversion
Convert infix expressions (A + B) to postfix notation (AB+) using operator precedence and stack. Postfix notation eliminates need for parentheses and is easier for computers to evaluate.
Visualization
Interactive visualization for Infix to Postfix Conversion
Use: A-Z, 0-9, +, -, *, /, ^, (, )
Interactive visualization with step-by-step execution
Implementation
1function infixToPostfix(infix: string): string {
2 const precedence: Record<string, number> = {
3 '+': 1, '-': 1,
4 '*': 2, '/': 2,
5 '^': 3
6 };
7
8 const stack: string[] = [];
9 let postfix = '';
10
11 for (const char of infix) {
12 if (/[a-zA-Z0-9]/.test(char)) {
13 postfix += char;
14 } else if (char === '(') {
15 stack.push(char);
16 } else if (char === ')') {
17 while (stack.length && stack[stack.length - 1] !== '(') {
18 postfix += stack.pop();
19 }
20 stack.pop(); // Remove '('
21 } else {
22 while (stack.length &&
23 stack[stack.length - 1] !== '(' &&
24 precedence[stack[stack.length - 1]] >= precedence[char]) {
25 postfix += stack.pop();
26 }
27 stack.push(char);
28 }
29 }
30
31 while (stack.length) {
32 postfix += stack.pop();
33 }
34
35 return postfix;
36}Deep Dive
Theoretical Foundation
Dijkstra's shunting-yard algorithm converts infix expressions to postfix (RPN) using a stack guided by operator precedence and associativity, eliminating parentheses in the output.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(n)
Applications
Industry Use
Compiler and interpreter front-ends
Calculator engines
Expression evaluators
Use Cases
Related Algorithms
Binary Search Tree (BST)
A hierarchical data structure where each node has at most two children, maintaining the property that all values in the left subtree are less than the node's value, and all values in the right subtree are greater. This ordering property enables efficient O(log n) operations on average for search, insert, and delete. BSTs form the foundation for many advanced tree structures and are fundamental in computer science.
Stack
LIFO (Last-In-First-Out) data structure with O(1) push/pop operations. Stack is a fundamental linear data structure where elements are added and removed from the same end (top). It's essential for function calls, expression evaluation, backtracking algorithms, and undo operations in applications.
Queue
FIFO (First-In-First-Out) data structure with O(1) enqueue/dequeue operations. Queue is a fundamental linear data structure where elements are added at one end (rear) and removed from the other end (front). Essential for breadth-first search, task scheduling, and buffering systems.
Hash Table (Hash Map)
A data structure that implements an associative array abstract data type, mapping keys to values using a hash function. Hash tables provide O(1) average-case time complexity for insertions, deletions, and lookups, making them one of the most efficient data structures for key-value storage. The hash function computes an index into an array of buckets from which the desired value can be found.