Polish notation (prefix Notation)

Source: Internet
Author: User

 

Polish notation, also called prefix notation.

 

When calculating a polish expression, you only need to find the operator of the first operation without remembering the operation hierarchy. Take the binary operation as an example. Read the expression from left to right. When an operator follows two operands, it is calculated and the result is replaced by the operator and two operands as the operand; repeat this step until all operators are processed. In the correct prefix expression, the number of operations must be one more than the operator, so an operator must be found to meet the operation conditions. In the replacement, two operations and one operator must be replaced with one, therefore, each operator and operand are reduced, and the operation can still be iterated until the entire formula is calculated. The same is true for multivariate operations. When there are enough operands, an operation is generated until iteration is completed. The condition for iteration end is guaranteed by the correctness of the expression. The following is an example of the operation sequence of each step:

The operator is placed before the operand, so it is also called prefix notation. If the number of elements (Arity) of the operator is fixed, the operators can still be parsed without parentheses.

 

Calculation method:

 

When calculating a polish expression, you only need to find the operator of the first operation without remembering the operation hierarchy. Take the binary operation as an example. Read the expression from left to right. When an operator follows two operands, it is calculated and the result is replaced by the operator and two operands as the operand; repeat this step until all operators are processed. In the correct prefix expression, the number of operations must be one more than the operator, so an operator must be found to meet the operation conditions. In the replacement, two operations and one operator must be replaced with one, therefore, each operator and operand are reduced, and the operation can still be iterated until the entire formula is calculated. The same is true for multivariate operations. When there are enough operands, an operation is generated until iteration is completed. The condition for iteration end is guaranteed by the correctness of the expression. The following is an example of the operation sequence of each step:

− × Listen 15 − 7 + 1 1 3 + 2 + 1 1 =

− × Listen 15 − 7 2 3 + 2 + 1 1 =

−×000015 5 3 + 2 + 1 1 =

−×3 3 + 2 + 1 1 =

− 9 + 2 + 1 1 =

− 9 + 2 2 =

− 9 4 =

5

Equivalent infix expression: (15 Gbit/s (7−( 1 + 1) × 3) −( 2 + (1 + 1) = 5

The following pseudocode uses a stack to obtain the prefix value. Note: Unlike the algorithms processed from left to right, the algorithms scan from right to left, but the values calculated by the two algorithms are the same. (In fact, this algorithm is equivalent to traversing the right subtree in the next traversal)

Scan the given prefix expression from right to left

For each symbol

{

If operand then

Push onto Stack

If operator then

{

Operand1 = pop Stack

Operand2 = pop Stack

Compute operand1 operator operand2

Push result onto Stack

}

}

Return top of stack as result

 

Applying this algorithm to the example abve yields the following:

− × Listen 15 − 7 + 1 1 3 + 2 + 1 1 =

− × Listen 15 − 7 + 1 1 3 + 2 2 =

− × Listen 15 − 7 + 1 1 3 4 =

−×000015 −7 2 3 4 =

−×000015 5 3 4 =

−×3 3 4 =

− 9 4 =

5

This uses the same expression as before and the algorithm above.

−×000015 −7 + 1 1 3 + 2 + 1 1

 

Token

Action

Stack

Notes

1

Operand

1

Push onto stack.

1

Operand

1 1

Push onto stack.

+

Operator

2

Pop the two operands (1, 1), calculate (1 + 1 = 2) and push onto stack.

2

Operand

2 2

Push onto stack.

+

Operator

4

Pop the two operands (2, 2), calculate (2 + 2 = 4) and push onto stack.

3

Operand

3 4

Push onto stack.

1

Operand

1 3 4

Push onto stack.

1

Operand

1 1 3 4

Push onto stack.

+

Operator

2 3 4

Pop the two operands (1, 1), calculate (1 + 1 = 2) and push onto stack.

7

Operand

7 2 3 4

Push onto stack.

Operator

5 3 4

Pop the two operands (7, 2), calculate (7−2 = 5) and push onto stack.

15

Operand

15 5 3 4

Push onto stack.

Bytes

Operator

3 3 4

Pop the two operands (15, 5), calculate (15 minutes 5 = 3) and push onto stack.

×

Operator

9 4

Pop the two operands (3, 3), calculate (3 × 3 = 9) and push onto stack.

Operator

5

Pop the two operands (9, 4), calculate (9 −4 = 5) and push onto stack.

The result is at the top of the stack.

 

Refer:

Http://en.wikipedia.org/wiki/Polish_notation and its Chinese Link

 

 

 

Polish notation (prefix Notation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.