[Data Structure] Super detailed explanation: Conversion of arithmetic expressions into suffix expressions, prefix expressions, and construction of expression trees

Insert image description here

  • Author: A hard-working freshman majoring in computer science who loves learning and creating. Currently learning and sharing: algorithms, data structures, Java and other related knowledge.
  • Blogger's homepage: @是呶瑶子了
  • Column: [Data Structure] : This column focuses on data structure knowledge and is continuously updated. Each article has high-quality content and is easy to understand without losing depth!
  • Short-term goal: write every article in the column well

Insert image description here

1. What are infix expressions, postfix expressions, and prefix expressions?

  • Infix expression:
    Infix expression is our common mathematical expression in whichthe operator is placed between two operands, for example: 3 + 4 * 2. Infix expressions can alsoinclude parentheses to change the precedence of operators.

  • Postfix expression:
    A postfix expression (also calleda reverse Polish expression) is an expression in which an operator is placed after the operands, for example: 3 4 2 * 1 5 - 2 ^ / +. In postfix expressions,all operators have the same precedence and parentheses are no longer needed.

  • Prefix expression:
    A prefix expression (also calleda polish expression) is an expression in which an operator is placed in front of the operands, for example: + / * 3 4 2 ^ - 1 5 2. Prefix expressions and postfix expressions are evaluated in the same way, only the position of the operators is different.

Among them, postfix expressions and prefix expressions are unambiguous representation methods that can be quickly calculated through the stack . In computer science, postfix expressions are commonly used because they allow expressions to be evaluated easily using the stack.

Generally speaking, when we talk about "arithmetic expressions", unless otherwise emphasized, we refer to "infix expressions", which are the most common ones.

Calculation via postfix expressions can be performed as follows:

  1. Create an empty stack to store numbers.
  2. Scan each element of the postfix expression from left to right.
  3. If the current element is a number, push it onto the stack.
  4. If the current element is an operator, pop up the two numbers on the top of the stack in turn, perform the corresponding operation, and push the result onto the stack.
  5. When the postfix expression is scanned, the only element on the stack is the final result of the expression.

Here is an example, evaluating the postfix expression " 3 4 2 * 1 5 - 2 ^ / +":

postfix expression operate stack
3 Push on the stack 3
4 Push on the stack 3 4
2 Push on the stack 3 4 2
* Pop stack calculation and push stack 3 8
1 Push on the stack 3 8 1
5 Push on the stack 3 8 1 5
- Pop stack calculation and push stack 3 8 -4
2 Push on the stack 3 8 -4 2
^ Pop stack calculation and push stack 3 8 16
/ Pop stack calculation and push stack 0.1875
+ Pop stack calculation and push stack 3.1875

Therefore, the postfix expression "3 4 2 * 1 5 - 2 ^ / +" evaluates to 3.1875.

2. Convert infix to suffix

To convert an arithmetic expression into a reverse Polish expression , you can follow these steps:

  1. Create an empty stack to store operators.
  2. Scan each element of the infix expression from left to right.
  3. If the current element is an operand, it is output directly to the reverse Polish expression.
  4. If the current element is an operator, the following operations are performed:
    • If the operator is a left bracket "(", it is pushed onto the stack.
    • If the operator is a right bracket ")", the operators in the stack are popped and output until the left bracket is encountered.
    • If the operator is another operator, according to its priority and associativity, the operators with a higher priority than it or equal to it in the stack are popped and output , and then the operator is pushed onto the stack.
  5. After the infix expression is scanned, if there are still operators on the stack, they will be popped out and output in turn.

The final output result is the reverse Polish expression.

The process of converting the arithmetic expression a+b*(c+d/e) into a postfix expression is as follows:

infix expression operate stack postfix expression
a output null a
+ Push on the stack + a
b output + a b
* Push on the stack + * a b
( Push on the stack + * ( a b
c output + * ( a b c
+ Push on the stack + *( + a b c
d output + * ( + a b c d
/ Push on the stack + * ( + / a b c d
e output + * ( + / a b c d e
) Pop stack output + * a b c d e /
Finish Pop stack output null a b c d e / + * +

Therefore, the arithmetic expression a+b*(c+d/e) is converted into a postfix expression as abcde / + * +

3. Convert infix to prefix

  1. Flip an infix expression.
  2. Flip the direction of all brackets, that is, the left bracket becomes a right bracket, and the right bracket becomes a left bracket.
  3. Convert the flipped infix expression into a postfix expression .
  4. Flip the postfix expression again to get the prefix expression.

For example, the process of converting the infix expression a+b*cd/e into a prefix expression is as follows:

  1. Flip the infix expression: e/dc*b+a
  2. Convert the flipped infix expression into a postfix expression: ed/ cb* - a+
  3. Flip the postfix expression again to get the prefix expression: +a -*c/b de

Therefore, the infix expression a+b*cd/e is transformed into the prefix expression +a -*c/b de.

4. Use expression trees

The process of converting an arithmetic expression into a binary tree can be implemented using the expression tree method. The specific steps are as follows:

  1. Convert arithmetic expressions into postfix expressions.
  2. Scan each element of the postfix expression from left to right:
    • If the current element is an operand, create a node containing only that operand and push the node onto the stack.
    • If the current element is an operator, create a node containing only the operator, pop two nodes from the stack as its left and right child nodes, and then push the node onto the stack.
  3. The only node in the final stack is the root node, and the entire tree structure has been established.

For example, the process of converting the arithmetic expression a+b*(c+d/e) into a binary tree is as follows:

  1. Convert arithmetic expressions to postfix expressions: abcde/+*+
  2. Scan each element of the postfix expression from left to right:
    • a: Create a node containing only a and push the node onto the stack.
    • b: Create a node containing only b and push the node onto the stack.
    • c: Create a node containing only c and push the node onto the stack.
    • d: Create a node containing only d and push the node onto the stack.
    • e: Create a node containing only e and push the node onto the stack.
    • /: Create a node containing only /, pop two nodes e and d from the stack as its left and right child nodes, and then push the node into the stack.
    • +: Create a node containing only +, pop two nodes c and / from the stack as its left and right child nodes, and then push the node into the stack.
    • *: Create a node containing only *, pop two nodes b and + from the stack as its left and right child nodes, and then push the node into the stack.
    • +: Create a node containing only +, pop two nodes a and * from the stack as its left and right child nodes, and then push the node into the stack.
  3. The root node is the last remaining node + in the stack.

Therefore, the binary tree transformed from the arithmetic expression a+b*(c+d/e) is as follows:

      +
     / \
    a   *
       / \
      b   +
         / \
        c   /
           / \
          d   e

After the conversion is completed, the preorder traversal of the binary tree corresponds to the prefix expression , the inorder traversal corresponds to the infix expression , and the postorder traversal corresponds to the postfix expression.

Guess you like

Origin blog.csdn.net/Yaoyao2024/article/details/130305193