-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Add Trie (prefix tree) string search algorithm #1001
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
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
277464f
Auto-update DIRECTORY.md
4f16a9b
Merge branch 'TheAlgorithms:master' into master
ayDavidGitHere 2e63444
Auto-update DIRECTORY.md
e7cb578
Created boyerMoore.js
ayDavidGitHere 8111a43
Update and rename boyerMoore.js to BoyerMoore.js
ayDavidGitHere 81e72f6
Update BooyerMoore.js
ayDavidGitHere 30f7486
Update BoyerMoore.js
ayDavidGitHere 95a902a
Final fix codestyle for BoyerMoore.js
ayDavidGitHere 508f38d
Auto-update DIRECTORY.md
40bd41c
Update BoyerMoore.js | Lgtm alert fix.
ayDavidGitHere 9181bde
BoyerMoore.js| Final fix indentation.
ayDavidGitHere 1992b41
BoyerMoore.js| Final Fix :(
ayDavidGitHere d97bc20
Merge branch 'TheAlgorithms:master' into master
ayDavidGitHere 738d17c
Create Knuth-Morris-Pratt.js
ayDavidGitHere eab1f7d
Delete KnuthMorrisPratt.js
ayDavidGitHere fbc9f56
Merge branch 'TheAlgorithms:master' into master
ayDavidGitHere eb5e5b2
Create Trie.js
ayDavidGitHere a13bf47
Update Trie.js
ayDavidGitHere 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
A trie (a digital or prefix tree) is a string search algorithm | ||
It is implemented using a nested object where each level has its direct children as keys. | ||
A trie node can be formed by using an object to store the children. | ||
The trie has a root node that is instantiated in the constructor of the Trie class, as shown. | ||
*/ | ||
const TrieNode = function () { | ||
this.children = {} | ||
this.endOfWord = false | ||
} | ||
const Trie = function () { | ||
this.root = new TrieNode() | ||
} | ||
Trie.prototype.insert = function (word) { | ||
let current = this.root | ||
for (let i = 0; i < word.length; i++) { | ||
const ch = word.charAt(i) | ||
let node = current.children[ch] | ||
if (node === null) { | ||
node = new TrieNode() | ||
current.children[ch] = node | ||
} | ||
current = node | ||
} | ||
current.endOfWord = true | ||
} | ||
/* | ||
To search inside a trie, each character of the word must be checked. | ||
This is done by setting a temporary variable of current on the root. | ||
The current variable is updated as each character in the word is checked. | ||
*/ | ||
Trie.prototype.search = function (word) { | ||
let current = this.root | ||
for (let i = 0; i < word.length; i++) { | ||
const ch = word.charAt(i) | ||
const node = current.children[ch] | ||
if (node === null) { | ||
return false // node doesn't exist | ||
} | ||
current = node | ||
} | ||
return current.endOfWord | ||
} | ||
/* | ||
To delete an element from a trie, the algorithm should traverse the root node until it reaches the last character of the word. | ||
Then, for each node that does not have any other children, the node should be deleted. | ||
The recursive implementation in the following code block implements this algorithm | ||
*/ | ||
Trie.prototype.delete = function (word) { | ||
this.deleteRecursively(this.root, word, 0) | ||
} | ||
Trie.prototype.deleteRecursively = function (current, word, index) { | ||
if (index === word.length) { | ||
// when end of word is reached only delete if current.end Of Word is true. | ||
if (!current.endOfWord) { | ||
return false | ||
} | ||
current.endOfWord = false | ||
// if current has no other mapping then return true | ||
return Object.keys(current.children).length === 0 | ||
} | ||
const ch = word.charAt(index) | ||
const node = current.children[ch] | ||
if (node === null) { | ||
return false | ||
} | ||
const canDeleteCurrentNode = this.deleteRecursively(node, word, index + 1) | ||
if (canDeleteCurrentNode) { | ||
delete current.children[ch] | ||
// return true if no mappings are left in the map. | ||
return Object.keys(current.children).length === 0 | ||
} | ||
return false | ||
} | ||
export { Trie } | ||
/* | ||
const trie1 = new Trie() | ||
trie1.insert("javascript") | ||
trie1.insert("algthm") | ||
trie1.search("algthm") // true | ||
trie1.delete("javascript") | ||
trie1.delete("algthm") | ||
trie1.search("javascript") // false | ||
console.log(trie1.search("algthm"))// false | ||
*/ |
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.