-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildTree.cpp
More file actions
216 lines (150 loc) · 6.38 KB
/
BuildTree.cpp
File metadata and controls
216 lines (150 loc) · 6.38 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "BuildTree.h"
//constructor
BuildTree::BuildTree(std::string aFileName)
{
std::ifstream in(aFileName); // Creates the specified input file (determined by the passed 'aFileName')
/* Uses std::string::assign, which iterates through our file 'in' and stores it in our variable 'exp' */
exp.assign((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
// Creates new node
TreeNode *newNode = new TreeNode;
// Sets root equal to the new, empty node
root = newNode;
// Our current node is also at the root at the beginning
currentNode = root;
}//constructor
std::vector<char> BuildTree::tokenize() {
/* temporary vector, this is the vector we return after tokenizing */
std::vector<char> v;
std::cout << "The expression is: ";
/* Stores each individual token in the vector, accounting for spaces which shouldn't be included */
for (int i = 0; i < exp.length(); i++) {
if (exp[i] != ' ') {
v.push_back(exp[i]);
std::cout << exp[i];
}//if
}//for
std::cout << std::endl;
return v; // Returns our vector of chars
}//tokenize
TreeNode* BuildTree::build(std::vector<char> v) {
// Iterates through our vector of chars
for (int i = 0; i < v.size(); i++) {
// Switch statement which builds the tree depending on the type of character
switch (v[i]) {
//if opening bracket, we create a left child of our currentNode
//left Parenthesis
case '(': {
// Create new node
TreeNode* newNode = new TreeNode;
// Creating a new left child of the current node
currentNode->setLeftChildPtr(newNode);
// Set the new node (which is a child) to have a parent of the current node, which is at this stage one level higher than the new node
newNode->setParentPtr(currentNode);
// Bring the current node down a level to the new node's level
currentNode = newNode;
break;
}
//rightParenthesis
case ')': {
// Bring the current node up a level by accessing it's parent
currentNode = currentNode->getParentPtr();
break;
}
//if an operator
case '+':
case '-':
case '/':
case '*': {
// Set the value of the current node to whatever operator it is currently
currentNode->setItem(v[i]);
// Create a new node
TreeNode* newNode = new TreeNode;
// Have the current node make a new right child node
currentNode->setRightChildPtr(newNode);
// Set the new node (which is a child) to have a parent of the current node, which is at this stage one level higher than the new node
newNode->setParentPtr(currentNode);
// Bring current node down to level of the new node
currentNode = newNode;
break;
}
// If character is a number
default: {
// Set the current node to the value of the number
currentNode->setItem(v[i]);
// Bring the current node up a level by accessing it's parent
currentNode = currentNode->getParentPtr();
break;
}
}//switch
}//for size of the vector
root->setParentPtr(nullptr);
return root; // Return the root of the tree
}
int BuildTree::evaluate(TreeNode* root)
{
// If the current node is an operator, call evaluate again until we find two children that are numbers
if (root->isOperator(root->getLeftChildPtr()->getItem())) {
evaluate(root->getLeftChildPtr());
}
// If the current node is an operator, call evaluate again until we find two children that are numbers
if (root->isOperator(root->getRightChildPtr()->getItem())) {
evaluate(root->getRightChildPtr());
}
else {
// Stores value of left child
int leftValue = std::stoi(root->getLeftChildPtr()->getItem());
root->setLeftChildPtr(nullptr);
// Stores value of right child
int rightValue = std::stoi(root->getRightChildPtr()->getItem());
root->setRightChildPtr(nullptr);
// Switch statement using whatever operator is stored in current node
switch (root->getItem()[0]) {
/* Each case here does its operation specified in the current node and if we are still not at the top
of the tree, then call evaluate again, otherwise just return the number in the root node,
which would signify we are done*/
case '+':
root->setItem(std::to_string(add(leftValue, rightValue)));
if (root->getParentPtr() != nullptr)
{
evaluate(root->getParentPtr());
break;
}
else
{
return (std::stoi(root->getItem()));
break;
}
case '-':
root->setItem(std::to_string(sub(leftValue, rightValue)));
if (root->getParentPtr() != nullptr) {
evaluate(root->getParentPtr());
break;
}
else {
return (std::stoi(root->getItem()));
break;
}
case '*':
root->setItem(std::to_string(mul(leftValue, rightValue)));
if (root->getParentPtr() != nullptr) {
evaluate(root->getParentPtr());
break;
}
else {
return (std::stoi(root->getItem()));
break;
}
case '/':
root->setItem(std::to_string(div(leftValue, rightValue)));
if (root->getParentPtr() != nullptr) {
evaluate(root->getParentPtr());
break;
}
else {
return (std::stoi(root->getItem()));
break;
} // else
} // case
} // else
return (std::stoi(root->getItem())); // Return the final value in the root node
} // function