-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.cpp
253 lines (230 loc) · 7.93 KB
/
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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include "Tree.h"
#include "expressionparser.h"
#include <stack>
#include <algorithm>
#include <set>
using namespace std;
//Constructor For Statement Parser w/ Root Head Node:
StatementParser::StatementParser(){
head = new StatementNode;
head->opType = 'v';
}
//Copy-Constructor For Statement Parser:
StatementParser::StatementParser(const StatementParser& s) {
//Copy new data
head = copy_statement(s.head);
variableNames = s.getVariableNames();
}
//Constructor To Merge Two StatementParsers:
//(i.e., Merges Two Logical Expressions Into One By Conjuncting Them Together)
StatementParser::StatementParser(const StatementParser& s1, const StatementParser& s2) {
head = new StatementNode;
head->opType = '&';
head->value = '&';
head->left = copy_statement(s1.head);
head->right = copy_statement(s2.head);
// A set is used to account for a variable that appears in both statments
set<string> variableNamesTemp;
for(string varName : s1.getVariableNames()) {
variableNamesTemp.insert(varName);
}
for(string varName : s2.getVariableNames()) {
variableNamesTemp.insert(varName);
}
for(string varName : variableNamesTemp) {
variableNames.emplace_back(varName);
}
}
//Constructor Based On String Input:
//Runs Shunting-Yard Algorithm And Sets New Value For Head.
StatementParser::StatementParser(const std::string& statement){
head = new StatementNode();
parseStatement(head, statement);
}
StatementParser::~StatementParser() {
destroy_statement(head);
}
StatementParser& StatementParser::operator=(const StatementParser& s) {
//Delete old data
destroy_statement(head);
variableNames.clear();
//Copy new data
head = copy_statement(s.head);
variableNames = s.getVariableNames();
return *this;
}
//Changes The Statement That An Underlying Expression Represents.
//Could Be Dangerous?
void StatementParser::changeHeadValue(const std::string& statement) {
head->value = statement;
}
//In-Order Traversal Printing of Expression Tree:
void StatementParser::print() const {
print(std::cout);
}
void StatementParser::print(std::ostream& o) const {
//Invokes Helper Function:
printNode(head, o);
o << std::endl;
}
//Helper Function For print():
void StatementParser::printNode(StatementNode* s, std::ostream& o) const {
if(!s){
return;
}
//If Negation Node, Append Negation Prior To Printing Value
if(s->negation){
o << "~";
}
//Case 1: Not Operator, Simply Print Value.
if(s->opType == 'v'){
o << s->value;
}
//Case 2: Operator
//Simply Print Left Child + Current Value Operator + Right Child.
else{
o << '(';
printNode(s->left, o);
o << " ";
o << s->value << " ";
printNode(s->right, o);
o << ")";
}
}
//Print The Tree In An Interesting Tree Structure
//Method To Visualize Tree In Reality
void StatementParser::printTree() const {
printTreeHelper(head, 0);
}
//Helper Function For printTree()
void StatementParser::printTreeHelper(StatementNode* s, int depth) const {
if (!s)
return;
printTreeHelper(s->right, depth+1);
std::cout << std::string(depth, '\t') << (s->negation ? '~' : ' ') << s->opType;
if (s->opType == 'v')
std::cout << ": " << (s->negation ? '~' : ' ') << s->value;
std::cout << std::endl;
printTreeHelper(s->left, depth+1);
}
//Copy Helper Function For Copying Nodes
StatementNode* StatementParser::copy_statement(StatementNode* old_node) {
if (old_node == nullptr)
return nullptr;
StatementNode* new_node = new StatementNode;
new_node->value = old_node->value;
new_node->opType = old_node->opType;
new_node->connector = old_node->connector;
new_node->negation = old_node->negation;
new_node->left = copy_statement(old_node->left);
new_node->right = copy_statement(old_node->right);
return new_node;
}
//Destroy Helper Function For Destroying Nodes
void StatementParser::destroy_statement(StatementNode*& old_node) {
if(old_node) {
destroy_statement(old_node->left);
destroy_statement(old_node->right);
delete old_node;
old_node = nullptr;
}
}
//Recursively Parses Any String Input Statment
//Previously Assumed That Statement Format = ((A) & (B)) & (~C)
//However, Now = Implementation w/ Shunting-Yard Algorithm
//Assesses The Precedence Of Operators To Construct Expression Tree.
void StatementParser::parseStatement(StatementNode*& n, const std::string& statement){
//Make A Copy Of Input Statement:
string currentInput = string(statement);
//Run Shunting-Yard Algorithm To Grab Output Reverser Polish Notation of Statement.
ExpressionParser* currentExpressionParser = new ExpressionParser();
string currentOutput = currentExpressionParser->runShuntingYardAlgorithm(currentInput);
stack<StatementNode*> convertToTree;
cout << "Output of Shunting-Yard Algorithm (Reverse Polish Notation) = " << currentOutput << endl;
//Case = Invalid Expression Communicated By Expression Parser.
if(currentOutput == "ERROR"){
return;
}
//Constant For Negation Operator Check.
char negValue = '~';
//Loop Through Current Output To Construct Expression Tree:
for(unsigned int k=0; k<currentOutput.size(); k++){
//Case 1: Operator != '~'
//Create New Node w/ Appropriate Previous Children.
if(currentExpressionParser->isOperator(currentOutput[k]) && currentOutput[k] != negValue){
//Compute First Previous Stack Value.
//= Most Recent Stack Value.
StatementNode* prevOne = NULL;
if(!convertToTree.empty()){
prevOne = convertToTree.top();
convertToTree.pop();
}
//Compute Second Previous Stack Value.
//= Second Most Recent Stack Value.
StatementNode* prevTwo = NULL;
if(!convertToTree.empty()){
prevTwo = convertToTree.top();
convertToTree.pop();
}
//Create New New Holding 'Operator' w/ Children = Two Previous Values On Stack.
StatementNode* currentNode = new StatementNode();
//Set Operator/Value = 'Operator'
currentNode->opType = currentOutput[k];
currentNode->value = currentOutput[k];
//Sanity Check: Set Default False, Negation To False.
currentNode->negation = false;
//Left Child = Eldest Recent Stack Value.
currentNode->left = prevTwo;
//Right Child = Youngest Recent Stack Value.
currentNode->right = prevOne;
//Append New Node To Stack.
convertToTree.push(currentNode);
}
//Case 2: Operator == '~'
//Do Not Create New Node.
//Rather Set Flag of Node To Be Negation = True;
else if(currentExpressionParser->isOperator(currentOutput[k]) && currentOutput[k] == negValue){
StatementNode* prevOne = NULL;
if(!convertToTree.empty()){
prevOne = convertToTree.top();
convertToTree.pop();
}
//As Long As No NULL, Add prevOne w/ Flag negation = True Back To Stack.
if(prevOne != NULL){
prevOne->negation = !(prevOne->negation);
convertToTree.push(prevOne);
}
}
//Case 3: Non-Operator Atomic Statement
else{
//Set Operator Type = 'v' To Consistently Print
//Value Set To Name of Node (e.g., 'A', 'B', ...)
//Note: v' May Indicate Leaf Node?
StatementNode* currentNode = new StatementNode();
currentNode->value = currentOutput[k];
if(find(variableNames.begin(), variableNames.end(), currentNode->value) == variableNames.end()) {
variableNames.push_back(currentNode->value);
}
currentNode->opType = 'v';
//Set Negation = False As Sanity Check Once Again.
currentNode->negation = false;
//Append New Node To Stack:
convertToTree.push(currentNode);
}
}
//Error Check For Random Error Edge Scenarios Unaccounted For:
if(convertToTree.size() != 1){
cout << "Error In Construction. Must Review Input/Output." << endl;
}
//Deletes memory
delete n;
delete currentExpressionParser;
//Set New Head Value = Top/Root of Stack.
n = convertToTree.top();
}
const vector<string>& StatementParser::getVariableNames() const {
return variableNames;
}
bool sortByVariableName(const pair<string, bool>& a, const pair<string, bool>& b) {
return a.first < b.first;
}