prefix, infix, suffix expression

Source: Internet
Author: User

1. Definition

The different notation for an expression differs in the position of the operator relative to the operand :

    1. Infix: operator in the middle of the operand: (3 + 4) x5-6. The way people are used to express
    2. Prefix: operator before operand: for example-X + 3 4 5 6. Computer convenient operation way: from right to left data into the stack, encountered operator, pop stack top two number operation, and the results into the stack
    3. Suffix: operator after operand: such as 3 4 + 5 x 6-. Computer convenient operation Way: From left to right data into the stack, encountered operator, pop stack top two number operations, and the results into the stack

For the convenience of computer processing, it is generally necessary to convert infix expression to prefix expression or suffix expression processing; for the convenience of people, it is generally necessary to convert prefixes and suffixes to infix expressions

2. Prefixes and suffixes converted to infix expressions

Method One: Use string to store infix expressions

The operation of the computer is consistent with the above, but can use string to store data, the original operation to the string refactoring operation, such as for the prefix expression, right-to-left, encountered operator "+", the top of the stack of two operators pop, refactor the string "(3+4)" into the stack.

For the processing of parentheses, the operator precedence is considered, and parentheses can be removed if the operator precedence in parentheses is no lower than the precedence of the operator outside the parentheses.

Method Two: Using the tree to store infix expression

Such as:

    1. Prefix to infix: The positive sequence traversal prefix expression, encountered the operator, with the operator as the root, the construction of its left and right sub-tree; Using recursion + stack
    2. Suffix to infix: reverse traversal suffix expression, encountered operator, with the operator as the root, build right left subtree; encounters a tree, finds the left subtree is empty ancestor node, constructs the left child. Using recursion + stack
3. infix expression converted to prefix or suffix expression

Method One: Bracket method

Parentheses are given for all non-parenthesized units of operator precedence, such as (((3+4) * 5)-6),

    1. Infix prefix: Move the operator to the front of the corresponding parenthesis, and then remove the parentheses. such as-(* (+ () 5) 6)------->-*+3456
    2. infix suffix: Move the operator to the back of the corresponding extension, and then remove the parentheses. e.g. ((+ 5) * 6)--------> 34+5*6-

Method Two: Using the tree to store infix expression, then the pre-sequence traversal is the prefix expression, post-order traversal is the suffix expression

Method Three: Use two stacks, Operation Fu Yi S2 and intermediate result stack S1 realize conversion

  1. infix prefix: Right-to-left traversal, encountered number, push to stack S1, encountered operator, if S2.top () is empty or closing parenthesis, Or top is lower or equal than the current operator, the operator is pushed to S2, otherwise, the S2 out of the stack, the parentheses are encountered, if the closing parenthesis, the stack, or the opening parenthesis, the S2 pops to S1 until a closing parenthesis is encountered, and the two parentheses are discarded. After the traversal, push the remaining operators of the S2 to S1 in turn. Finally, the contents of the S1 pop-up, is the prefix expression. --------principle: infix expression calculation, the first calculation of high priority, the same priority ranked in front of the first calculation. The prefix expression calculation is calculated from right to left, so the high priority is in the right place, and the same priority is ranked in the front where it ends up on the right. If the result is stored as a stack, then the high priority and equal priority rank in front: first in the S1 (the back of the stack on the right) , in the S2 after the stack ,----->top is higher than the current operator priority, if push to S2, then the order is not, that is, high priority in the S2 first into the stack, so the first pop into the stack S1 (where the same priority ranked in the front of the S2 after the stack, The last pop to S1 is the first into the stack).     The parentheses are included as a whole, a high priority overall, so the opening parenthesis this whole is complete, because the priority is high, as soon as possible into the stack s1-----If the opening parenthesis, then pop to the stack s1
  2. infix suffix: From left to right traversal, encountered number, push to queue S1; operator is encountered, if S2.top () is empty or opening parenthesis, or lower than the current operator priority, the operator is pushed to S2; otherwise, s2 out of the stack; The S2 pops to S1 until an opening parenthesis is encountered, and the two parentheses are discarded. After the traversal, push the remaining operators of the S2 to S1 in turn. Finally, the queue S1 the contents of the queue, is the suffix expression. --------Principle: basic Ibid. The prefix expression is from left to right, high priority in the left side, the same priority in front of the place in the end is also ranked on the left. If the result is stored as a queue, then the high priority and the equal priority rank in front: first in the S1 queue (out of the queue on the left), in the S2 after the stack .  ----The >top is higher or equal than the current operator, if push to S2, then the order is not correct, that is, the high priority and the same priority rank in front of the S2 first into the stack, so pop to S1. The parentheses contain a high-priority overall, because the left-to-right traversal, the opening parenthesis, the closing parenthesis, the whole is complete, as soon as possible into the queue S1------If the right parenthesis, pop to S1

Reference: http://blog.csdn.net/antineutrino/article/details/6763722

Http://blog.sina.com.cn/s/blog_4e0c21cc01010x38.html

http://blog.csdn.net/expleeve/article/details/7166521

prefix, infix, 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.