-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHW5.cpp
More file actions
398 lines (364 loc) · 10.7 KB
/
Copy pathHW5.cpp
File metadata and controls
398 lines (364 loc) · 10.7 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//CIS554 HW5 Due: 11:59PM, Monday, April 3.
//Jillian Handrahan
// jehandra@syr.edu
//8377009572
#include <iostream>
#include <memory>
#include <cmath>
using namespace std;
class node {
public:
int value;
shared_ptr<node> east;
weak_ptr<node> Lchild;
weak_ptr<node> Rchild;
node() {}
node(int i) { value = i; }
};
class tree {
public:
shared_ptr<node> root;
tree() { }
//tree() { root = nullptr; }
//Implement all member functions below
tree(int n, int m);//constructor for n-level tree with random values in 0 ... m-1
//n=1: 1 node; n-2: 3 nodes; n=3: 7 nodes; n=4: 15 nodes, etc.
tree(const tree& T);//copy constructor
~tree();//destructor
tree(tree&& T); //move constructor
tree(const initializer_list<int>& V);//See lecture video recording for this. preorder?
void operator = (const tree& T);//copy assignment
void operator = (tree&& T); //move assignment
tree ThreeTimes(); //return a tree with all node value being three times
shared_ptr<node> operator[](int k);//The function returns the address of kth node
//in the overall ring (or cycle). For example, when k is 0, it returns the address of the
//root; when k is 1, it returns the address of left child of root; when k is 2, it returns
//the address of right child of root, etc.
//For a level 4 tree, when k=14, it returns the address of the very last node of the tree.
friend ostream& operator<<(ostream& str, const tree& T);//
//print each level from root to leaves. At each level, print value from left to right.
void InorderPrint(shared_ptr<node> p);//in-order printing
void InorderHelp(weak_ptr<node> T);
// weak_ptr<node> Const_help(int n, int m); // helper from class
};
void tree::InorderHelp(weak_ptr<node> T) {
if (T.lock()) {
InorderHelp(T.lock()->Lchild);
cout << T.lock()->value << " ";
InorderHelp(T.lock()->Rchild);
}
}
//inorder
void tree::InorderPrint(shared_ptr<node> p) {
// cout << "Inorder" << endl;
InorderHelp(p->Lchild);
cout << p->value << " ";
InorderHelp(p->Rchild);
}
tree::tree(int n, int m) {
if (n < 1) {
return;
}
int numNodes = pow(2, n) - 1;
//root = make_shared<node>(rand() % m);
shared_ptr<node> ptr;
shared_ptr<node> newRoot = make_shared<node>(rand() % m);
root = newRoot; //root is shared ptr
ptr = root;
for(int i = 0; i < numNodes-1; i++){
shared_ptr<node> newN = make_shared<node>(rand() % m);
// weak_ptr<node> wp1{ newNode };, no... east is shared
ptr->east = newN;
ptr = ptr->east;
}
ptr->east = root;
shared_ptr<node> parent = root;
if(!root->east) return; //if only 1 level, we are done here
weak_ptr<node> child = root->east;
int level = 0;
int members = 0;
for(int i = 0; i < numNodes/2; i++){
parent->Lchild = child;
child = child.lock()->east; //lock weak
parent->Rchild = child;
child = child.lock()->east; //lock weak
parent = parent->east;
}
// root->east = root;
// root->Lchild.lock()->east = root->Rchild.lock();
// weak_ptr<node> fixL = root->Lchild;
// weak_ptr<node> fixR = root->Rchild;
// for (int i = 1; i < n-1; i++) {
// fixL = fixL.lock()->Lchild;
// fixR = fixR.lock()->Rchild;
// fixR.lock()->east = fixL.lock();
// }
// root->east = root;
cout << "Constructor" << endl;
}
//initializer list
tree::tree(const initializer_list<int>& V) {
auto p(V.begin());
int levels;
levels = 1;
int numNodes = 0;
while (p != V.end()) {
levels++;
p++;
}
numNodes = levels-1;
levels = log2(levels);
// cout << levels << endl;
if (levels < 1) {
return;
}
// cout << numNodes <<endl;
auto it(V.begin());
auto it2(V.begin());
shared_ptr<node> ptr;
shared_ptr<node> newRoot = make_shared<node>(*it);
it++;
it2++;
root = newRoot; //root is shared ptr
ptr = root;
for(int i = 0; i < numNodes-1; i++){
shared_ptr<node> newN = make_shared<node>(*it);
// weak_ptr<node> wp1{ newNode };, no east is shared
ptr->east = newN;
ptr = ptr->east;
it++;
}
ptr->east = root;
shared_ptr<node> parent = root;
it2++;
if(!root->east) return; //if only 1 level, we are done here
weak_ptr<node> child = root->east;
for(int i = 0; i < numNodes/2; i++){
parent->Lchild = child;
child = child.lock()->east; //lock weak
parent->Rchild = child;
child = child.lock()->east; //lock weak
parent = parent->east;
it2++;
}
cout << "Initializer List" << endl;
}
//copy constructor
tree::tree(const tree& T){
int numNodes;
numNodes = 1;
shared_ptr<node> count;
count = T.root;
count = count->east;
while (count!= T.root) {
count = count->east;
numNodes++;
}
// cout << numNodes << endl;
shared_ptr<node> user;
user = T.root;
shared_ptr<node> ptr;
shared_ptr<node> newRoot = make_shared<node>(user->value);
root = newRoot; //root is shared ptr
ptr = root;
user = user->east;
for(int i = 0; i < numNodes-1; i++){
shared_ptr<node> newN = make_shared<node>(user->value);
// weak_ptr<node> wp1{ newNode };, no east is shared
ptr->east = newN;
ptr = ptr->east;
user = user->east;
}
ptr->east = root;
shared_ptr<node> parent = root;
if(!root->east) return; //if only 1 level, we are done here
weak_ptr<node> child = root->east;
for(int i = 0; i < numNodes/2; i++){
parent->Lchild = child;
child = child.lock()->east; //lock weak
parent->Rchild = child;
child = child.lock()->east; //lock weak
parent = parent->east;
}
cout << "Copy Constructor" << endl;
}
//copy assignment (l-value operator)
void tree::operator = (const tree& T) {
//needs destructor, reset the L value
root.reset();
//needs new tree
int numNodes;
numNodes = 1;
shared_ptr<node> count;
count = T.root;
count = count->east;
//get the number of nodes
while (count!= T.root) {
count = count->east;
numNodes++;
}
//cout << numNodes << endl;
//user is what we use to look over the other tree (r-val T3) and add to the L-value tree (T4)
shared_ptr<node> user;
user = T.root;
shared_ptr<node> ptr;
shared_ptr<node> newRoot = make_shared<node>(user->value);
root = newRoot; //root is shared ptr
ptr = root;
user = user->east;
for(int i = 0; i < numNodes-1; i++){
shared_ptr<node> newN = make_shared<node>(user->value);
// weak_ptr<node> wp1{ newNode };, no east is shared
ptr->east = newN;
ptr = ptr->east;
user = user->east;
}
ptr->east = root;
shared_ptr<node> parent = root;
if(!root->east) return; //if only 1 level, we are done here
weak_ptr<node> child = root->east;
for(int i = 0; i < numNodes/2; i++){
parent->Lchild = child;
child = child.lock()->east; //lock weak
parent->Rchild = child;
child = child.lock()->east; //lock weak
parent = parent->east;
}
cout << "Copy Assignment" << endl;
}
//move constructor
tree::tree(tree&& T) {//move constructor
root = T.root;
T.root = nullptr;
cout << "Move Constructor" << endl;
}
//move assignment (r-value operator)
void tree::operator = (tree&& T) {
//destroy
root.reset();
cout << root.use_count() << " ROOTREF" << endl;
//pull a move constructor
root = T.root;
T.root = nullptr;
cout << "Move Assignment" << endl;
}
//destructor
tree::~tree() {
root.reset();
// if (root) {
// shared_ptr<node> del = root;
// if (del->Rchild.lock()){
// while (del) {
// del = del->Rchild.lock();
// del->east.reset();
// }
// }
// }
// if (root){
// shared_ptr<node> findDel = root;
// while (findDel->east != root) {
// findDel = findDel->east;
// }
// cout << "ref " << findDel.use_count() << endl;
// findDel.reset();
// cout << "ref 2 " << findDel.use_count() << endl;
// // cout << "ref 2 " << findDel->Lchild.use_count() << endl;
// }
cout << "Destructor" << endl;
}
tree tree::ThreeTimes() {
tree temp {*this};
//without affecting the rvalue, get the data from rvalue,
//multiply by 3, and return temp to go into the lval
//if i just change the east will it work?
shared_ptr<node> fakeEast = temp.root;
temp.root->value = temp.root->value*3;
fakeEast = fakeEast->east;
while (fakeEast!=temp.root) {
fakeEast->value = fakeEast->value*3;
fakeEast = fakeEast->east;
}
cout << "ThreeTimes" << endl;
return temp;
}
//kth node
shared_ptr<node> tree::operator[](int k) {
int count = 0;
//if we get an invalid index, return nullptr. First, find how many nodes we are dealing with.
shared_ptr<node> findLength = this->root;
while (findLength->east != root) {
findLength = findLength->east;
count++;
}
// cout << count << endl;
if (k>count || k < 0) {
return nullptr;
}
shared_ptr<node> find = this->root;
while (k) {
find = find->east;
k--;
}
cout << "Kth node" << endl;
return find;
}
ostream& operator<<(ostream& str, const tree& T){
shared_ptr<node> p = T.root;
str << p->value << " ";
p = p->east;
while (p!= T.root) {
str << p->value << " ";
p = p->east;
}
return str;
}
int main() {
tree T1(3, 10);
cout << T1 << endl;
T1.InorderPrint(T1.root);
cout << endl;
tree T2 = { 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
//
cout << T2 << endl;
T2.InorderPrint(T2.root);
cout << endl;
//
tree T3{ T2 };
cout << T3 << endl;
T3.InorderPrint(T3.root);
cout << endl;
tree T4;
T4 = T3;
cout << T4 << endl;
T4.InorderPrint(T4.root);
cout << endl;
//
T4 = T3.ThreeTimes();
cout << T4 << endl;
T4.InorderPrint(T4.root);
cout << endl;
cout << T2[14]->value << endl;
return 0;
}
//Constructor
//1 7 4 0 9 4 8
//0 7 9 1 4 4 8
//Initializer List
//10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//17 13 18 11 19 14 20 10 21 15 22 12 23 16 24
//Copy Constructor
//10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//17 13 18 11 19 14 20 10 21 15 22 12 23 16 24
//Copy Assignment
//10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//17 13 18 11 19 14 20 10 21 15 22 12 23 16 24
//Copy Constructor
//ThreeTimes
//Move Assignment
//Destructor
//30 33 36 39 42 45 48 51 54 57 60 63 66 69 72
//51 39 54 33 57 42 60 30 63 45 66 36 69 48 72
//Destructor
//Destructor
//Destructor
//Destructor