[C ++] Expression Tree
Expression Tree:
The leaf is the operand, and the rest of the nodes are operators, which areBinary TreeOne of the applications
======================================================================== =====
An expression tree is like:
1 struct TreeNode {2 object element; 3 TreeNode * leftChild; 4 TreeNode * rightChild; 5 };
Construct an expression tree with a suffix expression:
Idea: (similar to the four arithmetic structures computed by suffix expressions)
1. Read the input strings one by one
2. If it is an operand, It is initialized as a node and then written into the stack.
3. If it is an operator, two nodes (left and right subtree of the new node) will pop up from the stack, which will be combined with the newly read operator to build a new node, and then be added to the stack.
Repeat 1 ~ 3. There is a root node of the Expression Tree in the stack.
Code implementation:
1 # include <iostream> 2 # include <string> 3 # include <stack> 4 5 using namespace std; 6 7 struct TreeNode {8 char element; 9 TreeNode * leftChild; 10 TreeNode * rightChild; 11 TreeNode (char ch, TreeNode * l, TreeNode * r) {12 element = ch; 13 leftChild = l; 14 rightChild = r; 15} 16 TreeNode () {17 element = '0'; 18 leftChild = 0; 19 rightChild = 0; 20} 21 }; 22 23 // test function -- output tree 24 void drawTree (TreeNode * root, bool infix) {25 if (infix) {26 if (root) {27 // traverse 28 drawTree (root-> leftChild, infix) in the middle order; 29 cout <root-> element; 30 drawTree (root-> rightChild, infix ); 31} 32 else return; 33} 34 else {35 if (root) {36 // traverse 37 drawTree (root-> leftChild, infix) in descending order ); 38 drawTree (root-> rightChild, infix); 39 cout <root-> element; 40} 41 else return; 42} 43} 44 45 int main () {46 string input; 47 stack <TreeNode> expressionTree; 48 while (cin> input) {49 if (input = "0") break; 50 for (int I = 0; I <input. size ();) {51 char ch = input [I ++]; 52 if (ch> = '0' & ch <= '9') {53 TreeNode leaves; 54 leaves. element = ch; 55 expressionTree. push (leaves); 56} 57 else {58 // stack, becoming the right subtree of the new node 59 TreeNode * right = new TreeNode (expressionTree. top (). element, expressionTree. top (). leftChild, expressionTree. top (). rightChild); 60 expressionTree. pop (); 61 62 // The output stack becomes 63 TreeNode on the left of the new node * left = new TreeNode (expressionTree. top (). element, expressionTree. top (). leftChild, expressionTree. top (). rightChild); 64 expressionTree. pop (); 65 66 // new node into the stack 67 TreeNode leave (ch, left, right); 68 expressionTree. push (leave); 69} 70} 71 TreeNode * root = & expressionTree. top (); 72 expressionTree. pop (); 73 drawTree (root, true); 74} 75 return 0; 76} 77 78 // concepts of NULL and 0
Limitations:
1. Assume that all inputs are valid and do not contain spaces or other illegal characters.
2. the test output function cannot restore the priority. The Expression Tree of 12 + 3 * test output will be 1 + 2*3, not (1 + 2) * 3. If necessary, you can add a priority judgment in the struct. If the operator priority of the child node is smaller than that of the parent node, the expression of the child tree needs to be placed in a bracket at the end of the output.
Some buckets:
Learning about NULL, 0, and nullptr:
1. NULL is a macro.
2. C defines NULL as # define NULL (void *) 0)
3. The definition of NULL in C ++ is 0.
# Ifdef _ cplusplus
# Define NULL 0
# Else
# Define NULL (void *) 0)
# Endif
4. Definition of nullptr in C ++ 11
1 const 2 class nullptr_t 3 { 4 public: 5 template<class T> 6 inline operator T*() const 7 { return 0; } 8 9 template<class C, class T>10 inline operator T C::*() const11 { return 0; }12 13 private:14 void operator&() const;15 } nullptr = {};
5. As defined in C ++, errors are prone to errors during function overloading.
1 //NULL 0 nullptr 2 #include <iostream> 3 #include <stdio.h> 4 5 using namespace std; 6 7 int f(void* ptr) { 8 return 2; 9 }10 11 int f(int num) {12 return 3;13 }14 15 int main() {16 int result1 = f(0);17 //int result2 = f(NULL);18 int result3 = f(nullptr);19 cout << "result1 = " << result1 << endl;20 //cout << "result2 = " << result2 << endl;21 cout << "result3 = " << result3 << endl;22 return 0;23 }
When I remove the 17-line annotator: Compilation Error
The final running result is as follows:
Note that in the C ++ 11 standard, nullptr calls are not ambiguous during heavy load, while 0 calls the int-type Parameter Function during heavy load.
In C ++, if possible, use nullptr to assign values for null pointers.
Recommended articles:
Http://www.cppblog.com/airtrack/archive/2012/09/16/190828.html