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

Commit 35c2fe9

Browse files
authored
Merge pull request #104 from knaxus/trie
Trie - tree based data structure
2 parents 72f5ba5 + ddec5a6 commit 35c2fe9

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

β€ŽREADME.mdβ€Ž

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,16 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
3131
- [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array)
3232
- [Sort a Stack](src/_DataStructures_/Stack/sort-a-stack)
3333

34-
35-
- [Queue](src/_DataStructures_/Queue)
34+
* [Queue](src/_DataStructures_/Queue)
3635

3736
- [Weave](src/_DataStructures_/Queue/weave)
3837
- [Reverse First K Elements of a Queue](src/_DataStructures_/Queue/reverse-first-k)
3938
- [Generate all Binary Numbers from 1 to N](src/_DataStructures_/Queue/generate-binary-number)
4039
- [Queue using Stack](src/_DataStructures_/Queue/queue-using-stack)
4140

42-
- [Doubly Linked List](src/_DataStructures_/DoublyLinkedList)
41+
* [Doubly Linked List](src/_DataStructures_/DoublyLinkedList)
4342

44-
- [Trees](src/_DataStructures_/Trees)
43+
* [Trees](src/_DataStructures_/Trees)
4544
- [Binary Tree (creation using level order)](src/_DataStructures_/Trees/BinaryTree)
4645
- [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree)
4746
- [Find k<sup>th</sup> maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max)
@@ -50,6 +49,7 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
5049
- [Find Height of BST](src/_DataStructures_/Trees/BinarySearchTree/height-of-bst)
5150
- [Find k Nodes from Root of BST](src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root)
5251
- [Suffix Tree](src/_DataStructures_/SuffixTree)
52+
- [Trie](src/_DataStructures_/Trees/Trie)
5353

5454
### Logical Problems
5555

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class TrieNode {
2+
constructor(char) {
3+
this.char = char;
4+
this.children = [];
5+
this.isEndOfWord = false;
6+
7+
// mark all the alphabets as null
8+
for (let i = 0; i < 26; i += 1) this.children[i] = null;
9+
}
10+
11+
markAsLeaf() {
12+
this.isEndOfWord = true;
13+
}
14+
15+
unmarkAsLeaf() {
16+
this.isEndOfWord = false;
17+
}
18+
}
19+
20+
module.exports = TrieNode;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
const Node = require('./Node');
2+
3+
class Trie {
4+
constructor() {
5+
this.root = new Node('');
6+
}
7+
8+
// helper to get the index of a character
9+
// eslint-disable-next-line class-methods-use-this
10+
getIndexOfChar(char) {
11+
return char.charCodeAt(0) - 'a'.charCodeAt(0);
12+
}
13+
14+
insert(key) {
15+
if (!key) {
16+
return false;
17+
}
18+
19+
// convert to lower case
20+
// keys are basically words
21+
const word = key.toLowerCase();
22+
let currentNode = this.root;
23+
24+
for (let level = 0; level < word.length; level += 1) {
25+
const index = this.getIndexOfChar(word[level]);
26+
if (!currentNode.children[index]) {
27+
currentNode.children[index] = new Node(word[level]);
28+
}
29+
currentNode = currentNode.children[index];
30+
}
31+
32+
// when we are done with inserting all the character of the word,
33+
// mark the node as end leaf
34+
currentNode.markAsLeaf();
35+
return true;
36+
}
37+
38+
search(key) {
39+
if (!key) {
40+
return false;
41+
}
42+
43+
// convert word to lower case
44+
const word = key.toLowerCase();
45+
let currentNode = this.root;
46+
47+
for (let level = 0; level < word.length; level += 1) {
48+
const index = this.getIndexOfChar(word[level]);
49+
if (!currentNode.children[index]) {
50+
return false;
51+
}
52+
currentNode = currentNode.children[index];
53+
}
54+
if (currentNode !== null && currentNode.isEndOfWord) {
55+
return true;
56+
}
57+
return false;
58+
}
59+
}
60+
61+
// const words = ['bed', 'ball', 'apple', 'java', 'javascript'];
62+
// const trie = new Trie();
63+
64+
// words.forEach(word => trie.insert(word));
65+
66+
// console.log(trie.root);
67+
68+
// console.log(trie.search(words[3]));
69+
// console.log(trie.search('word'));
70+
// console.log(trie.search(words[4]));
71+
// console.log(trie.search('random'));
72+
73+
module.exports = Trie;

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /