Today to talk about theoretical knowledge
Before the subsequent binary tree
The basic definition of a binary tree
Bluntly speaking, only the binary tree consists of three parts: the root, left subtree, right subtree
However, each left subtree and right subtree can also see themselves as root, so they also have their own left subtree and right subtree
Note: The left subtree and right subtree can be air
In a two-or 树前 after ordinal
In the preamble of the binary tree are three different ways to traverse
The preamble sequence: root around
In order sequence: Left Right Root
The sequence after the sequence: about root
Below is an example:
Image reproduced from code like the wind farm's blog
Order of three kinds of this example are:
Preamble: 124,563
In order: 425 613
After order: 465 231
Before use in the subsequent expression
Expression into form before the suffix
Wherein the front suffix forms are equivalent to a binary tree in the preamble of the
First, the human brain is in order, so we can be an expression manifested in the form of a binary tree, and then seek other forms of this expression.
for example:
In this expression we shall be the human brain (in order): A + B * [C- (D + F)] / E
Preamble: + A / * B-C + DFE
After the sequence: ABCDF + - * E / +
How to evaluate the expression computer
The computer is very rigid, it can not be able to understand our human brackets.
Therefore, the computer can only talk about our human infix expression into postfix notation,
Then, they are placed on the stack from front to back, if the stack is the sign, then pop it into the stack that is earlier than it was two of them and just push symbol performing operations, and the result is placed on the stack.
Also used the example just in: ABCDF + - * E / +
A stack disposed a, inject and eject process is as follows:
1.A
2.AB inject letters, no operation is performed
3.A B C
4.A B C D
5.A B C D F
6.ABCDF + inject "+" pop "+", "D", "F", and calculates D + F then injected back into the results
7.ABC D + F - inject "-" pop "-", "C", "D + F", and obtains C- (D + F) and then injected back into the results
8.AB C- (D + F) * inject "*" pop "*", "B", "C- (D + F)", and calculates B * [C- (D + F)] and then injected back into the results
9.A B*[C-(D+F)] E
10.A B*[C-(D+F)] E / 弹入“/”,弹出“/”,“B*[C-(D+F)]”,“E”,并求出B*[C-(D+F)]/E然后重新弹入结果
11.A B*[C-(D+F)]/E + 弹入“+”,弹出“+”,“A”,“B*[C-(D+F)]” ,并求出A+B*[C-(D+F)]然后重新弹入结果
运算结果为最终栈中的A+B*[C-(D+F)]
计算机通过后缀形式可以算出表达式
如果您觉得此博客还不错,别忘记点赞+关注
另:转载请加上:转自https://www.cnblogs.com/Ryan-juruo/p/11601328.html