prefix, infix, suffix expression and its evaluation

Source: Internet
Author: User

They are all notation for expressions, so they are also referred to as prefix notation, infix notation, and postfix notation. The difference between them is that the operator is relative to the position of the operand: the operator of the prefix expression precedes the operand associated with it, and the infix and suffix are the same.

Like what:

(4 + 5) x6-7 is an infix expression.

-x+ 4567 Prefix expression

+ 6x7-suffix expression

Infix expression (infix notation)

Infix expression is a general arithmetic or logical formula representation method, the operator is in infix form in the middle of the operand. Infix expression is a commonly used arithmetic representation method.

Although the human brain is easy to understand and analyze infix expression, but to the computer infix expression is very complex, so when calculating the value of an expression, it is usually necessary to first convert infix expression to prefix or suffix expression, and then evaluate. Calculating the value of a prefix or suffix expression is straightforward for a computer.

Prefix expression (prefix notation, Polish style)

The operator of the prefix expression precedes the operand.

Computer evaluation of the prefix expression:

A right-to-left scan expression, when the number is encountered, the number is pressed into the stack, when the operator is encountered, the top two number of pop-up stack, with operators to do the corresponding calculation (stack top element op sub-top element), and the results into the stack;

For example, the prefix expression "-x+ 4567":

(1) From right to left scanning, 7, 6, 5, 4 is pressed into the stack;

(2) encountered the + operator, so pop up 4 and 5 (4 is the top element of the stack, 5 is the second top element, pay attention to the suffix expression comparison), calculate the value of the 4+5, 9, and then 9 into the stack;

(3) Next is the x operator, so eject 9 and 6, calculate the 9x6=54, 54 into the stack;

(4) The last is the-operator, which calculates the value of 54-7, or 47, resulting in the final result.

As you can see, it is easy to compute the value of the prefix expression with a computer.

converts an infix expression to a prefix expression :

Follow these steps:

(1) Initialize two stacks: operator stack S1 and stack S2 for storing intermediate results;

(2) Scanning infix expression from right to left;

(3) When the operand is encountered, it is pressed into the S2;

(4) When an operator is encountered, compare its precedence with the top operator of the S1 stack:

(4-1) If the S1 is empty, or the top operator of the stack is the closing parenthesis ")", the operator is placed directly into the stack;

(4-2) Otherwise, if the priority is higher or equal than the top operator of the stack, the operator is also pressed into the S1;

(4-3) Otherwise, the S1 stack top operator pops up and presses into the S2, and again goes to (4-1) compared with the new stack top operator in S1;

(5) When the parentheses are encountered:

(5-1) If the closing parenthesis ")", then press directly into the S1;

(5-2) If the opening parenthesis "(", then pops up the operator at the top of the S1 stack, and presses into the S2 until the closing parenthesis is encountered, this pair of parentheses is discarded;

(6) Repeat steps (2) to (5) until the leftmost side of the expression;

(7) The remaining operators in the S1 are popped and pressed into the S2;

(8) The elements in the S2 pop-up and output, the result is the infix expression corresponding to the prefix expression.

For example, the process of converting infix expression "1+ ((2+3) x4)-5" to a prefix expression is as follows:

The element that is scanned

S2 (Stack bottom, top of stack)

S1 (Stack bottom, top of stack)

Description

5

5

Empty

Numbers, directly into the stack

-

5

-

S1 is empty, operator directly into stack

)

5

- )

Right parenthesis directly into the stack

4

5 4

- )

Digital direct into the stack

X

5 4

-) x

S1 stack top is the right parenthesis, directly into the stack

)

5 4

-) x)

Right parenthesis directly into the stack

3

5 4 3

-) x)

Digital

+

5 4 3

-) x) +

S1 stack top is the right parenthesis, directly into the stack

2

5 4 3 2

-) x) +

Digital

(

5 4 3 2 +

-) x

Opening parenthesis, popup operator until a closing parenthesis is encountered

(

5 4 3 2 +x

-

Ditto

+

5 4 3 2 +x

- +

Priority vs.-Same, in-stack

1

5 4 3 2 +x1

- +

Digital

Reach the left End

5 4 3 2 +x1 +-

Empty

The remaining operators in the S1

So the result is "-+ 1x+ 2 3 4 5".

suffix expression (suffix notation, inverse polish)

A suffix expression is similar to a prefix expression, except that the operator is behind an operand.

Computer evaluation of suffix expression:

Similar to prefix expressions, except that the order is from left to right:

From left to right scan the expression, when the number is encountered, the number is pressed into the stack, when the operator is encountered, the top two number of pop-up stack, with operators to do the corresponding calculation (the top element of the OP stack top elements), and the results into the stack;

For example, the suffix expression "3 4 + 5x6-":

(1) scan from left to right, press 3 and 4 into the stack;

(2) encountered the + operator, so pop up 4 and 3 (4 is the top element of the stack, 3 is the second top element, note the comparison with the prefix expression), calculate the value of the 3+4, 7, and then 7 into the stack;

(3) 5 into the stack;

(4) Next is the x operator, so eject 5 and 7, calculate the 7x5=35, 35 into the stack;

(5) 6 into the stack;

(6) The last is the-operator, which calculates the value of 35-6, or 29, resulting in the final result.

Converts an infix expression to a suffix expression:

Similar to converting to prefix expressions, follow these steps:

(1) Initialize two stacks: operator stack S1 and stack S2 for storing intermediate results;

(2) Scanning infix expression from left to right;

(3) When the operand is encountered, it is pressed into the S2;

(4) When an operator is encountered, compare its precedence with the top operator of the S1 stack:

(4-1) If the S1 is empty, or the top operator of the stack is an opening parenthesis "(", the operator is put directly into the stack;

(4-2) Otherwise, if the precedence is higher than the top of the stack operator, the operator is also pressed into the S1 (note that the conversion to a prefix expression is higher or the same priority, but this does not include the same situation);

(4-3) Otherwise, the S1 stack top operator pops up and presses into the S2, and again goes to (4-1) compared with the new stack top operator in S1;

(5) When the parentheses are encountered:

(5-1) If the opening parenthesis "(", then press directly into the S1;

(5-2) If the closing parenthesis ")", then pops the operator at the top of the S1 stack and presses it into the S2 until the opening parenthesis is encountered, discarding the parentheses at this time;

(6) Repeat steps (2) to (5) until the rightmost side of the expression;

(7) The remaining operators in the S1 are popped and pressed into the S2;

(8) Pop-up the elements in S2 and output, the reverse of the result is the infix expression corresponding to the suffix expression (converted to prefix expression without reverse order).

For example, the process of converting infix expression "1+ ((2+3) x4)-5" to a suffix expression is as follows:

The element that is scanned

S2 (Stack bottom, top of stack)

S1 (Stack bottom, top of stack)

Description

1

1

Empty

Numbers, directly into the stack

+

1

+

S1 is empty, operator directly into stack

(

1

+ (

Left parenthesis, directly into the stack

(

1

+ ( (

Ditto

2

1 2

+ ( (

Digital

+

1 2

+ ( ( +

S1 Stack top is left parenthesis, operator directly into stack

3

1 2 3

+ ( ( +

Digital

)

1 2 3 +

+ (

Closing parenthesis, popup operator until an opening parenthesis is encountered

X

1 2 3 +

+ (x

S1 Stack top is left parenthesis, operator directly into stack

4

1 2 3 + 4

+ (x

Digital

)

1 2 3 + 4x

+

Closing parenthesis, popup operator until an opening parenthesis is encountered

-

1 2 3 + 4x+

-

-Same as + priority, so eject +, press in-

5

1 2 3 + 4x+ 5

-

Digital

Reach the right End

1 2 3 + 4x+ 5-

Empty

The remaining operators in the S1

So the result is "1 2 3 + 4x+ 5-" (Note the need for reverse output).

prefix, infix, suffix expression and its evaluation

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.