Rules:
When a operand is read, it is immediately placed in the output. When the operator is read, it is not output immediately, so there must be a place first. The correct approach is to put the operators you have already seen into the stack instead of the output. When opening parentheses are encountered, we also push them into the stack. If we encounter a closing parenthesis, we will pop up the symbol in the stack and output it until we encounter the opening parenthesis, and the left parenthesis only pops up and does not output.
Rules for operators in the stack: priority must remain high to low from top to bottom. When we encounter an operator, we pop the element out of the stack until we find an element that has a lower precedence than the operator we encountered. For example: if we encounter a *, and the element at this time in the stack is +, then we can put the * into the stack, but if we encounter a + , and at this time the top element in the stack is * +, priority is greater than +, then we will first put the * + eject and output, and then + stored in the stack.
Note: Only the closing parenthesis can handle the opening parenthesis
Cases:
We put
A + b * C + (d * e + f) * g
Converted to
A b c * + D e * f + G * +
The first step
Output:a
Staging stack:
Step Two:
Output:a
Staging stack:+
Step Three:
Output:a B
Staging stack:+
Fourth Step:
Output:a B
Staging stack:+ *
Fifth Step:
Output:a b C
Staging stack:+ *
Sixth step:
Output:a b c * +
Staging stack:+
Seventh Step:
Output:a b c * +
Staging stack:+ (
Eighth Step:
Output:a b c * + D
Staging stack:+ (
Nineth Step:
Output:a b c * + D
Staging stack:+ (*
Tenth step:
Output:a b c * + D e
Staging stack: + (*
11th Step:
Output:a b c * + d e *
Staging stack: + (+
12th Step:
Output: a b c * + d e * f
Staging stack: + (+
13th Step:
Output: a b c * + D e * f +
Staging stack:+
14th Step:
Output: a b c * + D e * f +
Staging stack:+ *
15th Step:
Output: a b c * + D e * f + g
Staging stack:+ *
16th Step:
Output: a b c * + D e * f + G * +
Staging stack:
Conversion of a binary-tree infix expression to a suffix-expression