-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrieSample.js
119 lines (94 loc) · 3.02 KB
/
trieSample.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
/*
Trie storage samples.
Read content from a given URL and then index all the words that appear there.
*/
function WordDictionary() {
this.dictionary = {};
WordDictionary.prototype.addWord = function(word) {
if (!this.dictionary.hasOwnProperty(word)) {
this.dictionary[word] = 1;
}
else {
this.dictionary[word]++;
}
return this.dictionary[word];
}
WordDictionary.prototype.addArryOfWord = function(a) {
var uniqueAdditions = 0;
for (var i = 0, len = a.length; i < len; i++) {
if (this.addWord(a[i]) === 1) uniqueAdditions++;
}
return uniqueAdditions;
}
}
function WordTrie() {
this.rootNode = {};
WordTrie.prototype.addWord = function(word) {
//returns the number of times this word has been added
var currentNode = this.rootNode;
var isNewWord = false;
// Work downwards through the trie, adding nodes
// as needed, and keeping track of whether we add
// any nodes.
for (var i = 0; i < word.length; i++) {
var char = word[i];
if (!currentNode.hasOwnProperty(char)) {
isNewWord = true;
currentNode[char] = {};
}
currentNode = currentNode[char];
}
// Explicitly mark the end of a word by creating a count node.
// Otherwise, we might say a word is
// present if it is a prefix of a different,
// longer word that was added earlier.
if (!currentNode.hasOwnProperty("ct")) {
isNewWord = true;
currentNode["ct"] = 1;
}
else {
currentNode["ct"]++;
}
return currentNode["ct"];
}
WordTrie.prototype.addArryOfWord = function(a) {
var uniqueAdditions = 0;
for (var i = 0, len = a.length; i < len; i++) {
if (this.addWord(a[i]) === 1) uniqueAdditions++;
}
return uniqueAdditions;
}
}
var fs = require('fs'),
wordArray = (fs.readFileSync('hamletlc.txt').toString()).split(' ');
console.log('wordArray length ' + wordArray.length);
//var wordArray = ['the', 'unfortunate', 'these', 'fortunate', 'the', 'and', 'the', 'horse', 'pig', 'fortune'];
global.gc();
var memStart = process.memoryUsage();
console.time('WordTrie');
var wt = new WordTrie();
console.log('unique additions ' + wt.addArryOfWord(wordArray));
var memEnd = process.memoryUsage(),
memUsed = {
rss: memEnd.rss - memStart.rss,
heapUsed: memEnd.heapUsed - memStart.heapUsed
};
console.log({
memUsed
});
console.timeEnd('WordTrie');
global.gc();
var memStart = process.memoryUsage();
console.time('WordDictionary');
var wd = new WordDictionary();
console.log('unique additions ' + wd.addArryOfWord(wordArray));
var memEnd = process.memoryUsage(),
memUsed = {
rss: memEnd.rss - memStart.rss,
heapUsed: memEnd.heapUsed - memStart.heapUsed
};
console.log({
memUsed
});
console.timeEnd('WordDictionary');
process.exit();