forked from MakeContributions/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(JavaScript): add trie implementations algorithm (MakeContributi…
…ons#863) Co-authored-by: Ming Tsai <[email protected]>
- Loading branch information
Showing
2 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,79 @@ | ||
/* | ||
A trie (pronounced as "try") or prefix tree is a tree data structure | ||
used to efficiently store and retrieve keys in a dataset of strings. | ||
There are various applications of this data structure, | ||
such as autocomplete and spellchecker. | ||
Insertion: | ||
Average Case: O(N) | ||
Worst Case: O(N) | ||
Best Case: O(N) | ||
Deletion: | ||
Average Case: O(N) | ||
Worst Case: O(N) | ||
Best Case: O(N) | ||
Searching: | ||
Average Case: O(N) | ||
Worst Case: O(N) | ||
Best Case: O(1) | ||
Space complexity: O(alphabet_size * average key length * N) | ||
*/ | ||
|
||
/* | ||
Create a node that will have two properties — | ||
one is the hash map for storing children. | ||
the other one is for keeping track of the end of the word. | ||
*/ | ||
|
||
class Node { | ||
constructor() { | ||
this.children = {}; | ||
this.isEndWord = false; | ||
} | ||
} | ||
|
||
class Trie { | ||
constructor() { | ||
this.root = new Node(); | ||
} | ||
insert(word) { | ||
let node = this.root; | ||
for (const char of word) { | ||
if (!node.children[char]) { | ||
node.children[char] = new Node(); | ||
} | ||
node = node.children[char]; | ||
} | ||
node.isEndWord = true; | ||
} | ||
search(word) { | ||
let node = this.root; | ||
for (const char of word) { | ||
if (!node.children[char]) { | ||
return false; | ||
} | ||
node = node.children[char]; | ||
} | ||
return node.isEndWord ? true : false; | ||
} | ||
startsWith(prefix) { | ||
let node = this.root; | ||
for (const char of prefix) { | ||
if (!node.children[char]) { | ||
return false; | ||
} | ||
node = node.children[char]; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
const trie = new Trie(); | ||
trie.insert('datastructures'); | ||
trie.insert('datablock'); | ||
console.log(trie.search('dsa')); // false | ||
console.log(trie.search('datablock')); // true | ||
console.log(trie.startsWith('data')); // true |