-
Notifications
You must be signed in to change notification settings - Fork 2
/
bkTree.js
292 lines (250 loc) · 7.27 KB
/
bkTree.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
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
function levenshteinDistance(str1, str2){
var m = str1.length;
var n = str2.length;
var dn = new Array(m+1);
for(var i=0; i<=m; ++i){
dn[i] = new Array(n+1);
for(var j=0; j<=n; ++j){
if(i==0)
dn[0][j] = j;
else if(j==0)
dn[i][0] = i;
else if(str1[i-1] == str2[j-1])
dn[i][j] = dn[i-1][j-1];
else
dn[i][j] = 1 + Math.min(
dn[i-1][j],
Math.min(dn[i][j-1],
dn[i-1][j-1])
);
}
}
return dn[m][n];
}
function hammingDistance(str1, str2){
var dist = 0;
if(str1.length != str2.length)
return Math.max(str1.length, str2.length);
for(var i=0; i<str1.length; ++i){
if(str1[i] != str2[i]){
dist = dist + 1;
}
}
return dist;
}
class BKTreeNode{
constructor(index=0){
this.index = index;
this.children = null;
}
toJSON(){
if(this.children != null)
var childrenJSON = Array.from(this.children);
else
var childrenJSON = null;
return {'index' : this.index, 'children' : childrenJSON};
}
parse(json){
if(typeof json === "string" || json instanceof String)
json = JSON.parse(json);
this.index = json["index"];
if(json["children"] == null)
this.children = null;
else{
// if(this.children == null)
this.children = new Map();
for(var i=0; i<json["children"].length; ++i){
var entry = json["children"][i];
var node = new BKTreeNode();
node.parse(entry[1]);
this.children.set(entry[0], node);
}
}
}
}
class BKTree{
constructor(items=null, metric=levenshteinDistance){
this.metric = metric;
this.deleted = new Set();
this.size = 0;
this.items = [];
if(items == null)
this._constructEmptyTree();
else if(typeof items === "string" || items instanceof String){
this.items.push(items);
this._constructFromRoot(items);
}else if(Array.isArray(items)){
this.items = items;
this._contructFromDict();
}
}
_constructEmptyTree(){
this.root = null;
}
_constructFromRoot(str){
this.root = new BKTreeNode(0);
}
_contructFromDict(){
this.root = new BKTreeNode(0);
this.size = this.items.length;
var temp;
for(var i=1; i<this.size; ++i){
if(!this._addFromItems(i)){
temp = this.items[i];
this.items[i] = this.items[this.size-1];
this.items[this.size-1] = temp;
this.items.pop();
this.size = this.size-1;
i = i-1;
}
}
}
add(str){
this.size = this.size + 1;
if(this.deleted.delete(str)){
return;
}
var index = this.items.length;
this.items.push(str);
if(this.root == null){
this.root = new BKTreeNode(0);
return;
}
if(!this._addFromItems(index)){
this.size = this.size - 1;
this.items.pop();
}
}
_addFromItems(index){
var t = this.root;
var dist = this.metric(this.items[t.index], this.items[index]);
while(dist > 0){
if(t.children == null){
t.children = new Map();
break;
}
if(!t.children.has(dist))
break;
t = t.children.get(dist);
dist = this.metric(this.items[t.index], this.items[index]);
}
if(dist > 0){
t.children.set(dist, new BKTreeNode(index));
return true;
}else{
return false;
}
}
delete(str){
if(this.has(str)){
this._unsafeDelete(str);
}
}
_unsafeDelete(str){
this.size = this.size - 1;
this.deleted.add(str);
}
query(str, tol){
if(this.size <= 0)
return [];
var matches = new Array();
this._query(str, tol, this.root, matches);
return matches;
}
_query(str, tol, n, vec){
if(this.deleted.has(this.items[n.index]))
return;
var dist = this.metric(str, this.items[n.index]);
if(dist <= tol)
if(vec != null)
vec.push({[dist] : this.items[n.index]});
var gte = dist-tol;
var lte = dist+tol;
if(n.children == null)
return;
for(const entry of n.children.entries()){
if(entry[0] <= lte && entry[0] >= gte)
this._query(str, tol, entry[1], vec);
}
}
entries(){
return this.items.slice(0);
}
has(str, tol=0){
if(this.size <= 0)
return false;
return this._has(str, tol, this.root);
}
_has(str, tol, n){
if(this.deleted.has(this.items[n.index]))
return;
var dist = this.metric(str, this.items[n.index]);
if(dist <= tol)
return true;
var gte = dist-tol;
var lte = dist+tol;
if(n.children == null)
return false;
for(const entry of n.children.entries()){
if(entry[0] <= lte && entry[0] >= gte)
if(this._has(str, tol, entry[1]))
return true;
}
return false;
}
_getMetricName(){
var metricName;
if(this.metric == hammingDistance)
metricName = "hammingDistance";
else if(this.metric == levenshteinDistance)
metricName = "levenshteinDistance";
else
metricName = null;
return metricName;
}
print(){
console.log(this.size);
console.log(this._getMetricName());
this._print();
}
_print(r=this.root, n=this.root, depth=0){
var str = "";
for(var i=0; i<depth; ++i)
str = str + "\t";
if(this.deleted.has(this.items[n.index]))
str = str + "*";
str = str + this.items[n.index] + ", " + this.metric(this.items[r.index], this.items[n.index]);
console.log(str);
if(n.children == null)
return;
for(const entry of n.children.entries()){
this._print(n, entry[1], depth+1);
}
}
merge(bk){
var items = bk.entries();
for(var i=0; i<items.length; ++i){
this.add(items[i]);
}
}
toJSON(){
return {'metric' : this._getMetricName(),'root' : this.root};
}
parse(json){
if(typeof json === "string" || json instanceof String)
json = JSON.parse(json);
if(this.root == null)
this.root = new BKTreeNode();
var metricName = json["metric"];
if(metricName == "hammingDistance")
this.metric = hammingDistance;
else if(metricName == "levenshteinDistance")
this.metric = levenshteinDistance;
else
this.metric = null;
this.root.parse(json["root"]);
}
stringify(){
return JSON.stringify(this);
}
}