Why do we need prefix, postfix notation
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In the realm of computer science and mathematics, the notation used to express mathematical expressions significantly impacts both the ease of human readability and the computational efficiency. Among these notations, infix, prefix, and postfix are the most commonly used forms. While infix notation, the traditional method utilized in arithmetic operations (`A + B`), is straightforward for humans, computers often find prefix and postfix notations more efficient for parsing and evaluation. This article delves into the reasons for adopting prefix and postfix notations, their technical advantages, and illustrative examples to build a comprehensive understanding.
Understanding Different Notations
Infix Notation
Infix notation is the familiar form used in arithmetic expressions, where operators are placed between the operands, such as `A + B` or `C * (D - E)`. While this is intuitive for human readers, it requires additional processing for computers, due to the need for operators' precedence and parentheses to be resolved.
Prefix Notation (Polish Notation)
In prefix notation, operators precede their operands. For instance, the infix expression `A + B` is written as `+ A B`. This form was introduced by Polish mathematician Jan Łukasiewicz, hence its alternative name, Polish notation.
Postfix Notation (Reverse Polish Notation)
In postfix notation, operators follow their operands. The infix expression `A + B` becomes `A B +`. This reverse form of Polish notation is particularly beneficial for stack-based computations.
Advantages of Prefix and Postfix Notations
- Operator Precedence and Parentheses Elimination:
Unlike infix notation, both prefix and postfix notations eliminate the need for parentheses. Operators' precedence is inherently clear due to their position relative to operands. This absence of brackets simplifies both manual analysis and automated computation. - Ease of Evaluation for Machines:
The straightforward parsing of prefix and postfix notations aligns well with stack data structures, facilitating efficient expression evaluation. This is why many calculators and compilers leverage these notations for backend calculations. - Consistency in Evaluation Order:
In infix expressions, operator precedence and varying associative rules can confuse or complicate evaluation orders. Prefix and postfix notations offer a consistent, one-pass evaluation process, significantly reducing ambiguity. - Reduction of Syntax Errors:
The elimination of parentheses in these notations reduces syntax mistakes during both manual calculations and computational processing, contributing to robust program execution.
Evaluating Prefix and Postfix Notations
- Prefix Evaluation:
To evaluate a prefix expression, read it right-to-left. For example, for the prefix expression `* + A B - C D`, follow these steps:- `+` operates on `A` and `B`.
- `-` operates on `C` and `D`.
- `*` operates on results of `+` and `-`.
- Postfix Evaluation:
Postfix expressions require a left-to-right scan. For the expression `A B + C D - *`:- Push `A` and `B`, then apply `+`.
- Push `C` and `D`, then apply `-`.
- Apply `*` to intermediate results.
Practical Examples
To illustrate the practical application, consider the infix expression: `3 + 4 * 5 - 6`. Converting it:
- Prefix: `- + 3 * 4 5 6`
- Postfix: `3 4 5 * + 6 -`
These formats, when processed by machines, bypass parentheses and follow clear operational hierarchies.
Conclusion
The adoption of prefix and postfix notations is driven by their inherent advantages in computational efficiency, parsing simplicity, and error reduction. While humans naturally gravitate towards infix notation for its readability, machines execute prefix and postfix forms more effectively.
Comparison Table
| Feature | Infix Notation | Prefix Notation | Postfix Notation |
| Operator Placement | Between Operands | Before Operands | After Operands |
| Parentheses Requirement | Required | Not Required | Not Required |
| Evaluation Complexity | High | Moderate | Moderate |
| Machine Efficiency | Low | High | High |
| Readability for Humans | High | Moderate | Low |
| Common Use Cases | General Arithmetic Expressions | Functional Programming Compilers | Stack-Based Calculators Compilers |
This discussion highlights why prefix and postfix notations are integral to aspects of computing, offering distinct advantages over the traditional infix approach. Their optimized evaluation processes explain their widespread application in programming languages, compiler construction, and beyond.
Related reading
- Why do we need to add a '0' null at the end of a character array in C?
- Why do we use gradient descent in linear regression?
- Why docker container is consuming lot of memory?
- Why does a heartbeat take O(log N) time to propagate
- Why does a Java class compile differently with a blank line?
- Why does adding a tokenbf_v2 index to my Clickhouse table not have any effect
- Why does adding Crossover to my Genetic Algorithm gives me worse results?
- Why does an async single task run faster than a normal single task?

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.