# What Is Polish Notation With Example in Data Structure?

//

Heather Bennett

Polish Notation, also known as Reverse Polish Notation (RPN), is a mathematical notation in which operators are placed after their operands. It was introduced by the Polish mathematician Jan Łukasiewicz in the 1920s and gained popularity due to its simplicity and ease of evaluation. In this article, we will dive into the details of Polish Notation and explore an example to understand it better.

## Understanding Polish Notation

In traditional mathematical notation, operators are placed between their operands. For example, the expression 2 + 3 is written with the operator ‘+’ between the operands ‘2’ and ‘3’.

However, in Polish Notation, the same expression would be written as + 2 3. The operator ‘+’ comes before its operands.

This reversal of notation eliminates the need for parentheses to indicate precedence. The order of operations becomes unambiguous as each operator is immediately followed by its operands. This makes evaluation straightforward and reduces the chances of making errors.

## Example of Polish Notation

Let’s consider a simple arithmetic expression: (4 + 5) * (7 – 3). To convert this expression into Polish Notation, we follow these steps:

1. Start from left to right: Begin scanning the expression from left to right.
2. If operand: If an operand is encountered, push it onto a stack.
3. If operator: If an operator is encountered, pop two operands from the stack and perform the operation on them. Push the result back onto the stack.
4. Repeat: Repeat steps 2-3 until the entire expression is scanned.
5. Final result: The final result will be the topmost element on the stack.

Let’s apply these steps to our example expression:

1. We start scanning from left to right and encounter ‘(‘: no action.
2. We encounter ‘4’: push it onto the stack.
3. We encounter ‘+’: pop ‘4’ and push it back onto the stack. Push ‘+’ onto the stack.
4. We encounter ‘5’: push it onto the stack.
5. We encounter ‘)’: pop ‘5’ and ‘4’. Perform addition: ‘4+5 = 9’. Push ‘9’ onto the stack.
6. We encounter ‘*’: pop ‘9’ and push it back onto the stack.

Push ‘*’ onto the stack.

7. We encounter ‘(‘: no action.
8. We encounter ‘7’: push it onto the stack.

9. We encounter ‘-‘: pop ‘7’ and push it back onto the stack. Push ‘-‘ onto the stack.

10. We encounter ‘3’: push it onto the stack.

11. We encounter ‘)’: pop ‘3’ and ‘7’. Perform subtraction: ‘7-3 = 4’. Push ‘4’ onto the stack.

After scanning the entire expression, our final result will be on top of the stack, which is ‘9 * 4 = 36‘.