-
Notifications
You must be signed in to change notification settings - Fork 2
/
SuffixTree.js
161 lines (125 loc) · 3.38 KB
/
SuffixTree.js
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
/*jshint esnext: true, -W097 */
/* globals console */
'use strict';
class Node {
constructor() {
this.transition = {};
this.suffixLink = null;
}
addTransition(node, [start, end], t) {
this.transition[t] = [node, start, end];
}
isLeaf() {
return Object.keys(this.transition).length === 0;
}
}
export class SuffixTree {
constructor() {
this.text = '';
this.root = new Node();
this.bottom = new Node();
this.root.suffixLink = this.bottom;
this.s = this.root;
this.k = 0;
this.i = -1;
}
addString(str) {
var temp = this.text.length;
this.text += (temp) ? '⚇' + str : str;
var[s, k, i] = [this.s, this.k, this.i];
for (var j = temp; j < this.text.length; j++) {
this.bottom.addTransition(this.root, [j, j], this.text[j]);
}
while(this.text[i+1]) {
i++;
[s, k] = this.update(s, [k, i]);
[s, k] = this.canonize(s, [k, i]);
}
[this.s, this.k, this.i] = [s, k, i];
return this;
}
update(s, [k, i]) {
var oldr = this.root;
var [endPoint, r] = this.testAndSplit(s, [k, i - 1], this.text[i]);
while(!endPoint) {
r.addTransition(new Node(), [i, Infinity], this.text[i]);
if(oldr != this.root) {
oldr.suffixLink = r;
}
oldr = r;
[s, k] = this.canonize(s.suffixLink, [k, i - 1]);
[endPoint, r] = this.testAndSplit(s, [k, i - 1], this.text[i]);
}
if(oldr != this.root) {
oldr.suffixLink = s;
}
return [s, k];
}
testAndSplit(s, [k, p], t) {
if(k <= p) {
var [s2, k2, p2] = s.transition[this.text[k]];
if(t == this.text[k2 + p - k + 1]) {
return [true, s];
} else {
var r = new Node();
s.addTransition(r, [k2, k2 + p - k], this.text[k2]);
r.addTransition(s2, [k2 + p - k + 1, p2], this.text[k2 + p - k + 1]);
return [false, r];
}
} else {
if(!s.transition[t])
return [false, s];
else
return [true, s];
}
}
canonize(s, [k, p]) {
if(p < k)
return [s, k];
else {
var [s2, k2, p2] = s.transition[this.text[k]];
while(p2 - k2 <= p - k) {
k = k + p2 - k2 + 1;
s = s2;
if(k <= p) {
[s2, k2, p2] = s.transition[this.text[k]];
}
}
return [s, k];
}
}
findLongestRepeatedSubstrings(n=3) {
var [text, root] = [this.text, this.root];
var longestSubstrings = [];
(function traverse(node, curStr='') {
if(node.isLeaf()) return;
for(var t in node.transition) {
var [s, a, b] = node.transition[t];
if(!s.isLeaf()) {
var curCurStr = curStr;
var curSubStr = text.substring(a, b + 1);
curCurStr = (node === root) ? curSubStr : curCurStr + curSubStr;
longestSubstrings.push(curCurStr);
traverse(s, curCurStr);
}
}
})(root);
return longestSubstrings.sort((a, b) => b.length - a.length).slice(0, n);
}
toString() {
var text = this.text;
function traverse(node, offset='', ret='') {
for(var t in node.transition) {
var [s, a, b] = node.transition[t];
ret += offset + '["' + text.substring(a, b + 1) + '", ' + a + ', ' + b + ']' + '\r\n';
ret += traverse(s, offset+'\t');
}
return ret;
}
var res = traverse(this.root);
return res;
}
print() {
console.log(this.toString());
}
}