-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathEvaluation of Expression Tree.cpp
122 lines (103 loc) · 2.41 KB
/
Evaluation of Expression Tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Input :
// Root node of the below tree
// Output :
// 100
// Input :
// Root node of the below tree
// Output :
// 110
// 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
// C++ program to evaluate an expression tree
#include <bits/stdc++.h>
using namespace std;
// Class to represent the nodes of syntax tree
class node
{
public:
string info;
node *left = NULL, *right = NULL;
node(string x)
{
info = x;
}
};
// Utility function to return the integer value
// of a given string
int toInt(string s)
{
int num = 0;
// Check if the integral value is
// negative or not
// If it is not negative, generate the number
// normally
if(s[0]!='-')
for (int i=0; i<s.length(); i++)
num = num*10 + (int(s[i])-48);
// If it is negative, calculate the +ve number
// first ignoring the sign and invert the
// sign at the end
else
for (int i=1; i<s.length(); i++)
{
num = num*10 + (int(s[i])-48);
num = num*-1;
}
return num;
}
// This function receives a node of the syntax tree
// and recursively evaluates it
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;
}
//driver function to check the above program
int main()
{
// create a syntax tree
node *root = new node("+");
root->left = new node("*");
root->left->left = new node("5");
root->left->right = new node("-4");
root->right = new node("-");
root->right->left = new node("100");
root->right->right = new node("20");
cout << eval(root) << endl;
delete(root);
root = new node("+");
root->left = new node("*");
root->left->left = new node("5");
root->left->right = new node("4");
root->right = new node("-");
root->right->left = new node("100");
root->right->right = new node("/");
root->right->right->left = new node("20");
root->right->right->right = new node("2");
cout << eval(root);
return 0;
}