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

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
ayDavidGitHere wants to merge 18 commits into TheAlgorithms:master from ayDavidGitHere:master
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
Mar 24, 2022
4f16a9b
Merge branch 'TheAlgorithms:master' into master
ayDavidGitHere Apr 20, 2022
2e63444
Auto-update DIRECTORY.md
Apr 20, 2022
e7cb578
Created boyerMoore.js
ayDavidGitHere Apr 20, 2022
8111a43
Update and rename boyerMoore.js to BoyerMoore.js
ayDavidGitHere Apr 20, 2022
81e72f6
Update BooyerMoore.js
ayDavidGitHere Apr 21, 2022
30f7486
Update BoyerMoore.js
ayDavidGitHere Apr 21, 2022
95a902a
Final fix codestyle for BoyerMoore.js
ayDavidGitHere Apr 21, 2022
508f38d
Auto-update DIRECTORY.md
Apr 21, 2022
40bd41c
Update BoyerMoore.js | Lgtm alert fix.
ayDavidGitHere Apr 21, 2022
9181bde
BoyerMoore.js| Final fix indentation.
ayDavidGitHere Apr 22, 2022
1992b41
BoyerMoore.js| Final Fix :(
ayDavidGitHere Apr 23, 2022
d97bc20
Merge branch 'TheAlgorithms:master' into master
ayDavidGitHere Apr 24, 2022
738d17c
Create Knuth-Morris-Pratt.js
ayDavidGitHere Apr 25, 2022
eab1f7d
Delete KnuthMorrisPratt.js
ayDavidGitHere Apr 25, 2022
fbc9f56
Merge branch 'TheAlgorithms:master' into master
ayDavidGitHere Apr 29, 2022
eb5e5b2
Create Trie.js
ayDavidGitHere Apr 29, 2022
a13bf47
Update Trie.js
ayDavidGitHere Apr 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions String/Trie.js
View file Open in desktop
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
*/

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