Expression Trees
Operators inside, operands outsideAn expression such as a + b * c becomes a binary tree: operators are internal nodes and operands are leaves, so the shape of the tree already says what is calculated first. Watch it built with a stack from infix, postfix or prefix, read it back with the three traversals, and evaluate it.
- Operanda number or letter — a leaf
- Operator+ − * / ^ — an internal node
- Postfixa b c * + (operator after)
- Prefix+ a * b c (operator before)
- Current
- Joined next
- Done
- New
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
Every operation, in plain steps
These are exactly the steps the visualizer animates.
Operators inside, operands outside
An expression tree is a binary tree where every INTERNAL node is an operator (+ − * / ^) and every LEAF is an operand (a number or a letter). The shape of the tree already says what must be calculated first — no brackets and no priority rules are needed to read it. To build one we keep a STACK of trees: operands become one-node trees, and an operator joins the top two.
struct Node {
char data; // an operator (+ - * / ^) or an operand (a, b, 3 ...)
struct Node *left; // the left operand (a subtree)
struct Node *right; // the right operand (a subtree)
};
// a + b * c operators are internal nodes, operands are leaves
//
// +
// / \
// a * b * c is calculated first because it is deeper
// / \
// b c
From postfix
- Read the expression from LEFT to right.
- An operand becomes a one-node tree pushed on the stack.
- An operator pops two trees: the FIRST pop is its right child, the second its left child.
- Push the new tree back. At the end exactly one tree is left.
From prefix
- The operator comes BEFORE its operands, so read from RIGHT to left.
- An operand becomes a one-node tree pushed on the stack.
- An operator pops two trees: the FIRST pop is its LEFT child, the second its right child.
- Push the new tree back. At the end exactly one tree is left.
From infix
- Use two stacks: operators, and trees.
- Operands go straight to the tree stack; "(" waits on the operator stack.
- Before an operator waits, finish every operator on top with a higher (or equal) priority.
- ")" finishes everything back to its "(". At the end finish what is left.
Priority and order
- ^ goes first, then * and /, then + and −.
- Equal priority: left to right, so a − b − c = (a − b) − c.
- The exception is ^ , which goes right to left: a ^ b ^ c = a ^ (b ^ c).
- Brackets override all of it.
Read the tree back
- Inorder (left, node, right) gives the INFIX expression.
- Preorder (node, left, right) gives the PREFIX expression.
- Postorder (left, right, node) gives the POSTFIX expression.
- Inorder loses the brackets — the tree itself remembers the order.
Evaluate the tree
- A leaf is just its value (a letter needs a value).
- An operator waits until both subtrees have values.
- Then it applies itself: left (operator) right.
- This is a postorder walk: the root holds the answer.
Pseudocode
Language-independent steps. The visualizer shows the same logic in C, Python and Java.
How fast is each operation?
n = number of tokens (and nodes), h = height. Every token is pushed and popped at most once, so building is O(n).
| Operation | Best | Average | Worst | Speed |
|---|---|---|---|---|
| Build from postfix / prefix | O(n) |
O(n) |
O(n) |
Grows with n |
| Build from infix | O(n) |
O(n) |
O(n) |
Grows with n |
| Inorder / preorder / postorder | O(n) |
O(n) |
O(n) |
Grows with n |
| Evaluate | O(n) |
O(n) |
O(n) |
Grows with n |
The three notations
| Infix | Postfix | Prefix | |
|---|---|---|---|
| a + b * c | a + b * c | a b c * + | + a * b c |
| Operator goes | between its operands | after its operands | before its operands |
| Brackets / priorities needed? | Yes | No | No |
| Read from the tree with | Inorder | Postorder | Preorder |
| Build the tree with | Two stacks (operators + trees) | One stack, scanning left to right | One stack, scanning right to left |
| Used by | People, school maths | Calculators (RPN), compilers | Lisp, some compilers |
Operator priority
| Operator | Priority | Same priority is done |
|---|---|---|
| ^ | 3 (highest) | right to left: a ^ b ^ c = a ^ (b ^ c) |
| * / | 2 | left to right: a / b * c = (a / b) * c |
| + − | 1 (lowest) | left to right: a − b − c = (a − b) − c |
Brackets always win: what is inside them is finished first.
Check your understanding
Answer each question — you will see the explanation straight away.
In an expression tree, where are the operands (numbers and letters)?
Operators need operands, so operators are internal nodes with two children, and the operands are the leaves.
The postfix expression a b c * + gives a tree. What is its root?
The LAST token of a postfix expression is the root operator: + joins a and the tree of b * c.
Building from postfix, an operator pops two trees. The FIRST tree popped becomes its…
The right operand was pushed last, so it is on top of the stack and is popped first. The second pop is the left child.
Which traversal of an expression tree gives the POSTFIX expression?
Postorder visits left, right, then the operator: operands first, operator after them — exactly postfix.
Why is a prefix expression read from RIGHT to left when building the tree?
Scanning right to left meets the operands of an operator before the operator itself, just like postfix scanning left to right.
Building from infix a + b * c, why is * combined before +?
When * arrives, + (lower priority) stays on the operator stack, so * and its operands are joined first and become a deeper subtree.
In a ^ b ^ c, which node is the right child of the root?
^ goes right to left: a ^ (b ^ c). The root ^ has a on the left and the subtree b ^ c on the right.
When can the operator at a node of an expression tree be applied?
An operator needs both operands. Evaluation is a postorder walk: children first, then the operator.
Trees quiz
Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.