-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.js
48 lines (40 loc) · 1.03 KB
/
trie.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
// It might require seomething, no?
var sys = require('sys'),
fs = require('fs');
// variable that holds the trie
var root = {};
function loadWords(filename){
fs.stat(filename, function (stat_error, stat){
if(stat_error){
sys.puts("error in file");
process.exit(1);
}
if(stat.isFile()){
fs.readFile(filename, 'utf8', function (read_error, content) {
root = JSON.parse(content);
sys.puts(content);
sys.puts(findWord("bar"));
sys.puts(findWord("rat"));
sys.puts(findWord("rate"));
sys.puts(findWord("a"));
});
}
});
}
function findWord(word){
var node = root;
debugger;
for(var i = 0; i < word.length - 1; i++){
if(!node[word[i]]){
return false;
}
node = node[word[i]];
}
if(node[word[i]]){
return node[word[i]].end === true;
} else{
return false;
}
}
loadWords(process.argv[2]);
sys.puts(findWord("bar"));