/
hw411_treeset.hpp
326 lines (276 loc) · 8.47 KB
/
hw411_treeset.hpp
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//
// GVTreeSet.hpp
#ifndef CS263_GVTreeSet
#define CS263_GVTreeSet
#include <stdexcept>
#include <ostream>
using namespace std;
namespace gvsu {
template<typename Z>
class TreeSet {
private:
class Node;
/* instance variables */
Node *rootPtr; /* poiter to the root node */
int nums;
/* inner class(es) */
struct Node { /* struct: is a class with public members */
Z data;
Node *parent; /* pointer to immediate parent */
Node *left;
Node *right;
};
public:
class const_iterator;
/* member functions */
TreeSet () {
rootPtr = nullptr;
nums = 0;
}
~TreeSet() {
if (rootPtr == nullptr)
return;
clearAll(rootPtr);
}
/* copy constructor */
TreeSet (const TreeSet& twin)
{
rootPtr = duplicate (twin.rootPtr);
}
/* move constructor */
TreeSet (TreeSet&& twin)
{
rootPtr = twin.rootPtr;
twin.rootPtr = nullptr;
}
int numberOfNodes() const {
return pnumberOfNodes(rootPtr);
}
int numberOfLeaves() const {
return pnumberOfLeaves(rootPtr);
}
int numberOfFullNodes() const {
return pnumberOfFullNodes(rootPtr);
}
/* copy assignment */
TreeSet& operator= (const TreeSet& rhs) {
clearAll(rootPtr);
rootPtr = duplicate (rhs.rootPtr);
return *this;
}
/* move assignment */
TreeSet& operator= (TreeSet&& rhs)
{
clearAll(rootPtr);
this->rootPtr = rhs.rootPtr;
rhs.rootPtr = nullptr;
return *this;
}
const Z& findMax() const {
return pfindMax(rootPtr);
}
bool contains (const Z& key)
{
return pcontains(key, rootPtr);
}
bool insert (const Z& key) {
return pinsert(key, rootPtr, nullptr);
}
void remove (const Z& key) {
premove(key, rootPtr);
}
vector<Z> levelOrder () const {
vector<Z> output;
vector<Node> q;
if(rootPtr != nullptr){
q.push_back(*rootPtr);
for(int i = 0; i < nums; ++i){
if(q.at(i).left != nullptr)
q.push_back(*(q.at(i)).left);
if(q.at(i).right != nullptr)
q.push_back(*(q.at(i)).right);
}
for(int i = 0; i < nums; ++i){
output.push_back(q.at(i).data);
}
}
return output;
}
void print (ostream& os = std::cout) const {
print (rootPtr, os, "");
}
const_iterator begin() {
return const_iterator(findMin(rootPtr));
}
const_iterator end() {
return const_iterator(nullptr);
}
private:
Node * findMin(Node *t) const{
if(t == nullptr)
return nullptr;
if(t->left == nullptr)
return t;
return findMin(t->left);
}
int pnumberOfNodes(Node *t) const {
if(t == NULL)
return 0;
else return 1+pnumberOfNodes(t->left)+ pnumberOfNodes(t->right);
}
int pnumberOfFullNodes(Node *t) const {
if(t == NULL)
return 0;
if(t->left != NULL && t->right!=NULL)
return 1+pnumberOfFullNodes(t->left)+ pnumberOfFullNodes(t->right);
else if(t->left != NULL) return pnumberOfFullNodes(t->left);
else pnumberOfFullNodes(t->right);
}
int pnumberOfLeaves(Node *t) const {
if(t == NULL)
return 0;
if(t->left == NULL && t->right==NULL)
return 1;
else
return pnumberOfLeaves(t->left)+ pnumberOfLeaves(t->right);
}
bool pinsert(const Z& key, Node * & t, Node * p){
if(t == nullptr){
t = new Node;
t->data = key;
nums++;
t->left = nullptr;
t->right = nullptr;
t->parent = p;
return true;
}
else if(key < t->data)
pinsert(key, t->left, t);
else if(key > t->data)
pinsert(key, t->right, t);
return false;
}
bool pcontains (const Z& key, Node * & t) const
{
for(int i = 0; i < nums; ++i){
if(t == nullptr)
return false;
else if(key < t->data)
return pcontains(key, t->left);
else if(key > t->data)
return pcontains(key, t->right);
return true;
}
return false;
}
void premove (const Z& key, Node * & t) {
if(t == nullptr){
return;
}
else if(key < t->data)
premove(key, t->left);
else if(key > t->data)
premove(key, t->right);
else if(t->left != nullptr && t->right != nullptr){
t->data = pfindMax(t->left);
premove(t->data, t->left);
}
else{
Node *old = t;
if(t->left != nullptr)
t = t->left;
else t = t->right;
nums--;
delete old;
}
}
const Z& pfindMax(Node *t) const {
if (rootPtr != nullptr)
{
while(t->right!=nullptr)
t=t->right;
return t->data;
}
else
throw runtime_error("can't findMax() on empty tree");
}
Node * duplicate (Node *theOtherNode)
{
if (theOtherNode != nullptr)
{
Node *n = new Node;
n->data = theOtherNode->data;
n->parent = theOtherNode->parent;
n->left = duplicate(theOtherNode->left);
n->right = duplicate(theOtherNode->right);
return n;
}
else
return nullptr;
}
void clearAll (Node* & pos)
{
if (pos != nullptr)
{
clearAll (pos->left);
clearAll (pos->right);
delete pos;
pos = nullptr;
}
}
void print (Node *pos, ostream& os, const string&& path) const
{
if (pos) {
print (pos->left, os, path + "L");
os << pos->data << " (" << path << ")" << endl;
print (pos->right, os, path + "R");
}
}
public:
class const_iterator {
public:
Node *current;
const_iterator (Node *n) : current{n}
{
}
/* copy assignment */
const_iterator operator= (const const_iterator& rhs) {
this->current = rhs.current;
return *this;
}
bool operator == (const const_iterator& rhs) const {
return this->current == rhs.current;
}
bool operator != (const const_iterator& rhs) const {
return this->current != rhs.current;
}
const_iterator& operator++ () {
if(this->current ==nullptr)
return *this;
Z data = this->current->data;
if(this->current->right == nullptr)
while(this->current != nullptr && this->current->data <= data)
this->current = this->current->parent;
else this->current = findMin(this->current->right);
return *this;
}
Node * findMin(Node *t){
if(t == nullptr)
return nullptr;
if(t->left == nullptr)
return t;
return findMin(t->left);
}
Z& operator *() const {
return current->data;
}
};
};
template <typename Z>
ostream& operator<< (ostream& os, const TreeSet<Z>& t)
{
t.print (os);
return os;
}
}
#endif