Postfix notation to expression tree
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction to Postfix Notation
Postfix notation, also known as Reverse Polish Notation (RPN), is a method of writing arithmetic expressions where the operator follows the operands. It eliminates the need for parentheses to define the order of operations, making it especially useful in computer science for stack-based calculations.
For example, the infix expression A + B becomes AB+ in postfix notation.
Expression Tree Basics
An expression tree is a binary tree structure used to represent expressions. Each internal node represents an operator, and each leaf node represents an operand. Traversing an expression tree collects the elements of the original expression in a specific notation—usually infix, prefix, or postfix.
The Role of Postfix Notation in Expression Trees
Postfix notation is used to simplify the construction of expression trees. Its inherent order makes it straightforward to build an expression tree using a stack-based approach:
- Operands are pushed onto a stack.
- When an operator is encountered, the required number of operands are popped from the stack.
- A subtree is formed with the operator as the root, and the operands as the children.
- The subtree is then pushed back onto the stack, treated as a single operand for further operations.
Step-by-step Conversion from Postfix Notation to an Expression Tree
Example
Consider the postfix expression AB+C*.
- Initialize an empty stack.
- Process each symbol:
- Symbol:
A- Operand, push onto stack. Stack:
[A]
- Symbol:
B- Operand, push onto stack. Stack:
[A, B]
- Symbol:
+- Operator, pop the top two operands (B and A).
- Create a new tree node with the operator as the root:
+ - Subtree:
- Push the subtree back onto the stack.
- Symbol:
C- Operand, push onto stack.
- Symbol:
*- Operator, pop the top two (subtree and operand C).
- Create a new tree node with the operator as the root:
* - Subtree:
- Push the result back onto the stack.
- Parentheses Elimination: Postfix notation does not require parentheses, resolving ambiguity and simplifying parsing.
- Efficient Compilation: Many compilers use postfix for code generation as it is closer to machine-level instructions.
- Stack-based Evaluation: It aligns naturally with stack operations, which are fundamental in computer programming and execution.
Related reading
- potential On solution to Longest Increasing Subsequence
- Pre-order to post-order traversal
- Prefix search against half a billion strings
- Prefix sums weighted by a polynomial expression, can you do faster?
- Prepare array in linear time to find k smallest elements in Ok
- Pretty-print a NumPy array without scientific notation and with given precision
- Pretty Git branch graphs
- Prevent stack trace logging for custom exception in Spring Boot application

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.