1. Relationship Between Expressions and Binary Trees
Prefix expressions correspond to the forward traversal of Binary trees;
The infix expression corresponds to the ordinal traversal of a binary tree;
Suffix expressions correspond to post-sequential traversal of Binary trees;
Ii. Generate a binary tree based on the infix expression
Infix expression: A + B * (c-d)-E/F
The middle-order traversal is: Left son, right son, and root node (kaiwii corrected to: Left son, root node, right son)
Based on the operator priority, the binary tree generation process is as follows:
1. c-d has a high priority. The root is the-operator. C and D are the right and right sons respectively.
-
|
C d
2. The next step is multiplication. The root is the * operator. The content in B and 1 is the left and right sons respectively.
*
|
B-
|
C d
3. Next is the trigger. The root is the/operator. E and F are the right and right sons.
/
|
E F
4. The next step is addition. The root is the + operator. The content in A and 2 is the left and right sons respectively.
+
|
A *
|
B-
|
C d
It also includes the tree in step 3.
5. The next step is subtraction. The root is the-operator. The two operators in 4 are the right and right sons.
-
|
+/
|
A * E F
|
B-
|
C d
3. Obtain the prefix expression by traversing the binary tree in the forward order.
The first traversal is: root node, left son, right son
The prefix expression is-+ a * B-CD/EF.
4. Obtain the suffix expression based on the Post-order traversal of the Binary Tree
Backward traversal: Left son, right son, and root node
The suffix expression is ABCD-* + EF /-
V. Summary
If you can determine a binary tree based on the prefix, infix, or suffix expression, you can generate the corresponding prefix, infix, and suffix expression.
The methods of pre-order traversal, middle-order traversal, and subsequent traversal are as follows:
1. The first order is that the root node is in the front edge, the middle order is that the root node is in the middle, and the root node is in the back
2. always start with the left son and then the right son.
Preface:Root Node, Left son, right son
Central order: Left son,Root NodeRight son
Follow-up: Left son, right son,Root Node