Java Advanced-Application of Data Structure "Stack"-Pre, Middle, and Postfix Expressions

Java Advanced-Application of Data Structure "Stack"-Pre, Middle, and Postfix Expressions

Everyone must be familiar with basic mathematical expressions. For example: 2+3*5-2=15. We can know the result at a glance. According to the order of priority, we calculate from left to right, and count the last in parentheses first. Calculate the calculation rules outside the brackets, etc., but there are not so many rules in the computer, but why can we get the correct result by inputting the number on the computer? This is because the computer has its special operation rules for auxiliary calculations when performing calculations, and this rule is the front, middle and suffix expression I want to talk about today.

1. Prefix expression - the operator of the prefix expression is located before the operand.

For example: (2+((3 5)-2)) is transformed into a prefix expression: + 2-* 3 5 2
where (2+((3
5)-2)) is an infix expression ( That is: an arithmetic expression that people have always used subjectively), + 2-* 3 5 2 is a prefix expression, also called a Polish expression.

Operation process: need to be assisted by the stack. For prefix expressions, the expressions are traversed from right to left. If it is a number, then it will be pushed onto the stack. If it is an operator, then two numbers will be popped out of the stack and carried out by the operator. Operate, and then continue to push the operation result into the stack until the last number is left in the stack, this number is the final operation result.

Infix expression to Polish expression:

Two stacks are needed to assist, defined here as s1 and s2. Operators (including parentheses) are stored in s1, and operands and operators (excluding parentheses) are stored in s2.
Process:
(1) Traverse the expression from right to left, if it is a number, enter s2 directly;
(2) When encountering a right parenthesis, enter s1 directly;
(3) When encountering an operator, compare it with the operator on the top of the stack in s1 , The priority is greater than or equal to the stack, or, s1 is empty, or the top element of the stack is the right parenthesis, directly enters s1; if the priority of the top element of the stack is greater than the priority of the operator currently traversed (for example: the top element of the stack is *, the current element is +). At this time, the top element of s1 is popped and pushed into s2. The new top element of s1 continues to be compared with the current operator and repeats (3);
(4) When encountered When the current element in the expression is a left parenthesis, pop the elements in s1 until the top element on the stack is a right parenthesis, and stop the pair of parentheses. Each time an element is popped, s2 will be pressed into one;
repeat the above process until the expression The formula is traversed, and finally if there are elements in s1, then all pop up and press into s2. At this time, what is stored in s2 is the prefix expression. You only need to pop out the elements in s2, and the result of the pop-up is the prefix expression.

2. Infix expressions - the intermediary of prefix and suffix expressions

Just like the example I gave above, infix expressions are familiar to us. The drag formulas learned in elementary school are not introduced here.

Three, postfix expression - reverse Polish expression (operator is located after the operand)

For example: (2+((3*5)-2)) is transformed into a suffix expression: 2 3 5 * 2-+
where 2 3 5 * 2-+ is the corresponding suffix expression, also called inverse Polish Expression, because it is just the opposite of the prefix expression.

Operation process: need to be assisted by the stack. For suffix expressions, the expression is traversed from left to right. If it is a number, then it will be pushed onto the stack. If it is an operator, then two numbers will be popped from the stack (note: at this time) The calculation order is opposite to the pop-up order), the operation is performed by the operator, and the operation result is continued to be pushed onto the stack until the last number is left in the stack, and this number is the final operation result.

The infix expression reverses the Polish expression:

Two stacks are needed to assist, defined here as s1 and s2. Operators (including parentheses) are stored in s1, and operands and operators (excluding parentheses) are stored in s2.
Process:
(1) Traverse the expression from left to right, if it is a number, enter s2 directly;
(2) When encountering a left parenthesis, enter s1 directly;
(3) When encountering an operator, compare it with the operator on the top of the stack in s1 , The priority is greater than the stack, or s1 is empty, or the top element of the stack is a left parenthesis directly into s1; if the priority of the top element of the stack is less than or equal to the operator priority currently traversed, at this time, s1 is popped The top element of the stack is pressed into s2, and the new top element in s1 continues to be compared with the current operator. Repeat (3);
(4) When the current element in the expression is the right parenthesis, pop up in s1 Element, stop until the top element of the stack is a left parenthesis, and invalidate the pair of parentheses. Every time an element is popped, s2 is pressed into one;
repeat the above process until the expression is traversed, and finally if there are elements in s1, then All pop up and press into s2. At this time, what is stored in s2 is the suffix expression. You only need to pop out the elements in s2 and take the reverse order. The final result is the suffix expression.

Guess you like

Origin blog.csdn.net/qq_45841205/article/details/114760791