Skip to content

Commit

Permalink
chore(JavaScript): add trie implementations algorithm (MakeContributi…
Browse files Browse the repository at this point in the history
…ons#863)

Co-authored-by: Ming Tsai <[email protected]>
  • Loading branch information
pygaurav and ming-tsai authored Oct 6, 2022
1 parent 9b6d8e0 commit 684d69d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
4 changes: 4 additions & 0 deletions algorithms/JavaScript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@

- [Max Heap](src/heaps/max-heap.js)
- [Min Heap](src/heaps/min-heap.js)

## Trie

- [Trie Implementation](src/trie/trie-implementation.js)
79 changes: 79 additions & 0 deletions algorithms/JavaScript/src/trie/trie-implementation.js
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

0 comments on commit 684d69d

Please sign in to comment.