Evaluation of Expression Tree - GeeksforGeeks
Given a simple expression tree, consisting of basic binary operators i.e., + , – ,* and / and some integers, evaluate the expression tree.
Read full article from Evaluation of Expression Tree - GeeksforGeeks
Given a simple expression tree, consisting of basic binary operators i.e., + , – ,* and / and some integers, evaluate the expression tree.
int
eval(node* root)
{
// empty tree
if
(!root)
return
0;
// leaf node i.e, an integer
if
(!root->left && !root->right)
return
toInt(root->info);
// Evaluate left subtree
int
l_val = eval(root->left);
// Evaluate right subtree
int
r_val = eval(root->right);
// Check which operator to apply
if
(root->info==
"+"
)
return
l_val+r_val;
if
(root->info==
"-"
)
return
l_val-r_val;
if
(root->info==
"*"
)
return
l_val*r_val;
return
l_val/r_val;
}