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

Trie problems #117

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

Merged
TheSTL merged 10 commits into master from trie
Oct 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 7 additions & 3 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
- [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array)
- [Sort a Stack](src/_DataStructures_/Stack/sort-a-stack)

* [Queue](src/_DataStructures_/Queue)
- [Queue](src/_DataStructures_/Queue)

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

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

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

### Logical Problems

Expand Down
5 changes: 5 additions & 0 deletions src/_DataStructures_/Trees/Trie/Node.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class TrieNode {
this.char = char;
this.children = [];
this.isEndOfWord = false;
this.wordCount = 0;

// mark all the alphabets as null
for (let i = 0; i < 26; i += 1) this.children[i] = null;
Expand All @@ -15,6 +16,10 @@ class TrieNode {
unmarkAsLeaf() {
this.isEndOfWord = false;
}

increaseCount() {
this.wordCount += 1;
}
}

module.exports = TrieNode;
46 changes: 46 additions & 0 deletions src/_DataStructures_/Trees/Trie/all-words-in-trie/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// eslint-disable-next-line no-unused-vars
const Trie = require('../index');

function getAllWords(root, level, word) {
let result = [];

if (!root) return result;

if (root.isEndOfWord) {
let temp = '';
for (let i = 0; i < level; i += 1) {
temp += String(word[i]);
}
// get the count and push all the occurences
const res = [];
for (let i = 0; i < root.wordCount; i += 1) {
res.push(temp);
}
result = [...result, ...res];
}

for (let i = 0; i < 26; i += 1) {
if (root.children[i] !== null) {
// eslint-disable-next-line no-param-reassign
word[level] = String.fromCharCode(i + 'a'.charCodeAt(0));
result = [...result, ...getAllWords(root.children[i], level + 1, word)];
}
}
return result;
}

function allWordsFromTrie(root) {
const word = []; // char arr to store a word
for (let i = 0; i < 26; i += 1) {
word[i] = null;
}
return getAllWords(root, 0, word);
}

// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
// const trie = new Trie();

// words.forEach(word => trie.insert(word));
// console.log(allWordsFromTrie(trie.root));

module.exports = allWordsFromTrie;
40 changes: 40 additions & 0 deletions src/_DataStructures_/Trees/Trie/get-unique-words/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const Trie = require('../index');

function getAllUniqueWords(root, level, word) {
let result = [];

if (!root) return result;

if (root.isEndOfWord) {
let temp = '';
for (let i = 0; i < level; i += 1) {
temp += String(word[i]);
}
result = [...result, temp];
}

for (let i = 0; i < 26; i += 1) {
if (root.children[i]) {
// eslint-disable-next-line no-param-reassign
word[level] = String.fromCharCode(i + 'a'.charCodeAt(0));
result = [...result, ...getAllUniqueWords(root.children[i], level + 1, word)];
}
}
return result;
}

function allUniqueWordsFromTrie(root) {
const word = []; // char arr to store a word
for (let i = 0; i < 26; i += 1) {
word[i] = null;
}
return getAllUniqueWords(root, 0, word);
}

// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
// const trie = new Trie();

// words.forEach(word => trie.insert(word));
// console.log(allUniqueWordsFromTrie(trie.root));

module.exports = allUniqueWordsFromTrie;
1 change: 1 addition & 0 deletions src/_DataStructures_/Trees/Trie/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Trie {
// when we are done with inserting all the character of the word,
// mark the node as end leaf
currentNode.markAsLeaf();
currentNode.increaseCount();
return true;
}

Expand Down
23 changes: 23 additions & 0 deletions src/_DataStructures_/Trees/Trie/total-words-in-trie/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// eslint-disable-next-line no-unused-vars
const Trie = require('../index');

function totalWords(root) {
let result = 0;
if (root.isEndOfWord) {
result += root.wordCount;
}
for (let i = 0; i < 26; i += 1) {
if (root.children[i] !== null) {
result += totalWords(root.children[i]);
}
}
return result;
}

// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
// const trie = new Trie();

// words.forEach(word => trie.insert(word));
// console.log(totalWords(trie.root));

module.exports = totalWords;
23 changes: 23 additions & 0 deletions src/_DataStructures_/Trees/Trie/unique-word-count/index.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-disable no-unused-vars */
const Trie = require('../index');

function uniqueWordCount(root) {
let result = 0;
if (root.isEndOfWord) {
result += 1;
}
for (let i = 0; i < 26; i += 1) {
if (root.children[i]) {
result += uniqueWordCount(root.children[i]);
}
}
return result;
}

// const words = ['bed', 'ball', 'apple', 'java', 'javascript', 'bed'];
// const trie = new Trie();

// words.forEach(word => trie.insert(word));
// console.log(uniqueWordCount(trie.root));

module.exports = uniqueWordCount;

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