Reverse Polish notation is a way for a computer to remove the brackets from a mathematical equation. It is often referred to you as postfix notation because the operators moved to the end of the individual parts the equation .
It may seem unusual to do this, but in fact when we realise that a computer processes long equations using the abstract data type of a stack the reason for using postfix suddenly becomes clearer.
Infix notation is the version of arithmetic that most of us are familiar with from maths. For example we would be familiar with 2 + 4 and equate this to 6. In postfix notation this is simply represented 2 4 +.
Although this example is very simplistic, as long as we understand the rules of BIDMAS (Brackets, indices, division, multiplication, subtraction) we can convert much more complex equations directly into that postfix notation , or reverse Polish notation. In the example below we will work through the steps we would use to convert an infix notation example that uses brackets into its postfix format.
For the purposes of this example we will be using the infix equation below:
(7 – 2) + (10 /2)
The first stage is to convert the first internal set of brackets into its postfix notation, therefore turning 7 – 2 into 7 2 – :
(7 2 -) + (10 /2)
The next stage is to convert the second pair of brackets in the same way:
(7 2 -) + (10 2 /)
Now we have used the bid mass rules to convert inside the brackets we can remove middle operator to the end, thus negating the need to have the brackets at all:
7 2 - 10 2 / +
Use of a Stack
Now we have converted into reverse Polish notation , it’s useful to have some context as to why we would do this at all. At first glance, this version looks much more complicated. However, once placed onto a stack we can see how the processor would take this longer equation and accurately process it without the need for brackets and a much more complicated setup program code.
The reverse Polish notation is placed onto the stack from left to right. When an operator is identified this is applied 2 operands hello collapsing the stack down as you can see in the example below.