-
Notifications
You must be signed in to change notification settings - Fork 269
Trie problems #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Trie problems #117
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3b55b40
update: total words in a Trie
ashokdey 42e4372
update: entry in Readme
ashokdey f77ad9d
update: find all words in a trie
ashokdey 4f7d0c7
update: entry in readme
ashokdey 6469d5f
-fix: problem name fix
ashokdey a6b3ce7
update: counting word ocurences
ashokdey f0ca6ed
update: unique count
ashokdey 2fbc809
fix: return words based on wordCount
ashokdey 3c3d2c4
update: find all unique words
ashokdey c61dfa1
update: README entries added
ashokdey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // eslint-disable-next-line no-unused-vars | ||
| const Trie = require('../index'); | ||
|
|
||
| function getAllWords(root, level, word) { | ||
| let result = []; | ||
|
|
||
| if (!root) return result; | ||
|
|
||
| if (root.isEndOfWord) { | ||
| let temp = ''; | ||
| for (let i = 0; i < level; i += 1) { | ||
| temp += String(word[i]); | ||
| } | ||
| // get the count and push all the occurences | ||
| const res = []; | ||
| for (let i = 0; i < root.wordCount; i += 1) { | ||
| res.push(temp); | ||
| } | ||
| result = [...result, ...res]; | ||
| } | ||
|
|
||
| for (let i = 0; i < 26; i += 1) { | ||
| if (root.children[i] !== null) { | ||
| // eslint-disable-next-line no-param-reassign | ||
| word[level] = String.fromCharCode(i + 'a'.charCodeAt(0)); | ||
| result = [...result, ...getAllWords(root.children[i], level + 1, word)]; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function allWordsFromTrie(root) { | ||
| const word = []; // char arr to store a word | ||
| for (let i = 0; i < 26; i += 1) { | ||
| word[i] = null; | ||
| } | ||
| return getAllWords(root, 0, word); | ||
| } | ||
|
|
||
| // const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed']; | ||
| // const trie = new Trie(); | ||
|
|
||
| // words.forEach(word => trie.insert(word)); | ||
| // console.log(allWordsFromTrie(trie.root)); | ||
|
|
||
| module.exports = allWordsFromTrie; |
40 changes: 40 additions & 0 deletions
src/_DataStructures_/Trees/Trie/get-unique-words/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| const Trie = require('../index'); | ||
|
|
||
| function getAllUniqueWords(root, level, word) { | ||
| let result = []; | ||
|
|
||
| if (!root) return result; | ||
|
|
||
| if (root.isEndOfWord) { | ||
| let temp = ''; | ||
| for (let i = 0; i < level; i += 1) { | ||
| temp += String(word[i]); | ||
| } | ||
| result = [...result, temp]; | ||
| } | ||
|
|
||
| for (let i = 0; i < 26; i += 1) { | ||
| if (root.children[i]) { | ||
| // eslint-disable-next-line no-param-reassign | ||
| word[level] = String.fromCharCode(i + 'a'.charCodeAt(0)); | ||
| result = [...result, ...getAllUniqueWords(root.children[i], level + 1, word)]; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function allUniqueWordsFromTrie(root) { | ||
| const word = []; // char arr to store a word | ||
| for (let i = 0; i < 26; i += 1) { | ||
| word[i] = null; | ||
| } | ||
| return getAllUniqueWords(root, 0, word); | ||
| } | ||
|
|
||
| // const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed']; | ||
| // const trie = new Trie(); | ||
|
|
||
| // words.forEach(word => trie.insert(word)); | ||
| // console.log(allUniqueWordsFromTrie(trie.root)); | ||
|
|
||
| module.exports = allUniqueWordsFromTrie; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // eslint-disable-next-line no-unused-vars | ||
| const Trie = require('../index'); | ||
|
|
||
| function totalWords(root) { | ||
| let result = 0; | ||
| if (root.isEndOfWord) { | ||
| result += root.wordCount; | ||
| } | ||
| for (let i = 0; i < 26; i += 1) { | ||
| if (root.children[i] !== null) { | ||
| result += totalWords(root.children[i]); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| // const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed']; | ||
| // const trie = new Trie(); | ||
|
|
||
| // words.forEach(word => trie.insert(word)); | ||
| // console.log(totalWords(trie.root)); | ||
|
|
||
| module.exports = totalWords; |
23 changes: 23 additions & 0 deletions
src/_DataStructures_/Trees/Trie/unique-word-count/index.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* eslint-disable no-unused-vars */ | ||
| const Trie = require('../index'); | ||
|
|
||
| function uniqueWordCount(root) { | ||
| let result = 0; | ||
| if (root.isEndOfWord) { | ||
| result += 1; | ||
| } | ||
| for (let i = 0; i < 26; i += 1) { | ||
| if (root.children[i]) { | ||
| result += uniqueWordCount(root.children[i]); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| // const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed']; | ||
| // const trie = new Trie(); | ||
|
|
||
| // words.forEach(word => trie.insert(word)); | ||
| // console.log(uniqueWordCount(trie.root)); | ||
|
|
||
| module.exports = uniqueWordCount; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.