Inverse Polish notation (Reverse Polish notation) Is a representation that places operators after the operands. For example, if 3× 4 is expressed in reverse Polish format, it should be written as 3×.
In 1920, the Polish mathematician Jan ł ukasiewicz invented a mathematical notation. It places operators before all operands. This notation is also called prefix notation. In February 1954, fredrisi Bauer and izger Dickus proposed a reverse Polish structure to evaluate expressions to leverage stacks and reduce computer memory access. Afterwards, the Australian philosopher and computer scientist Charles hambrein expanded the inverse Polish notation and corresponding algorithms. So it forms a modern inverse Polish notation, also known as suffix notation.
The biggest characteristic of the reverse Polish style is its conciseness. In an inverse Polish formula, the operator on the left has the highest priority and the operator on the right has the lowest priority. Therefore, you only need to scan an inverse Polish formula from left to right and execute the calculations of each operator sequentially to get the results of the entire expression. This computing method can be easily implemented using stacks, so it is suitable for computing by computers. It can be said that the Polish style is born for computers.
Reverse Polish Interpretation
In the inverse Polish notation, operators are placed behind the operands. For example, when "three plus four" is expressed, "3 4 +" is written instead of "3 + 4 ". If there are multiple operators, the operators are placed behind the second operand, therefore, the "3-4 + 5" in the general infix notation writes "3 4-5 +" in the inverse Polish Notation: 3 minus 4, plus 5. One advantage of using the inverse Polish notation is that parentheses are not needed. For example, the "3-4*5" and "(3-4) * 5" in the infix method are different, but the former is written as "3 4 5 *-" in the suffix method *-", "3 (4 5 *) −" without ambiguity; the latter is written as "3 4-5 *".
The interpreter for the reverse polish expression is generally stack-based. The interpretation process is generally: the operand is added to the stack; when an operator is encountered, the operand goes out of the stack, evaluate the result, and import the result into the stack; when it is repeated, the top of the stack is the value of the expression. Therefore, it is easy to evaluate the inverse polish expression using the stack structure, and can quickly evaluate the value.
Note: The inverse Polish notation is not a reverse of a simple polish expression. For an operator that does not meet the exchange law, its operands are still in the regular order, for example, the inverse of the Polish notation "/6 3" is the Polish notation "6 3/" rather than "3 6/". The digital writing is also a conventional order.
Implementation of the inverse Polish Style
In the implementation of the inverse Polish formula, the biggest problem is how to change the order of operators and place the operators with high priority on the left side of the operators with low priority. For example, if there is an infix expression 2 + 3 × 4, its corresponding inverse polish expression should be 2 3 4 × +. When we convert an infix expression to an inverse polish expression, we must move the high-priority multiplication operator to the left of the low-priority addition operator without changing the operand order.
Naturally, we will think of stacks. The stack has the following features. Therefore, we can design a symbol stack to temporarily store all operators in an infix expression; design an RPN array to store reverse Polish output. Scan an infix expression from left to right and perform the following steps cyclically:
1. If a number is scanned, it is placed at the end of the RPN array.
2. If an operator is scanned. First, compare the priority of this operator and the symbol stack top operator. If the operator at the top of the symbol Stack has a low priority, the new operator is pushed into the symbol stack (so that when the symbol is output from the stack, the high priority symbol first goes out of the stack ). If the top symbol of the stack has a higher priority, the top symbol of the stack will pop up and be placed at the end of the RPN array. Then compare the priorities of the newly scanned operators and stack top operators until you find a stack top element with a lower priority than yourself or reach the bottom of the stack.
3. If expression scan ends. Place the operator order in the stack to the RPN array. In this way, a reverse polish expression is obtained.
The following is an example of a reverse-Polish generation process. The green part represents the changed part in each step. The special feature is the processing of the arc. The priority of the arc is higher than that of all operators, but the priority of all operators in the arc is higher than that of the arc. So when the top of the stack is an arc, the arc will not pop up, but will continue to press the stack. When the arc is popped up, the arc will not be placed in the reverse Polish style.
Original infix expression: 3 + 4 × (5-2)/2
Reverse polish expression: 3 4 5 2-× 2/+