-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
bk-tree.js
180 lines (146 loc) · 3.2 KB
/
bk-tree.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
/* eslint no-constant-condition: 0 */
/**
* Mnemonist BK Tree
* ==================
*
* Implementation of a Burkhard-Keller tree, allowing fast lookups of words
* that lie within a specified distance of the query word.
*
* [Reference]:
* https://en.wikipedia.org/wiki/BK-tree
*
* [Article]:
* W. Burkhard and R. Keller. Some approaches to best-match file searching,
* CACM, 1973
*/
var forEach = require('obliterator/foreach');
/**
* BK Tree.
*
* @constructor
* @param {function} distance - Distance function to use.
*/
function BKTree(distance) {
if (typeof distance !== 'function')
throw new Error('mnemonist/BKTree.constructor: given `distance` should be a function.');
this.distance = distance;
this.clear();
}
/**
* Method used to add an item to the tree.
*
* @param {any} item - Item to add.
* @return {BKTree}
*/
BKTree.prototype.add = function(item) {
// Initializing the tree with the first given word
if (!this.root) {
this.root = {
item: item,
children: {}
};
this.size++;
return this;
}
var node = this.root,
d;
while (true) {
d = this.distance(item, node.item);
if (!node.children[d])
break;
node = node.children[d];
}
node.children[d] = {
item: item,
children: {}
};
this.size++;
return this;
};
/**
* Method used to query the tree.
*
* @param {number} n - Maximum distance between query & item.
* @param {any} query - Query
* @return {BKTree}
*/
BKTree.prototype.search = function(n, query) {
if (!this.root)
return [];
var found = [],
stack = [this.root],
node,
child,
d,
i,
l;
while (stack.length) {
node = stack.pop();
d = this.distance(query, node.item);
if (d <= n)
found.push({item: node.item, distance: d});
for (i = d - n, l = d + n + 1; i < l; i++) {
child = node.children[i];
if (child)
stack.push(child);
}
}
return found;
};
/**
* Method used to clear the tree.
*
* @return {undefined}
*/
BKTree.prototype.clear = function() {
// Properties
this.size = 0;
this.root = null;
};
/**
* Convenience known methods.
*/
BKTree.prototype.toJSON = function() {
return this.root;
};
BKTree.prototype.inspect = function() {
var array = [],
stack = [this.root],
node,
d;
while (stack.length) {
node = stack.pop();
if (!node)
continue;
array.push(node.item);
for (d in node.children)
stack.push(node.children[d]);
}
// Trick so that node displays the name of the constructor
Object.defineProperty(array, 'constructor', {
value: BKTree,
enumerable: false
});
return array;
};
if (typeof Symbol !== 'undefined')
BKTree.prototype[Symbol.for('nodejs.util.inspect.custom')] = BKTree.prototype.inspect;
/**
* Static @.from function taking an arbitrary iterable & converting it into
* a tree.
*
* @param {Iterable} iterable - Target iterable.
* @param {function} distance - Distance function.
* @return {Heap}
*/
BKTree.from = function(iterable, distance) {
var tree = new BKTree(distance);
forEach(iterable, function(value) {
tree.add(value);
});
return tree;
};
/**
* Exporting.
*/
module.exports = BKTree;