# Reverse Polish notation in data structure

• Data Structures And Algorithms Reverse Polish notation (RPN) is a mathematical notation in which operators follow their operands. It is also known as postfix notation. In RPN, the operator is placed after the operands, unlike infix notation (traditional mathematical notation) where the operator is placed between the operands.

For example, in infix notation, the expression 2 + 3 is written as 2 + 3, while in RPN, the same expression is written as 2 3 +.

In data structure, RPN is commonly used in stack-based computer algorithms and compilers. The stack data structure is used to evaluate RPN expressions by pushing operands onto the stack and popping them off when an operator is encountered. The result of the operation is then pushed back onto the stack.

To evaluate an RPN expression, we start from the left and push each operand onto the stack. When an operator is encountered, we pop the top two operands from the stack, perform the operation on them, and push the result back onto the stack. This process continues until the entire expression has been evaluated, and the final result is left on the stack.

For example, let’s evaluate the RPN expression “3 4 + 5 *”:

1. Push 3 onto the stack: 
2. Push 4 onto the stack: [3, 4]
3. Pop 4 and 3 from the stack, add them, and push the result (7) onto the stack: 
4. Push 5 onto the stack: [7, 5]
5. Pop 5 and 7 from the stack, multiply them, and push the result (35) onto the stack: 
6. The final result is 35.

One advantage of using RPN is that it eliminates the need for parentheses and operator precedence, making it easier to evaluate expressions. It is also useful in situations where the order of operations needs to be strictly defined, such as in computer programs and compilers.