Prefix to infix (expression)

Source: Internet
Author: User

Problem description:

Prefix-to-infix example, with extra parentheses allowed:

* + 4 2 + 3 6 => (4 + 2) * (3 + 6)

-+/3 4 2 5 => (3/4 + 2)-5

-+ 3/4 2 5 => (3 + 4/2)-5

 

Thought 1 (recursion ):

1. Scan from left to right

2. If an operator is encountered, a recursive solution is used to return a new string. If a number is encountered, a numeric string is directly returned. For example:

Case '*':

Return "(" + exp () + "*" + exp () + ")"; // note that brackets are added to ensure that the infix expression can be correctly solved.

Default:

Return next_str;

 

Idea 2 (tree ):

1. Scan from left to right

2. Construct a subtree continuously based on operators

3. traverse the tree in the middle order and output it to obtain the infix expression.

 

Reprinted, please indicate the reference from:

Http://www.cnblogs.com/breakthings/p/4051912.html

Prefix to infix (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.