-
Notifications
You must be signed in to change notification settings - Fork 0
/
trieNode.js
76 lines (66 loc) · 2.24 KB
/
trieNode.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
var TrieNode = function(ch) {
var that = Object.create(TrieNode.prototype);
// array of children nodes.
var children = [];
// key used to represent this node. empty string for root node.
var key = ch;
// optional value stored in the node. value will only be present for words
// that were added to the trie. null for root node.
var value = null;
that.value = function() {
return value;
}
that.key = function() {
return key;
}
// inserts a word into the node
that.put = function(word, depth) {
if (word.length == depth) { value = word; return; }
var c = word[depth];
// get child corresponding to character
var child = children.filter(function(child) {
return child.key() == c;
}).pop();
// if child does not exist, create it and add to children array
if (!child) {
child = TrieNode(c);
children.push(child);
// resort children array
children.sort(function(child1, child2) {
return +(child1.key() > child2.key()) || +(child1.key() === child2.key()) - 1;
});
}
// add word to child
child.put(word, depth+1);
},
// gets n words under the node that match the prefix
that.getWithPrefix = function(prefix, n) {
var node = that.getNode(prefix);
if (!node) { return []; } // can't find prefix on the Trie
return node.nWords(n);
}
// returns node associated with given prefix, null if prefix not present
that.getNode = function(prefix) {
if (prefix.length == 0) { return that; }
var c = prefix[0];
var child = children.filter(function(child) {
return child.key() == c;
}).pop();
if (!child) { return null };
return child.getNode(prefix.substring(1));
}
// returns the first n lexicographically sorted words in the tree node
// if tree has less than n words, returns all words sorted
that.nWords = function(n) {
var words = [];
if (n < 1) { return words; } // base case
if (value) { words.push(value); };
// since the children array is sorted this will return results
// in lexicographical order
return children.reduce(function(prev, child) {
return prev.concat(child.nWords(n - prev.length));
}, words);
}
Object.freeze(that);
return that;
};