Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b55b40

Browse files
committed
update: total words in a Trie
1 parent 43ed1bc commit 3b55b40

File tree

1 file changed

+23
-0
lines changed
  • src/_DataStructures_/Trees/Trie/total-words-in-trie

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const Trie = require('../index');
3+
4+
function totalWords(root) {
5+
let result = 0;
6+
if (root.isEndOfWord) {
7+
result += 1;
8+
}
9+
for (let i = 0; i < 26; i += 1) {
10+
if (root.children[i] !== null) {
11+
result += totalWords(root.children[i]);
12+
}
13+
}
14+
return result;
15+
}
16+
17+
// const words = ['bed', 'ball', 'apple', 'java', 'javascript'];
18+
// const trie = new Trie();
19+
20+
// words.forEach(word => trie.insert(word));
21+
// console.log(totalWords(trie.root));
22+
23+
module.exports = totalWords;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /