Inverse polish expression Algorithm

Source: Internet
Author: User

The method for converting a medium-order expression into a reverse polish expression is actually very simple and also a moldingAlgorithm. It is also very easy to calculate the subvalue of a formula by using an inverse polish expression. It will be used once it is encountered.

1. convert a forward expression into a reverse polish expression.

The first thing we need to maintain is two stacks. We are now referred to as S1 and S2. The final result in S1 is the reverse polish expression, s2 will be used to temporarily store operators and the stack will be cleared when a reverse polish expression is formed. Next, let's take a look at how to form a reverse polish expression.

First, we define the priority relationship of the operator, which is sorted from small to small. The same priority is not separated by commas:(, +-, * \, Negative number ,).

Traverse a given medium-order expression from left to right, that is, our regular mathematical expression.

(1) If we encounter numbers, we can directly add them to stack S1;

(2) If left brackets are encountered, they are directly added to stack S2;

(3) If the right parenthesis is encountered, add the operator in stack S2 to stack S1 at one time until the left parenthesis is encountered, however, the left parenthesis of stack S2 is not added to stack S1;

(4) If you encounter an operator, including a single object operator and a binary operator, follow the rules below:

(1) If Stack S2 is empty at this time, the operator is directly added to stack S2;

(2) If Stack S2 is not empty at this time, the priority of the operator currently traversed is greater than or equal to the priority of the operator on the top of the stack, then directly go to stack S2;

(3) If stack S2 is not empty at this time and the priority of the operator currently traversed is smaller than that of the top-stack operator, the top-stack operator is always added to stack S1, when the stack is empty or an operator has a priority smaller than or equal to the priority of the operator currently traversed, this operator is added to stack S2;

(5) An operator still exists in stack S2 after traversing the complete middle-order expression, so these operators are added to stack S1 in turn until the stack is empty.

According to the above five operations, the stack S1 stores the reverse polish expression.


2. Use reverse Polish expressions to evaluate

Using an inverse polish expression to evaluate a calculated value is actually very simple. Because of this, the inverse polish expression is used to calculate the value of an expression in the compilation principle.

Let's take a look at how to calculate the value of a reverse polish expression:

We now maintain a data result stack S3, and we will see that the value of the final expression is stored in the stack. We traverse stack S1 from left to right and perform stack s3according to the following rules.

(1) If you encounter a number, press the number into S3;

(2) If a single object operator is encountered, take an element at the top of the S3 stack for a single object operation and press the result into Stack S3 again;

(3) If binary operators are encountered, take the two elements at the top of the S3 stack for calculation. First, the two elements at the top of the stack are left and right at the back of the stack, press the result into S3 again.

According to the above three rules, traverse the complete stack S1, then the value in S3 is the value of the inverse polish expression, therefore, we can see that it is very easy to evaluate the value using the reverse polish expression. There are only two types of operations, either directly or after the operation, the result is pushed to the stack.


So far, we have been clear about how to convert a forward expression into a reverse polish expression and how to evaluate a reverse polish expression. I do not know whether the above description is clear or incorrect. Please help me to correct it. Thank you again!


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.