Conversion of a binary-tree infix expression to a suffix-expression

Source: Internet
Author: User

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

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.