forked from rtrettin/cop4530
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bet.cpp
242 lines (218 loc) · 4.82 KB
/
bet.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
/* Remi Trettin COP4530 Project 4 */
#include <iostream>
#include <vector>
#include <stack>
#include <sstream>
#include "bet.h"
using namespace std;
const int PrecPD = 2;
const int PrecAM = 1;
// determine operator precedence
int precedence(string s) {
if(s == "+" || s == "-") {
return PrecAM;
}
return PrecPD;
}
// return true if s is an operator
bool isOperator(string s) {
if(s == "+" || s == "-" || s == "*" || s == "/") {
return true;
}
return false;
}
// default constructor - set root to null
BET::BET() : root(nullptr) {
}
// build a BET from a postfix expression
BET::BET(string postfix) : root(nullptr) {
stack<BinaryNode *> stk;
BinaryNode *n1, *n2;
auto temp = explode(postfix, ' ');
for(auto itr = temp.begin(); itr != temp.end(); ++itr) {
if(!isOperator(*itr)) {
root = new BinaryNode(*itr);
stk.push(root);
}else{
root = new BinaryNode(*itr);
if(stk.empty()) {
makeEmpty(root);
root = NULL;
return;
}
n1 = stk.top();
stk.pop();
n2 = stk.top();
stk.pop();
root->right = n1;
root->left = n2;
stk.push(root);
}
}
if(stk.empty()) {
makeEmpty(root);
root = NULL;
return;
}
root = stk.top();
stk.pop();
if(!stk.empty()) {
makeEmpty(root);
root = NULL;
return;
}
}
// build a BET from another BET using the private clone() function
BET::BET(const BET& bet) : root(nullptr) {
BinaryNode * copy = clone(bet.root);
root = copy;
}
// destructor - delete necessary memory
BET::~BET() {
makeEmpty(root);
root = NULL;
}
// rebuild BET from a postfix expression
bool BET::buildFromPostfix(const string postfix) {
if(!empty()) {
makeEmpty(root);
root = NULL;
}
stack<BinaryNode *> stk;
BinaryNode *n1, *n2;
auto temp = explode(postfix, ' ');
for(auto itr = temp.begin(); itr != temp.end(); ++itr) {
if(!isOperator(*itr)) {
root = new BinaryNode(*itr);
stk.push(root);
}else{
root = new BinaryNode(*itr);
if(stk.empty()) {
makeEmpty(root);
root = NULL;
return false;
}
n1 = stk.top();
stk.pop();
n2 = stk.top();
stk.pop();
root->right = n1;
root->left = n2;
stk.push(root);
}
}
if(stk.empty()) {
makeEmpty(root);
root = NULL;
return false;
}
root = stk.top();
stk.pop();
if(!stk.empty()) {
makeEmpty(root);
root = NULL;
return false;
}
return true;
}
// assignment operator
const BET & BET::operator=(const BET &rhs) {
if(!empty()) {
makeEmpty(root);
root = NULL;
}
BinaryNode * copy = clone(rhs.root);
root = copy;
return *this;
}
// public interface
void BET::printInfixExpression() {
printInfixExpression(root);
cout << endl;
}
// public interface
void BET::printPostfixExpression() {
printPostfixExpression(root);
cout << endl;
}
// return true if the BET is empty
bool BET::empty() {
if(root == nullptr) {
return true;
}
return false;
}
// public interface
size_t BET::size() {
if(root == NULL)
return 0;
else
return size(root);
}
// public interface
size_t BET::leaf_nodes() {
if(root == NULL)
return 0;
else
return leaf_nodes(root);
}
// private function to traverse the tree inorder using recursion
void BET::printInfixExpression(BinaryNode *t) {
if(t) {
printInfixExpression(t->left);
cout << t->element << " ";
printInfixExpression(t->right);
}
}
// private function to print the postfix expression using recursion
void BET::printPostfixExpression(BinaryNode *t) {
if(t) {
printPostfixExpression(t->left);
printPostfixExpression(t->right);
cout << t->element << " ";
}
}
// return the size of the tree using recursion
size_t BET::size(BinaryNode *t) {
size_t total = 1;
if(t->left != NULL) total = total + size(t->left);
if(t->right != NULL) total = total + size(t->right);
return total;
}
// return the number of leaf nodes using recursion
size_t BET::leaf_nodes(BinaryNode *t) {
if(t == NULL)
return 0;
if(t->left == NULL && t->right == NULL)
return 1;
else
return leaf_nodes(t->left) + leaf_nodes(t->right);
}
// empty the tree using recursion
void BET::makeEmpty(BinaryNode * &t) {
if(t == NULL)
return;
makeEmpty(t->left);
makeEmpty(t->right);
delete t;
t = NULL;
}
// clone the tree denoted by t using recursion
BET::BinaryNode * BET::clone(BinaryNode *t) const {
if(t == NULL) {
return t;
}
BinaryNode * temp = new BinaryNode(t->element);
temp->left = clone(t->left);
temp->right = clone(t->right);
return temp;
}
// return a vector of strings without the delimiter character
vector<string> BET::explode(string const &s, char delimiter) {
vector<string> result;
istringstream iss(s);
for(string token; getline(iss, token, delimiter);) {
result.push_back(move(token));
}
return result;
}