Skip to content

Commit 00fba31

Browse files
authored
Merge pull request knaxus#117 from knaxus/trie
Trie problems
2 parents a00df3e + c61dfa1 commit 00fba31

File tree

7 files changed

+145
-3
lines changed

7 files changed

+145
-3
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,16 +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-
* [Queue](src/_DataStructures_/Queue)
34+
- [Queue](src/_DataStructures_/Queue)
3535

3636
- [Weave](src/_DataStructures_/Queue/weave)
3737
- [Reverse First K Elements of a Queue](src/_DataStructures_/Queue/reverse-first-k)
3838
- [Generate all Binary Numbers from 1 to N](src/_DataStructures_/Queue/generate-binary-number)
3939
- [Queue using Stack](src/_DataStructures_/Queue/queue-using-stack)
4040

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

43-
* [Trees](src/_DataStructures_/Trees)
43+
- [Trees](src/_DataStructures_/Trees)
4444
- [Binary Tree (creation using level order)](src/_DataStructures_/Trees/BinaryTree)
4545
- [Binary Search Tree](src/_DataStructures_/Trees/BinarySearchTree)
4646
- [Find k<sup>th</sup> maximum in a BinarySearchTree](src/_DataStructures_/Trees/BinarySearchTree/find-kth-max)
@@ -50,6 +50,10 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
5050
- [Find k Nodes from Root of BST](src/_DataStructures_/Trees/BinarySearchTree/find-k-nodes-from-root)
5151
- [Suffix Tree](src/_DataStructures_/SuffixTree)
5252
- [Trie](src/_DataStructures_/Trees/Trie)
53+
- [Total words count count in a Trie](src/_DataStructures_/Trees/Trie/total-words-in-trie)
54+
- [Unique words count in a Trie](src/_DataStructures_/Trees/Trie/unique-word-count)
55+
- [All the words from a Trie](src/_DataStructures_/Trees/Trie/all-words-in-trie)
56+
- [Unique words in a Trie](src/_DataStructures_/Trees/Trie/get-unique-words)
5357

5458
### Logical Problems
5559

src/_DataStructures_/Trees/Trie/Node.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ class TrieNode {
33
this.char = char;
44
this.children = [];
55
this.isEndOfWord = false;
6+
this.wordCount = 0;
67

78
// mark all the alphabets as null
89
for (let i = 0; i < 26; i += 1) this.children[i] = null;
@@ -15,6 +16,10 @@ class TrieNode {
1516
unmarkAsLeaf() {
1617
this.isEndOfWord = false;
1718
}
19+
20+
increaseCount() {
21+
this.wordCount += 1;
22+
}
1823
}
1924

2025
module.exports = TrieNode;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const Trie = require('../index');
3+
4+
function getAllWords(root, level, word) {
5+
let result = [];
6+
7+
if (!root) return result;
8+
9+
if (root.isEndOfWord) {
10+
let temp = '';
11+
for (let i = 0; i < level; i += 1) {
12+
temp += String(word[i]);
13+
}
14+
// get the count and push all the occurences
15+
const res = [];
16+
for (let i = 0; i < root.wordCount; i += 1) {
17+
res.push(temp);
18+
}
19+
result = [...result, ...res];
20+
}
21+
22+
for (let i = 0; i < 26; i += 1) {
23+
if (root.children[i] !== null) {
24+
// eslint-disable-next-line no-param-reassign
25+
word[level] = String.fromCharCode(i + 'a'.charCodeAt(0));
26+
result = [...result, ...getAllWords(root.children[i], level + 1, word)];
27+
}
28+
}
29+
return result;
30+
}
31+
32+
function allWordsFromTrie(root) {
33+
const word = []; // char arr to store a word
34+
for (let i = 0; i < 26; i += 1) {
35+
word[i] = null;
36+
}
37+
return getAllWords(root, 0, word);
38+
}
39+
40+
// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
41+
// const trie = new Trie();
42+
43+
// words.forEach(word => trie.insert(word));
44+
// console.log(allWordsFromTrie(trie.root));
45+
46+
module.exports = allWordsFromTrie;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const Trie = require('../index');
2+
3+
function getAllUniqueWords(root, level, word) {
4+
let result = [];
5+
6+
if (!root) return result;
7+
8+
if (root.isEndOfWord) {
9+
let temp = '';
10+
for (let i = 0; i < level; i += 1) {
11+
temp += String(word[i]);
12+
}
13+
result = [...result, temp];
14+
}
15+
16+
for (let i = 0; i < 26; i += 1) {
17+
if (root.children[i]) {
18+
// eslint-disable-next-line no-param-reassign
19+
word[level] = String.fromCharCode(i + 'a'.charCodeAt(0));
20+
result = [...result, ...getAllUniqueWords(root.children[i], level + 1, word)];
21+
}
22+
}
23+
return result;
24+
}
25+
26+
function allUniqueWordsFromTrie(root) {
27+
const word = []; // char arr to store a word
28+
for (let i = 0; i < 26; i += 1) {
29+
word[i] = null;
30+
}
31+
return getAllUniqueWords(root, 0, word);
32+
}
33+
34+
// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
35+
// const trie = new Trie();
36+
37+
// words.forEach(word => trie.insert(word));
38+
// console.log(allUniqueWordsFromTrie(trie.root));
39+
40+
module.exports = allUniqueWordsFromTrie;

src/_DataStructures_/Trees/Trie/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Trie {
3232
// when we are done with inserting all the character of the word,
3333
// mark the node as end leaf
3434
currentNode.markAsLeaf();
35+
currentNode.increaseCount();
3536
return true;
3637
}
3738

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// eslint-disable-next-line no-unused-vars
2+
const Trie = require('../index');
3+
4+
function totalWords(root) {
5+
let result = 0;
6+
if (root.isEndOfWord) {
7+
result += root.wordCount;
8+
}
9+
for (let i = 0; i < 26; i += 1) {
10+
if (root.children[i] !== null) {
11+
result += totalWords(root.children[i]);
12+
}
13+
}
14+
return result;
15+
}
16+
17+
// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
18+
// const trie = new Trie();
19+
20+
// words.forEach(word => trie.insert(word));
21+
// console.log(totalWords(trie.root));
22+
23+
module.exports = totalWords;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* eslint-disable no-unused-vars */
2+
const Trie = require('../index');
3+
4+
function uniqueWordCount(root) {
5+
let result = 0;
6+
if (root.isEndOfWord) {
7+
result += 1;
8+
}
9+
for (let i = 0; i < 26; i += 1) {
10+
if (root.children[i]) {
11+
result += uniqueWordCount(root.children[i]);
12+
}
13+
}
14+
return result;
15+
}
16+
17+
// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
18+
// const trie = new Trie();
19+
20+
// words.forEach(word => trie.insert(word));
21+
// console.log(uniqueWordCount(trie.root));
22+
23+
module.exports = uniqueWordCount;

0 commit comments

Comments
 (0)