Evaluate Reverse Polish Notation
Evaluate an expression given in postfix notation using a stack.
Visualization
Interactive visualization for Evaluate Reverse Polish Notation
Operators: +, -, *, /
Interactive visualization with step-by-step execution
Implementation
1function evalRPN(tokens:string[]):number{ const st:number[]=[]; const ops:Record<string,(a:number,b:number)=>number>={"+":(a,b)=>a+b,"-":(a,b)=>a-b,"*":(a,b)=>a*b,"/":(a,b)=>Math.trunc(a/b)}; for(const t of tokens){ if(t in ops){ const b=st.pop()!, a=st.pop()!; st.push(ops[t](a,b)); } else st.push(parseFloat(t)); } return st.pop()!; }Deep Dive
Theoretical Foundation
Evaluate postfix (Reverse Polish) expressions by scanning tokens left to right. Push operands; when encountering an operator, pop required operands, apply operator, and push result.
Complexity
Time
O(n)
O(n)
O(n)
Space
O(n)
Applications
Industry Use
Calculator engines and RPN calculators
Expression evaluators/interpreters
Compiler backends and stack-based VMs
Spreadsheet formula evaluation (postfix intermediate)
Rule engines and DSL evaluation
Math training and computer science education
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.