Stacks and Their Operations
A postfix expression places each operator after the two operands it acts on, so the order of symbols alone determines the computation and no parentheses are needed.
Predicting a Result Before Calculating
Before any rules are introduced, try a short challenge. The expression below is written in postfix form, which means the operator comes after the values it works on. Read it from left to right and decide what single number it produces. Make a prediction first, and only then check the answer.
What do you think happens?
What single number does the postfix expression 3 4 + 2 * produce?
Reveal answer
14
Read left to right: 3 and 4 are operands, so they are saved. The plus sign acts on the two most recent values, 3 and 4, giving 7. The 2 is saved next to that 7, and the times sign acts on 7 and 2, giving 14. This is the same as the infix expression (3 + 4) * 2.
If your guess was 10 or 24, the mistake most likely came from applying the operators in the wrong order or from combining the wrong numbers. The rest of this article explains the procedure that avoids both errors. Notice that the answer depends only on the order of the symbols. No parentheses were needed to tell the reader what to compute.
Where Operators Sit in Infix and Postfix
The notation most people learn first is infix. In infix, the operator sits between the two values it combines, as in 6 - 3. This is comfortable for people to read, but it is awkward for a simple machine. Grouping and precedence rules require the reader to look ahead to decide what to compute first.
Postfix notation rearranges the same ingredients so that the operator trails the two values it acts on. The expression 6 - 3 becomes 6 3 -. The expression (6 - 3) * 4 becomes 6 3 - 4 *. The second form needs no parentheses at all, because the order of the symbols alone tells the reader what to compute.
| Expression | Infix form | Postfix form | Parentheses needed in postfix? |
|---|---|---|---|
| Simple subtraction | 6 - 3 | 6 3 - | No |
| Subtraction then multiplication | (6 - 3) * 4 | 6 3 - 4 * | No |
The same calculations written in infix and postfix notation. In postfix, each operator follows the two operands it acts on.
In postfix, each operator applies to the two items immediately before it in the expression. Those two items may themselves be results of earlier operators, so the arrangement of symbols already encodes the order of operations.
Why a Stack Fits Postfix Evaluation
Suppose a program reads a calculation one symbol at a time and must produce a single number at the end. When it sees a number, it cannot yet know what will be done with it, because the operator that uses it may appear much later. So the program has to hold on to that number. Holding values until they are needed, and always reaching for the most recently held value first, is exactly the behavior of a stack.
Postfix expressions can be processed in one pass from left to right. Nothing needs to be reread or reordered. This is what makes them convenient to process with a stack.
The Two-Case Evaluation Procedure
Every token in a postfix expression falls into one of two cases, and the procedure depends only on which case applies. Tokens are read in order from left to right.
- 1If the token is an operand, push it onto the stack so it is saved for later.
- 2If the token is an operator, pop the top value, then pop the next value below it.
- 3Apply the operator to those two values, using the second popped value as the left operand and the first popped value as the right operand.
- 4Push the result back onto the stack so a later operator can use it.
- 5When the expression is finished, the single remaining value on the stack is the answer.
Operand Order for Subtraction and Division
For addition and multiplication, the order of the two popped values does not change the result. For subtraction and division it does. For these operators, the first popped value is the right operand, and the second popped value is the left operand. The pop order must be respected, or the answer will be wrong.
3- The first popped value, 3, becomes the right operand.
- The second popped value, 6, becomes the left operand.
- Reversing the pop order would compute 3 - 6, which is -3.
Tracing a Postfix Expression Through the Stack
The diagram below traces the postfix expression 6 3 - 4 *, which is the postfix form of (6 - 3) * 4. Each node shows a token and the stack contents after that token has been processed. The stack is written from bottom to top, so the rightmost value is the top.
Evaluating 6 3 - 4 *
Evaluate the postfix expression 6 3 - 4 * by pushing operands and applying each operator to the two most recent values.
- 1Read 6
6 is an operand, so push it. Stack: 6.
- 2Read 3
3 is an operand, so push it. Stack: 6 3.
- 3Read minus
The operator pops 3 first, which is the right operand, then pops 6, the left operand. It computes 6 - 3 = 3 and pushes 3. Stack: 3.
- 4Read 4
4 is an operand, so push it. Stack: 3 4.
- 5Read times
The operator pops 4 and then 3, computes 3 * 4 = 12, and pushes 12. Stack: 12.
- 6End of expression
One value remains on the stack, so that value is the result.
12
A Longer Expression With Nested Results
The next expression is not from the source. It shows how an operator can use a result produced by an earlier operator. Read it carefully, and watch which values are on the stack when each operator arrives. The postfix expression is 5 1 2 + 4 * + 3 -.
Evaluating 5 1 2 + 4 * + 3 -
Evaluate the postfix expression 5 1 2 + 4 * + 3 - using a stack.
- 1Push 5, 1, 2
All three are operands, so they are saved. Stack bottom to top: 5 1 2.
- 2Plus
The operator pops 2 (right) and 1 (left), computes 1 + 2 = 3, and pushes 3. Stack: 5 3.
- 3Push 4
4 is an operand, so push it. Stack: 5 3 4.
- 4Times
The operator pops 4 (right) and 3 (left), computes 3 * 4 = 12, and pushes 12. Stack: 5 12.
- 5Plus
The operator pops 12 (right) and 5 (left), computes 5 + 12 = 17, and pushes 17. Stack: 17.
- 6Push 3, then minus
Push 3, giving stack 17 3. The minus pops 3 (right) and 17 (left), computes 17 - 3 = 14, and pushes 14. Stack: 14.
14
Notice that the first plus did not use the 5 at all. It combined the two most recent values, 1 and 2. The 5 stayed on the stack, waiting until the second plus was reached. This is the behavior that makes a stack the right structure: values wait exactly until the operator that needs them appears.
Mistakes Beginners Make With Postfix
Common mistake Reading postfix from right to left
What it looks likeReading 6 3 - as 3 minus 6
Why it failsPostfix is processed in one pass from left to right. Reading in the other direction changes which value is pushed first.
Do this insteadRead tokens in order from left to right, pushing operands as you go.
Common mistake Popping the operands in the wrong order for subtraction or division
What it looks likeComputing 3 - 6 for the expression 6 3 -
Why it failsThe first popped value is the right operand. Treating it as the left operand swaps the two values.
Do this insteadPop the top value first and treat it as the right operand; the value below it is the left operand.
Common mistake Applying an operator to the wrong pair of values
What it looks likeIn 5 1 2 + 4 * + 3 -, using the 5 in the first plus
Why it failsEach operator applies only to the two most recent values on the stack, not to any earlier value.
Do this insteadBefore applying an operator, identify the top two values and use only those.
Common mistake Forgetting to push the result of an operator back onto the stack, so later operators cannot use it
Practice Predicting and Evaluating
Predict first, then evaluate: what single number does the postfix expression 8 2 / 1 + produce? Write down your prediction, then use the stack procedure to check it. Remember that for division the first popped value is the right operand.
Hints
- Push 8 and 2, then find the division operator.
- The division pops 2 as the right operand and 8 as the left operand.
- After the division, push 1 and apply the plus to the two most recent values.
Key Takeaways
- In postfix notation, each operator follows the two operands it acts on, so the order of the symbols alone determines the computation and no parentheses are needed.
- A postfix expression is evaluated from left to right in a single pass: operands are pushed onto a stack, and each operator pops two values, applies itself, and pushes the result back.
- The two values an operator uses are always the two most recently saved values, which is why a stack's last-in, first-out behavior fits this procedure.
- For subtraction and division, the first popped value is the right operand, so the pop order must be respected.
- Predicting a result before computing it works by tracking which operator will be applied last and which values are waiting on the stack.