-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConcatStringTree.h
829 lines (769 loc) · 21.6 KB
/
ConcatStringTree.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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
#ifndef __CONCAT_STRING_TREE_H__
#define __CONCAT_STRING_TREE_H__
#include "main.h"
class LitStringHash;
class ConcatStringTree;
static int NodeID = 1;
struct StringNode
{
string str;
StringNode(string _str)
{
str = _str;
}
};
class TreeNode {
public:
class ParentTree;
int leftLength;
int length;
StringNode* data;
TreeNode* left;
TreeNode* right;
int id;
ParentTree* parent;
LitStringHash* hash;
TreeNode()
{
leftLength = 0;
length = 0;
data = nullptr;
left = nullptr;
right = nullptr;
if (NodeID > 1e7) throw overflow_error("Id is overflow!");
id = NodeID;
NodeID++;
parent = new ParentTree();
hash = nullptr;
}
TreeNode(string _data)
{
leftLength = 0;
length = _data.length();
data = new StringNode(_data);
left = nullptr;
right = nullptr;
if (NodeID > 1e7) throw overflow_error("Id is overflow!");
id = NodeID;
NodeID++;
parent = new ParentTree();
hash = nullptr;
}
~TreeNode()
{
leftLength = 0;
length = 0;
delete data;
data = nullptr;
left = nullptr;
right = nullptr;
id = 0;
delete parent;
parent = nullptr;
hash = nullptr;
}
void addParent(int _id)
{
parent->insert(_id);
if (left != nullptr) left->parent->insert(_id);
if (right != nullptr) right->parent->insert(_id);
}
void delParent(int _id)
{
parent->remove(_id);
if (parent->size() == 0)
{
if (left != nullptr) left->delParent(this->id);
if (right != nullptr) right->delParent(this->id);
terminate();
}
}
void terminate()
{
delete this;
}
class ParentTree
{
protected:
struct ParentNode {
int id;
ParentNode* left;
ParentNode* right;
int height;
ParentNode()
{
id = -1;
left = nullptr;
right = nullptr;
height = 0;
}
ParentNode(int _id)
{
id = _id;
left = nullptr;
right = nullptr;
height = 0;
}
};
ParentNode* root;
int nodes;
// Calculate height
int height(ParentNode* p)
{
return (p == nullptr) ? 0 : p->height;
}
// New node creation
ParentNode* newNode(int _id)
{
ParentNode* pRet = new ParentNode(_id);
pRet->height = 1;
return pRet;
}
// Rotate right
ParentNode* rightRotate(ParentNode* y) {
ParentNode* x = y->left;
ParentNode* T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
}
// Rotate left
ParentNode* leftRotate(ParentNode* x) {
ParentNode* y = x->right;
ParentNode* T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return y;
}
// Get the balance factor of each node
int getBF(ParentNode* p)
{
return (p == nullptr) ? 0 : (height(p->left) - height(p->right));
}
// Insert a node
ParentNode* insertNode(ParentNode* root, int id) {
// Find the correct postion and insert the node
if (root == nullptr) return (newNode(id));
if (id < root->id)
{
root->left = insertNode(root->left, id);
}
else if (id > root->id)
{
root->right = insertNode(root->right, id);
}
else return root;
// Update the balance factor of each node and
// balance the tree
root->height = 1 + max(height(root->left), height(root->right));
int bf = getBF(root);
if (bf > 1) {
if (id < root->left->id) return rightRotate(root);
else if (id > root->left->id)
{
root->left = leftRotate(root->left);
return rightRotate(root);
}
}
if (bf < -1) {
if (id > root->right->id) return leftRotate(root);
else if (id < root->right->id)
{
root->right = rightRotate(root->right);
return leftRotate(root);
}
}
return root;
}
ParentNode* getLeftTree(ParentNode* root)
{
ParentNode* cur = root;
while (cur->right != nullptr) cur = cur->right;
return cur;
}
// Delete a node
ParentNode* deleteNode(ParentNode* root, int id) {
// Find the node and delete it
if (root == nullptr) return root;
if (id < root->id) root->left = deleteNode(root->left, id);
else if (id > root->id) root->right = deleteNode(root->right, id);
else
{
if ((root->left == nullptr) || (root->right == nullptr))
{
ParentNode* tmp = (root->left != nullptr) ? root->left : root->right;
if (tmp == nullptr)
{
tmp = root;
root = nullptr;
}
else *root = *tmp;
delete tmp;
}
else
{
ParentNode* tmp = getLeftTree(root->left);
root->id = tmp->id;
root->left = deleteNode(root->left, tmp->id);
}
}
if (root == nullptr) return root;
// Update the balance factor of each node and
// balance the tree
root->height = 1 + max(height(root->left), height(root->right));
int bf = getBF(root);
if (bf > 1)
{
if (getBF(root->left) >= 0) {
return rightRotate(root);
}
else {
root->left = leftRotate(root->left);
return rightRotate(root);
}
}
if (bf < -1)
{
if (getBF(root->right) <= 0) {
return leftRotate(root);
}
else {
root->right = rightRotate(root->right);
return leftRotate(root);
}
}
return root;
}
string PreOrderString(ParentNode* p) const
{
if (p == nullptr) return "";
string ret = "(id=" + to_string(p->id) + ")";
if (p->left != nullptr)
{
ret += (";" + PreOrderString(p->left));
}
if (p->right != nullptr)
{
ret += (";" + PreOrderString(p->right));
}
return ret;
}
public:
ParentTree()
{
root = nullptr;
nodes = 0;
}
int size()
{
return nodes;
}
void insert(int id)
{
root = insertNode(root, id);
nodes++;
}
void remove(int id)
{
root = deleteNode(root, id);
nodes = max(0, nodes - 1);
}
string toStringPreOrder() const
{
string ret = "ParentsTree[" + PreOrderString(root) + "]";
return ret;
}
};
};
class ConcatStringTree {
protected:
TreeNode* root;
int len;
int indexOfF(TreeNode* p, char c)
{
if(p == nullptr) return -1;
if(p->data != nullptr)
{
int idx = p->data->str.find(c);
return (idx == string::npos)? -1 : idx;
}
int idx = indexOfF(p->left, c);
if(idx == -1)
{
idx = indexOfF(p->right, c);
if(idx != -1) idx += p->leftLength;
}
return idx;
}
string PreOrderStringF(TreeNode* p) const
{
if(p->data != nullptr)
{
string ret = "(LL=0,L=" + to_string(p->length) + ",\"" + p->data->str + "\")";
return ret;
}
string ret = "(LL=" + to_string(p->leftLength) + ",L=" + to_string(p->length) + ",<NULL>)";
if(p->left != nullptr)
{
ret += (";" + PreOrderStringF(p->left));
}
if(p->right != nullptr)
{
ret += (";" + PreOrderStringF(p->right));
}
return ret;
}
string toString(TreeNode* p) const
{
if (p == nullptr) return "";
if(p->data != nullptr) return p->data->str;
string ret = ((p->left != nullptr)? toString(p->left) : "") + ((p->right != nullptr) ? toString(p->right) : "");
return ret;
}
string reverse(string str) const
{
string ret = "";
int l = str.length();
for (int i = l - 1; i >= 0; i--) ret += str[i];
return ret;
}
TreeNode* reverseNodeF(TreeNode* root) const
{
if (root == nullptr) return nullptr;
if (root->data != nullptr)
{
string ret = reverse(root->data->str);
TreeNode* p = new TreeNode(ret);
return p;
}
TreeNode* p = new TreeNode();
p->left = reverseNodeF(root->right);
p->right = reverseNodeF(root->left);
int LLen = (p->left == nullptr) ? 0 : p->left->length;
int RLen = (p->right == nullptr) ? 0 : p->right->length;
p->leftLength = LLen;
p->length = LLen + RLen;
return p;
}
TreeNode* subStringF(TreeNode* root, int from, int to) const
{
if (root == nullptr) return nullptr;
if (root->data != nullptr)
{
string ret = root->data->str.substr(from, to - from);
TreeNode* pRet = new TreeNode(ret);
return pRet;
}
TreeNode* pRet = new TreeNode();
if (from >= root->leftLength)
{
pRet->right = subStringF(root->right, from - root->leftLength, to - root->leftLength);
}
else if (to <= root->leftLength)
{
pRet->left = subStringF(root->left, from, to);
}
else
{
pRet->left = subStringF(root->left, from, root->leftLength);
pRet->right = subStringF(root->right, 0, to - root->leftLength);
}
int LLen = (pRet->left == nullptr) ? 0 : pRet->left->length;
int RLen = (pRet->right == nullptr) ? 0 : pRet->right->length;
pRet->leftLength = LLen;
pRet->length = LLen + RLen;
return pRet;
}
public:
ConcatStringTree()
{
root = nullptr;
len = 0;
}
ConcatStringTree(const char * s)
{
string tmp = "";
for (int i = 0; s[i] != '\0'; i++)
{
tmp += s[i];
}
root = new TreeNode(tmp);
len = root->length;
root->addParent(root->id);
}
ConcatStringTree(TreeNode* p)
{
root = p;
len = root->length;
root->addParent(root->id);
}
~ConcatStringTree()
{
len = 0;
if(root != nullptr) root->delParent(root->id);
root = nullptr;
}
int length() const
{
return len;
}
char get(int index)
{
if(index < 0 || index >= len) throw out_of_range("Index of string is invalid!");
TreeNode* cur = root;
while(cur->data == nullptr)
{
if(index < cur->leftLength)
{
cur = cur->left;
}
else
{
index -= cur->leftLength;
cur = cur->right;
}
}
return cur->data->str[index];
}
int indexOf(char c)
{
return indexOfF(root,c);
}
string toStringPreOrder() const
{
string ret = "ConcatStringTree[" + PreOrderStringF(root) + "]";
return ret;
}
string toString() const
{
string ret = "ConcatStringTree[\"" + toString(root) + "\"]";
return ret;
}
ConcatStringTree concat(const ConcatStringTree & otherS) const
{
TreeNode* newNode = new TreeNode();
newNode->leftLength = len;
newNode->length = len + otherS.len;
newNode->left = root;
newNode->right = otherS.root;
return ConcatStringTree(newNode);
}
ConcatStringTree subString(int from, int to) const
{
if (from < 0 || from >= len || to < 0 || to > len) throw out_of_range("Index of string is invalid");
if (from >= to) throw logic_error("Invalid range");
TreeNode* newRoot = subStringF(root, from, to);
return ConcatStringTree(newRoot);
}
ConcatStringTree reverse() const
{
TreeNode* newRoot = reverseNodeF(root);
return ConcatStringTree(newRoot);
}
int getParTreeSize(const string& query) const
{
TreeNode* cur = root;
if (cur == nullptr) throw runtime_error("Invalid query: reaching NULL");
for (auto c : query) if (c != 'l' && c != 'r') throw runtime_error("Invalid character of query");
for (auto c : query)
{
if (c == 'l')
{
cur = cur->left;
if (cur == nullptr) throw runtime_error("Invalid query: reaching NULL");
}
else if (c == 'r')
{
cur = cur->right;
if (cur == nullptr) throw runtime_error("Invalid query: reaching NULL");
}
else throw runtime_error("Invalid character of query");
}
return cur->parent->size();
}
string getParTreeStringPreOrder(const string& query) const
{
TreeNode* cur = root;
if (cur == nullptr) throw runtime_error("Invalid query: reaching NULL");
for (auto c : query) if(c != 'l' && c != 'r') throw runtime_error("Invalid character of query");
for (auto c : query)
{
if (c == 'l')
{
cur = cur->left;
if (cur == nullptr) throw runtime_error("Invalid query: reaching NULL");
}
else if (c == 'r')
{
cur = cur->right;
if (cur == nullptr) throw runtime_error("Invalid query: reaching NULL");
}
else throw runtime_error("Invalid character of query");
}
return cur->parent->toStringPreOrder();
}
};
class ReducedConcatStringTree; // forward declaration
class HashConfig {
protected:
int p;
double c1, c2;
double lambda;
double alpha;
int initSize;
friend class ReducedConcatStringTree;
friend class LitStringHash;
public:
HashConfig(int _p, double _c1, double _c2, double _lambda, double _alpha, int _initSize)
{
p = _p;
c1 = _c1;
c2 = _c2;
lambda = _lambda;
alpha = _alpha;
initSize = _initSize;
}
};
class LitStringHash {
public:
struct LitString
{
TreeNode* node;
int num;
LitString()
{
node = nullptr;
num = 0;
}
};
protected:
int p, size, curSize, lastIdx;
double c1, c2, lambda, alpha;
LitString* HashMap;
public:
LitStringHash(const HashConfig& hashConfig)
{
p = hashConfig.p;
size = hashConfig.initSize;
curSize = 0;
c1 = hashConfig.c1;
c2 = hashConfig.c2;
lambda = hashConfig.lambda;
alpha = hashConfig.alpha;
lastIdx = -1;
HashMap = nullptr;
}
~LitStringHash()
{
p = 0;
size = 0;
curSize = 0;
c1 = 0;
c2 = 0;
lambda = 0;
alpha = 0;
lastIdx = 0;
HashDelete();
}
void HashInit()
{
HashMap = new LitString[size];
}
void HashDelete()
{
delete[] HashMap;
HashMap = nullptr;
}
int hp(int idx, int i)
{
int newIdx = idx + c1 * i;
newIdx %= size;
newIdx += c2 * pow(i, 2);
newIdx %= size;
return newIdx;
}
int hashFunction(string s)
{
int idx = 0;
for (int i = 0; i < s.length(); i++)
{
idx += s[i] * pow(p, i);
idx %= size;
}
return idx;
}
TreeNode* addNode(string s, int idx)
{
if (HashMap == nullptr) return nullptr;
for (int i = 0; i < size; i++)
{
int newIdx = hp(idx, i);
if (HashMap[newIdx].node == nullptr) continue;
else if (HashMap[newIdx].node->data->str == s)
{
HashMap[newIdx].num++;
return HashMap[newIdx].node;
}
}
return nullptr;
}
void insert(TreeNode* _node, int idx)
{
if (HashMap == nullptr) HashInit();
idx = probing(idx);
HashMap[idx].node = _node;
HashMap[idx].num = 1;
lastIdx = idx;
curSize++;
if ((double)(curSize * 1.0 / size * 1.0) > lambda) rehash();
}
int probing(int idx)
{
for (int i = 0; i < size; i++)
{
int newIdx = hp(idx, i);
if (HashMap[newIdx].node == nullptr) return newIdx;
}
throw runtime_error("No possible slot");
}
void rehash()
{
int newSize = size * alpha;
int tmp = size;
size = newSize;
LitString* NewHashMap = new LitString[newSize];
for (int i = 0; i < tmp; i++)
{
if (HashMap[i].node == nullptr) continue;
int idx = hashFunction(HashMap[i].node->data->str);
bool found = 0;
for (int j = 0; j < size; j++)
{
int newIdx = hp(idx, j);
if (HashMap[newIdx].node == nullptr)
{
idx = newIdx;
found = 1;
break;
}
}
if(!found) throw runtime_error("No possible slot");
NewHashMap[idx] = HashMap[i];
lastIdx = idx;
}
HashDelete();
HashMap = NewHashMap;
}
int delNode(string s)
{
if (HashMap == nullptr) return -1;
int idx = hashFunction(s);
int newIdx = idx;
int ret = -1;
for (int i = 0; i < size; i++)
{
newIdx = idx + c1 * i;
newIdx %= size;
newIdx += c2 * pow(i, 2);
newIdx %= size;
if (HashMap[newIdx].node == nullptr) continue;
else if (HashMap[newIdx].node->data->str == s)
{
HashMap[newIdx].num--;
if (HashMap[newIdx].num == 0)
{
HashMap[newIdx].node = nullptr;
curSize--;
}
ret = HashMap[newIdx].num;
break;
}
}
if(curSize == 0) HashDelete();
return ret;
}
int getLastInsertedIndex() const
{
return lastIdx;
}
string toString() const
{
string ret = "LitStringHash[";
if (HashMap != nullptr)
{
for (int i = 0; i < size; i++)
{
ret += "(" + ((HashMap[i].node != nullptr) ? ("litS=\"" + HashMap[i].node->data->str + "\"") : "") + ")" + ((i == size - 1) ? "" : ";");
}
}
ret += "]";
return ret;
}
};
class ReducedConcatStringTree : public ConcatStringTree {
protected:
LitStringHash* litStringHash;
public:
ReducedConcatStringTree(const char* s, LitStringHash* litStringHash)
{
this->litStringHash = litStringHash;
string tmp = "";
for (int i = 0; s[i] != '\0'; i++)
{
tmp += s[i];
}
int idx = litStringHash->hashFunction(tmp);
root = litStringHash->addNode(tmp, idx);
if (root == nullptr)
{
root = new TreeNode(tmp);
litStringHash->insert(root, idx);
root->addParent(root->id);
}
len = root->length;
}
ReducedConcatStringTree(TreeNode* p)
{
root = p;
len = root->length;
root->addParent(root->id);
}
~ReducedConcatStringTree()
{
len = 0;
if (root != nullptr)
{
if (root->data == nullptr || litStringHash->delNode(root->data->str) <= 0)
{
root->delParent(root->id);
}
}
root = nullptr;
}
ReducedConcatStringTree concat(const ReducedConcatStringTree& otherS) const
{
TreeNode* newNode = new TreeNode();
newNode->leftLength = len;
newNode->length = len + otherS.len;
newNode->left = root;
newNode->right = otherS.root;
return ReducedConcatStringTree(newNode);
}
ReducedConcatStringTree subString(int from, int to) const
{
if (from < 0 || from >= len || to < 0 || to > len) throw out_of_range("Index of string is invalid");
if (from >= to) throw logic_error("Invalid range");
TreeNode* newRoot = subStringF(root, from, to);
return ReducedConcatStringTree(newRoot);
}
ReducedConcatStringTree reverse() const
{
TreeNode* newRoot = reverseNodeF(root);
return ReducedConcatStringTree(newRoot);
}
};
#endif // __CONCAT_STRING_TREE_H__