Evaluation of Expression Tree

 

Given a simple expression tree, consisting of basic binary operators i.e., + , – ,* and / and some integers, evaluate the expression tree.

Examples:

Input :
Root node of the below tree


Output :
100

Input :
Root node of the below tree


Output :
110

 

We strongly recommend you to minimize your browser and try this yourself first.

As all the operators in the tree are binary hence each node will have either 0 or 2 children. As it can be inferred from the examples above , the integer values would appear at the leaf nodes , while the interior nodes represent the operators.
To evaluate the syntax tree , a recursive approach can be followed .

Algorithm :
Let t be the syntax tree
If  t is not null then
      If t.info is operand then  
         Return  t.info
      Else
         A = solve(t.left)
         B = solve(t.right)
         return A operator B
         where operator is the info contained in t

The time complexity would be O(n), as each node is visited once. Below is a C++ program for the same:

 1 #include <iostream>
 2 #include <cstdlib>
 3 using namespace std;
 4 
 5 typedef struct node{
 6     string s;
 7     node *left;
 8     node *right;
 9     node(string x): s(x), left(NULL), right(NULL){}
10 }Node;
11 
12 // Utility function to return the integer value
13 // of a given string
14 int toInt(string s){
15     int len = s.length();
16     int num = 0;
17     for(int i = 0; i < len; i++){
18         num = num * 10 + (s[i]-'0');
19     }
20     return num;
21 }
22 
23 // Check which operator to apply
24 int calculate(const char *c, int lval, int rval){
25     int ans;
26     switch(*c){
27     case '+': ans = lval + rval; break;
28     case '-': ans = lval - rval; break;
29     case '*': ans = lval * rval; break;
30     case '/': ans = lval / rval; break;
31     }
32     return ans;
33 }
34 
35 // This function receives a node of the syntax tree
36 // and recursively evaluates it
37 int eval(Node *root){
38     // empty tree
39     if(root == NULL)
40         return 0;
41     // leaf node i.e, an integer
42     if(root->left == NULL && root->right == NULL)
43         return toInt(root->s);
44     // Evaluate left subtree
45     int lval = eval(root->left);
46     // Evaluate right subtree
47     int rval = eval(root->right);
48     return calculate((root->s).c_str(), lval, rval);
49 }
50 
51 int main()
52 {
53     // create a syntax tree
54     node *root = new node("+");
55     root->left = new node("*");
56     root->left->left = new node("5");
57     root->left->right = new node("4");
58     root->right = new node("-");
59     root->right->left = new node("100");
60     root->right->right = new node("20");
61     cout << eval(root) << endl;
62  
63     delete(root);
64  
65     root = new node("+");
66     root->left = new node("*");
67     root->left->left = new node("5");
68     root->left->right = new node("4");
69     root->right = new node("-");
70     root->right->left = new node("100");
71     root->right->right = new node("/");
72     root->right->right->left = new node("20");
73     root->right->right->right = new node("2");
74  
75     cout << eval(root);
76     system("pause");
77     return 0;
78 }

100

110

参考:http://www.geeksforgeeks.org/evaluation-of-expression-tree/

越努力,越幸运