-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource1.h
687 lines (614 loc) · 16 KB
/
source1.h
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#ifndef __source1_H__
#define __source1_H__
#include <iostream>
#include <vector>
using namespace std;
class Triehard // compressed binary trie
// constructor should make a left and right that are empty for search to work
// magnitude is 1 for length 1, so it must be >= 1
// flag means the value ending there is being stored
{
private:
class Trienode
{
private:
int magnitude;
bool flag;
Trienode * left;
Trienode * right;
//Convenient method for printing.
//Returns a string to be able to chain
//printing more easily.
//Side is either 0 (left), or 1 (right)
string getNodeVal(int side)
{
string output = "";
string sideStr = to_string(side);
for(int i = 0; i < magnitude; ++i)
{
output += sideStr;
}
return output;
}
public:
Trienode(int magnitude, bool flag):
magnitude{magnitude}, flag{flag}
{
left = nullptr;
right = nullptr;
}
~Trienode() // Unsure about syntax, if this will play nicely with delete method
{
delete left;
delete right;
}
int getMag()
{
return magnitude;
}
bool getFlag()
{
return flag;
}
//Side is 0 (left) or 1 (right)
void print(int side, string output = "")
{
string val = getNodeVal(side);
if(getFlag())
{
cout << output + val << endl;
}
if(left != nullptr)
{
left->print(0, output + val);
}
if(right != nullptr)
{
right->print(1, output + val);
}
}
Trienode * getLeft()
{
return left;
}
Trienode * getRight()
{
return right;
}
void addMag()
{
++magnitude;
}
void subMag()
{
--magnitude;
}
void tFlag()
{
flag = true;
}
void fFlag()
{
flag = false;
}
Trienode * setLeft(int mag, bool flg)
{
left = new Trienode(mag, flg);
return left;
}
Trienode * setRight(int mag, bool flg)
{
right = new Trienode(mag, flg);
return right;
}
void copyLeft(Trienode * node)
{
left = node;
}
void copyRight(Trienode * node)
{
right = node;
}
};
Trienode * left;
Trienode * right;
public:
Triehard() // Initializes both sides as empty, but makes it searchable, mutatable
{
left = new Trienode(0, false);
right = new Trienode(0, false);
}
~Triehard() // Same concern (syntax) as nodes, don't forget to write an erase method as well, maybe an empty/wipe
{
delete left;
delete right;
}
void print()
{
//Default param arg seems to be a bit different
//than i thought. Leaving it in the node print
//function, try to fix later perhaps?
if(left != nullptr)left->print(0);
if(right != nullptr)right->print(1);
}
// build an array of what is "processed" so far. then when a flag is hit, print that array.
void mainPrint(Trienode * curnode, vector<int> * chars, int right)
{
if (!curnode) return;
int curmag = curnode->getMag();
while (curmag)
{
chars->push_back(right);
--curmag;
}
if (curnode->getFlag())
{
int len = chars->size();
for (int i = 0; i < len; i++)
{
cout << (*chars)[i] << " ";
}
cout << endl;
}
mainPrint(curnode->getLeft(), chars, 0);
mainPrint(curnode->getRight(), chars, 1);
curmag = curnode->getMag();
while (curmag)
{
chars->pop_back();
--curmag;
}
}
void myPrintIsBetterThanYoursLogan()
{
vector<int> * side1 = new vector<int>();
vector<int> * side2 = new vector<int>();
mainPrint(left, side1, 0);
mainPrint(right, side2, 1);
delete side1;
delete side2;
}
bool search(int * val, int len) // val is the string, len is its length
{
Trienode * curnode;
bool side; // represents if you are on the left or right (right being true)
if (val[0])
{
curnode = right;
side = true;
}
else
{
curnode = left;
side = false;
}
int curmag = curnode->getMag();
for (int i = 0; i < len; i++) // each iteration checks the current character for accuracy. it does not prepare for the next character like the preamble
{
if (val[i]) // if next digit is 1
{
if (side) // if you're on the right
{
if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
{
--curmag;
continue;
}
if (curnode->getRight()) // If current node is "exhausted", move on to next one
{
curnode = curnode->getRight();
curmag = curnode->getMag() - 1;
continue;
}
else
{
return false;
}
}
else
{
if (curmag)
{
return false;
}
if (curnode->getRight())
{
curnode = curnode->getRight();
curmag = curnode->getMag() - 1;
side = true;
continue;
}
else
{
return false;
}
}
}
else
{
if (!side)
{
if (curmag)
{
--curmag;
continue;
}
if (curnode->getLeft())
{
curnode = curnode->getLeft();
curmag = curnode->getMag() - 1;
continue;
}
else
{
return false;
}
}
else
{
if (curmag)
{
return false;
}
if (curnode->getLeft())
{
curnode = curnode->getLeft();
curmag = curnode->getMag() - 1;
side = false;
continue;
}
else
{
return false;
}
}
}
}
return curnode->getFlag();
}
void insert(int * val, int len) // assumes valid input
{
Trienode * curnode;
bool side; // represents if you are on the left or right (right being true)
if (val[0])
{
curnode = right;
side = true;
}
else
{
curnode = left;
side = false;
}
int curmag = curnode->getMag();
for (int i = 0; i < len; i++)
{
if (val[i]) // if next digit is 1
{
if (side) // if you're on the right
{
if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
{
--curmag;
continue;
}
else if (curnode->getRight()) // If current node is "exhausted", move on to next one
{
curnode = curnode->getRight();
curmag = curnode->getMag() - 1;
continue;
}
else if (!(curnode->getLeft())) // if there are no subtrees, just increase this node's magnitude
{
curnode->addMag();
continue;
}
else // we're on a "1" node, but it is depleted, and there is a left subtree. so, we create a new node to the right to represent this bit
{
curnode = curnode->setRight(1, false);
continue;
}
}
else // we're on a left subtree, but have a 1 coming up
{
if (curmag) // this means we have a value here, so we need to split this node up, branching to the right will be handled by following code
{
Trienode * newnode = new Trienode(0, curnode->getFlag()); // this will be the second half of the big node
curnode->fFlag(); // this and the getFlag ensure the flag is transferred properly
while (curmag) // fills newnode with the extra magnitude
{
curnode->subMag();
newnode->addMag();
}
newnode->copyLeft(curnode->getLeft()); // move the children to the bottom half
newnode->copyRight(curnode->getRight());
curnode->copyLeft(newnode); // put new node at left of curnode
curnode->copyRight(nullptr); // nothing is in the right yet
goto SKIP1; // skip next if check since we know new right is NULL
}
if (curnode->getRight()) // we can and should move to the right. once there, we sub 1 from magnitude and move on.
{
curnode = curnode->getRight();
curmag = curnode->getMag() - 1;
side = true;
continue;
}
else // we are on left, it is empty, and the right side is empty. create and set that node to curnode->
{
SKIP1:
curnode = curnode->setRight(1, false);
side = true;
continue;
}
}
}
else // next digit is a 0
{
if (!side) // on a left subtree
{
if (curmag) // still have 0s "remaining" at this node
{
--curmag;
continue;
}
else if (curnode->getLeft()) // no 0s remaining, but there is a left subtree
{
curnode = curnode->getLeft();
curmag = curnode->getMag() - 1;
continue;
}
else if (!(curnode->getRight())) // no subtrees and we're on the correct side, so add to this node's magnitude
{
curnode->addMag();
continue;
}
else // no 0s remaining, no left subtree, and we are going to add one.
{
curnode = curnode->setLeft(1, false);
continue;
}
}
else // we're on a right subtree but have a 0 coming up
{
if (curmag) // this means we have a value here, so we need to split this node up and branch to the left before this point
{
Trienode * newnode = new Trienode(0, curnode->getFlag()); // this will be the second half of the big node
curnode->fFlag(); // this and the getFlag ensure the flag is transferred properly
while (curmag) // fills newnode with the extra magnitude
{
curnode->subMag();
newnode->addMag();
}
newnode->copyLeft(curnode->getLeft()); // move the children to the bottom half
newnode->copyRight(curnode->getRight());
curnode->copyLeft(nullptr); // nothing is in the left yet
curnode->copyRight(newnode); // put new node at right of curnode
goto SKIP2; // skip next if check since we know new left is NULL
}
if (curnode->getLeft()) // we can and should move to the left. once there, we sub 1 from magnitude and move on.
{
curnode = curnode->getLeft();
curmag = curnode->getMag() - 1;
side = false;
continue;
}
else // we are on right, it is empty, and the left side is empty. create and set that node to curnode->
{
SKIP2:
curnode = curnode->setLeft(1, false);
side = false;
continue;
}
}
}
}
curnode->tFlag();
}
void cut(int * val, int len) // this is delete because i can't use delete :(
{
Trienode * curnode;
Trienode * prevnode = nullptr;
bool side; // represents if you are on the left or right (right being true)
bool side2; // previous node's side
if (val[0])
{
curnode = right;
side = true;
side2 = true;
}
else
{
curnode = left;
side = false;
side2 = false;
}
int curmag = curnode->getMag();
cout << "loop starting" << endl;
for (int i = 0; i < len; i++) // each iteration checks the current character for accuracy. it does not prepare for the next character like the preamble
{
if (val[i]) // if next digit is 1
{
if (side) // if you're on the right
{
if (curmag) // if your current magnitude is >= 1 (still info "left" in this node)
{
--curmag;
side2 = side;
continue;
}
if (curnode->getRight()) // If current node is "exhausted", move on to next one
{
prevnode = curnode;
curnode = curnode->getRight();
curmag = curnode->getMag() - 1;
side2 = side;
continue;
}
else // node doesn't exist
{
return;
}
}
else
{
if (curmag) // node doesn't exist
{
return;
}
if (curnode->getRight())
{
prevnode = curnode;
curnode = curnode->getRight();
curmag = curnode->getMag() - 1;
side = true;
side2 = false;
continue;
}
else // node doesn't exist
{
return;
}
}
}
else
{
if (!side)
{
if (curmag)
{
--curmag;
side2 = side;
continue;
}
if (curnode->getLeft())
{
prevnode = curnode;
curnode = curnode->getLeft();
curmag = curnode->getMag() - 1;
side2 = side;
continue;
}
else // node doesn't exist
{
return;
}
}
else
{
if (curmag) // node doesn't exist
{
return;
}
if (curnode->getLeft())
{
prevnode = curnode;
curnode = curnode->getLeft();
curmag = curnode->getMag() - 1;
side = false;
side2 = true;
continue;
}
else // node doesn't exist
{
return;
}
}
}
}
// at this point, we have curnode being the "end" of our value
if (!(prevnode)) // if we are deleting one of the 2 base trees
{
if (side)
{
right->fFlag();
}
else
{
left->fFlag();
}
}
else if (curnode->getLeft() && curnode->getRight()) // we have shit to both sides, just unflag
{
curnode->fFlag();
}
else if (!(curnode->getLeft()) && !(curnode->getRight())) // if our node has no children, destroy it and change parent's reference to NULL
{
if (side)
{
delete curnode;
prevnode->copyRight(nullptr);
}
else
{
delete curnode; // do not finish this step
prevnode->copyLeft(nullptr);
}
}
else if (side && curnode->getLeft() && prevnode->getLeft() && side2 && !(prevnode->getFlag()) && !(prevnode->getLeft()))
// we are on the right, we have shit to the left, and the parent has nothing to the left, and is not flagged
// this is a rare case where we do have to compress
{
while (curnode->getMag()) // Change mag to parent
{
curnode->subMag();
prevnode->addMag();
}
prevnode->copyLeft(curnode->getLeft()); // Move left side up, delete old data
curnode->copyLeft(nullptr);
prevnode->copyRight(nullptr);
delete curnode;
}
else if (!(side) && curnode->getRight() && prevnode->getRight() && !(side2) && !(prevnode->getFlag()) && !(prevnode->getRight()))
// we are on the left, we have shit to the right, and the parent has nothing to the right, and is not flagged
// the same rare case as above
{
while (curnode->getMag()) // Change mag to parent
{
curnode->subMag();
prevnode->addMag();
}
prevnode->copyRight(curnode->getRight()); // Move left side up, delete old data
curnode->copyRight(nullptr);
prevnode->copyLeft(nullptr);
delete curnode;
}
else if ((side && curnode->getLeft()) || (!(side) && curnode->getRight()))
// we are to the right and have shit to the left or vice versa
{
curnode->fFlag();
}
else if (side) // we are on the right and have shit to the right
{
Trienode * child = curnode->getRight();
while (child->getMag()) // moves magnitude from child to parent we are removing
{
child->subMag();
curnode->addMag();
}
if (child->getFlag()) curnode->tFlag(); // Sets flag based on child
else curnode->fFlag();
curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
curnode->copyRight(child->getRight());
child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
child->copyRight(nullptr);
delete child;
}
else // we are on the left and have shit to the left
{
Trienode * child = curnode->getLeft();
while (child->getMag()) // moves magnitude from child to parent we are removing
{
child->subMag();
curnode->addMag();
}
if (child->getFlag()) curnode->tFlag(); // Sets flag based on child
else curnode->fFlag();
curnode->copyLeft(child->getLeft()); // moves child's children to our parent node
curnode->copyRight(child->getRight());
child->copyLeft(nullptr); // Change child's children to null to allow for safe deletion
child->copyRight(nullptr);
delete child;
}
}
};
#endif