As we all know, our mathematical formula is the infix expression (infix), shaped like A * (B+C), supporting parentheses to adjust the order of operations. What we usually use is infix expression.
So what is a suffix expression (postfix)?
The suffix expression (also known as inverse Polish reverse polish) is a technique that allows you to adjust the order of operations without the need for parentheses.
For example: ab+cde+**
The above is a typical suffix expression, change it to infix expression is actually (a+b) * ((d+e) *c). We do this by converting the suffix expression into infix expressions, which are consistent with our mathematical calculations, but do not conform to the way computers operate. We can understand the suffix expression through the stack in the data structure and why it conforms to computer computing.
What is Stack? In short, it is LIFO (after-in, first-out). Drawing a picture is the following
The stack operation is actually very simple, you can think of the stack as a Hanoi, you can add the Hanoi circle at the top of the tower, you can take it from one of the tops, but you can't just take the first lap from underneath. So you know, to get the first one, you have to take out all the laps, and the last circle you put in can be taken immediately. This is the essence of LIFO.
The implementation of a stack can be a linked list or an array. They have their own merits, they have a lot of comparison network, of course, I will write it later.
Now you know the stack. This structure allows you to re-understand the suffix expression again. We use the expression "ab+cde+**" as an example. We create a stack and then scan from the beginning of the expression (of course, from the left), if the data is scanned to press (push) into the stack. So after scanning two characters the stack is like this:
Then we found an operator (operator), which is a plus sign (+). For each operator we do the following:
1. Remove two operands from the stack, such as A/b here
2. Perform operator operations on these two operands, as in the A+b
3. Press the result of the operation into the stack, e.g. if the result m (m=a+b) is here, press M to the stack
This is the case in the stack:
Repeating the above procedure, you can find that the suffix expression actually calculates the expression of infix expressions.
We will find that when we scan to the end of the entire suffix expression, we just compute the entire expression, so for a suffix expression that has N characters, we calculate that the time is O (N), which is the linear time.
Well, the operation of the suffix expression is this way, now let's take a look at the operation of infix expression.
We still use (a+b) * (c* (d+e)) for example. For infix expressions, we use the method of converting to suffix expressions, and we are still using stacks to implement calculations.
First introduce the method of handwritten time conversion
For ((a+b) * (c* (d+e)), the operator is referred to the corresponding parentheses behind, to get an intermediate conversion like this
((AB)-(c (DE) +) *) *
And then we take this intermediate conversion out of parentheses and get the suffix expression
ab-cde+**
Now let's implement the conversion of the computer using the stack, the conversion rules are as follows:
1. We stipulate that the priority of + + is 1,*/priority is 2, (the opening parenthesis has a priority of 9,) and the right parenthesis has a priority of 0.
2. We stipulate that only high-priority operators can be stacked, low-priority, or directly output with the precedence operator.
3. We stipulate (after stacking, the next operator (except the right parenthesis) can be stacked.
4. After we have met, the operator of the stack pops up until the corresponding (appears.
5. We specify that the operands are output when the scan completes all elements of the pop-up stack.
We use the legend to visualize:
This is the way infix expressions are converted to postfix expressions. We find that the time complexity of the conversion is also O (N).
Next I'll introduce you to a magical data structure that can be combined into infix expressions and suffix expressions through different traversal methods. He is--two forks!
Binary tree definition: Each node has a tree structure of up to two subtrees. The knowledge about it is too much, I do not introduce each one here, you can collect relevant information from the Internet.
Let's look at the binary tree to show us The example of the formula:
Let's try the middle sequence traversal first, what is the middle sequence traversal? You can simply think of the first left, then root, and finally the right traversal order. Notice that the tree structure of thinking is recursive, so we start from the far left a traversal, the traversal order is a--"+--" b--"*--" c--"*--" d--"E. You can think of a part as a whole when traversing, so you can get the infix expression strictly following the Left-parent-right way.
Then, of course, it is the suffix expression that conforms to computer computing, and we use the post-post traversal (first left and right last root). Traversal order is a--"b--" +--"c--" d--"e--" +--"*--" *
PS: In fact, through the pre-sequence traversal, we can get the prefix expression, but this thing really does not have much use, love the study of you can search their own.
All right, my thinking is here ... Endeavors, this is further!
Thoughts on suffix expressions and infix expressions