Expression, conversion, and calculation, use C language description --Part3

xiaoxiao2021-03-06  113

Expression, conversion, and calculation, describe the C language --Part3 (all what you should know about expressions)

Remainder

All remaining conversions can be easily done with a binary expression tree. In fact, the above two conversions, i.e., the prefix-> prefix, and prefix> suffix, can also do it with two-point tree, but skill is too strong, and the stack is much easier. Now let's continue to explain, first of all the two-point expression tree is defined.

Diced expression tree

Expression tree is a strict two-point tree, leaf node storage operand, non-leaf node storage operator, root node stores operators for calculating the results of left sub-tree and right subtroves. Once we get a certain specified tree, convert it into a variety of different representations (prefix, prefix, and suffix), and calculate their values. It is only necessary to traverse this tree. The following figure shows the corresponding expression tree of the previous expression 2 * 3 / (2-1) 5 * (4-1).

Note: The two-point expression tree does not include parentheses. When calculating the expression value with the tree structure, the structure of the expression tree itself has determined the order of the calculation.

When we traverse (first access the root node, access the left son and right son) Expression tree, the prefix representation, similar, similar, reseen, and sequential, to the left son, right son , Re-access the root node) will be represented by the suffix. What is the result of how would we get if the middle sequential traversal (access to the left son, right son, root node) For expressions that do not include parentheses, the middle sequence traversal will result in its determination form, but for those expressions between the priorities between the operator, the simple or secondary traversal can not be restored to the original Pox form.

Complete the conversion between various representations with expression trees

The prefix-> is mixed with the algorithm for use in the form of brackets in the form of contranes to change the expressions between the original priorities between the operator. 1) Creating an expression tree in the prefix expression) in the order of this tree

Prefix -> suffix 1) Creating an expression tree 2 according to prefix expression)) rear sequence traversed this tree

How easy you see this! The key is that the expression tree is created according to the prefix expression, the following algorithm completed this work:

According to the prefix expression, the algorithm for creating an expression tree 1) Infervislation 2) Inferviolation 2) Check the next element 3) If it is an operand, i) Create a leaf node, that is, no son's node (Node- > left_child = node-> right_child = NULL) II) Assign an operand to a Data component III of the leaf node III) Leaf node address into the stack 4) If an operator, i) Create a node II) Accord to this node Data component iii) Stack top node address out of the stack, assigning it to the new node's Left component, named-> LEFT_CHILD IV) Stack The top node address out of the stack, assigning it to the Right component of the new node, ie Node -> Right_Child V) New Node Address Fact 5) If the input has not ended, jump to step 26) If the input ends, the node address out of the stack is out of the stack, which is the root node address prefix to the entire tree. Algorithm reference program # 2 represented by embellishment and suffixes.

The suffix -> is in the same, the algorithm here is only applicable to the expressions of the original priorities between the operator in the form of contractions. 1) Creating an expression tree 2 by suffix expression)

Suffix -> Prefix 1) Creating an Expression Tree 2 by suffix expressions Creating an expression tree This tree is created according to the suffix expression of an expression tree 1) Check the next element 2), if it is an operand, then i) creation A leaf node, that is, no son's node (node-> left_child = node-> right_child = null) ii) Assign an operand to the leaf node III) Leaf node address into the stack 3), if it is an operator, then i) Creating a node ii) The Data component III of the node is assigned to the Node III) Stack The top node address out of the stack, and assigns it to the Right component of the new node, named-> Right_Child IV) Stack The top node address out of the stack, And given it to the new node's Left component, Node-> LEFT_CHILD V) New Node Address Fact 4) If the input has not ended, jump to step 25) If the input ends, the node address in the stack out of the stack, this address That is, the root node address of the whole tree

The above algorithm reference program # 2.

Ok, ultimately we can refer to any two expressions of the expression. Here we make a short section of the previous section: 1) Pixel-> prefix, a stack 2) is infixed -> suffix, a stack 3) prefix -> prefix, use expression tree 4) prefix -> suffix, used Expression Tree 5) Suffix -> Pixel, Expression Tree 6) Suffix -> Prefix, with Expression Tree

The problem we have now calculates how the expression is calculated. Calculating an expression contains two steps: 1) Creating a bid tree 2 corresponding to a given expression) Recursively calculates the value of this tree (according to the method, for a common infix expression we have to be influented -> Prefix or suffix -> Expression Tree -> The process of recursive value. In fact, it can also be used to calculate the value of the expression directly by the prefixed form - Translator's Note)

The following functions will calculate the value of an expression tree with a recursive method: function name: EvaluateTree, parameter: root node address

Int EvaluateTree (struct node * root)

If root! = Null

IF current Node Contains an Operator

x = evataTree (root -> left_child)

Y = evataTree (root -> right_child)

Perform Operation on x and y, specified by the

Operator and store results in z

Return Z

Else Return root-> data

Refer Program # 2 for evaction of an expression ion Tree. Reference program # 2 calculates an algorithm for the value of the expression tree.

转载请注明原文地址:https://www.9cbs.com/read-125757.html

New Post(0)