From 59149479831575e502ddc6f1a6a0cc8058445bb0 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sat, 19 Oct 2019 10:48:51 +0530 Subject: [PATCH 1/3] update: TrieNode, insert() in Trie --- src/_DataStructures_/Trees/Trie/Node.js | 20 ++++++++++++ src/_DataStructures_/Trees/Trie/index.js | 39 ++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/_DataStructures_/Trees/Trie/Node.js create mode 100644 src/_DataStructures_/Trees/Trie/index.js diff --git a/src/_DataStructures_/Trees/Trie/Node.js b/src/_DataStructures_/Trees/Trie/Node.js new file mode 100644 index 00000000..05f6e673 --- /dev/null +++ b/src/_DataStructures_/Trees/Trie/Node.js @@ -0,0 +1,20 @@ +class TrieNode { + constructor(char) { + this.char = char; + this.children = []; + this.isEndOfWord = false; + + // mark all the alphabets as null + for (let i = 0; i < 26; i += 1) this.children[i] = null; + } + + markAsLeaf() { + this.isEndOfWord = true; + } + + unmarkAsLeaf() { + this.isEndOfWord = false; + } +} + +module.exports = TrieNode; diff --git a/src/_DataStructures_/Trees/Trie/index.js b/src/_DataStructures_/Trees/Trie/index.js new file mode 100644 index 00000000..6d058d48 --- /dev/null +++ b/src/_DataStructures_/Trees/Trie/index.js @@ -0,0 +1,39 @@ +const Node = require('./Node'); + +class Trie { + constructor() { + this.root = new Node(''); + } + + insert(key) { + if (!key) { + return false; + } + + // convert to lower case + // keys are basically words + const word = key.toLowerCase(); + let currentNode = this.root; + + for (let level = 0; level < word.length; level += 1) { + const index = this.getIndexOfChar(word[level]); + if (!currentNode.children[index]) { + currentNode.children[index] = new Node(word[level]); + } + currentNode = currentNode.children[index]; + } + + // when we are done with inserting all the character of the word, + // mark the node as end leaf + currentNode.markAsLeaf(); + return true; + } + + // helper to get the index of a character + // eslint-disable-next-line class-methods-use-this + getIndexOfChar(char) { + return char.charCodeAt(0) - 'a'.charCodeAt(0); + } +} + +module.exports = Trie; From a3cafc8e368b44bc1a3b8f6df8adde6fd91f49f7 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sat, 19 Oct 2019 11:08:50 +0530 Subject: [PATCH 2/3] update: trie search() --- src/_DataStructures_/Trees/Trie/index.js | 42 +++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/_DataStructures_/Trees/Trie/index.js b/src/_DataStructures_/Trees/Trie/index.js index 6d058d48..8975eb7e 100644 --- a/src/_DataStructures_/Trees/Trie/index.js +++ b/src/_DataStructures_/Trees/Trie/index.js @@ -5,6 +5,12 @@ class Trie { this.root = new Node(''); } + // helper to get the index of a character + // eslint-disable-next-line class-methods-use-this + getIndexOfChar(char) { + return char.charCodeAt(0) - 'a'.charCodeAt(0); + } + insert(key) { if (!key) { return false; @@ -29,11 +35,39 @@ class Trie { return true; } - // helper to get the index of a character - // eslint-disable-next-line class-methods-use-this - getIndexOfChar(char) { - return char.charCodeAt(0) - 'a'.charCodeAt(0); + search(key) { + if (!key) { + return false; + } + + // convert word to lower case + const word = key.toLowerCase(); + let currentNode = this.root; + + for (let level = 0; level < word.length; level += 1) { + const index = this.getIndexOfChar(word[level]); + if (!currentNode.children[index]) { + return false; + } + currentNode = currentNode.children[index]; + } + if (currentNode !== null && currentNode.isEndOfWord) { + return true; + } + return false; } } +const words = ['bed', 'ball', 'apple', 'java', 'javascript']; +const trie = new Trie(); + +words.forEach(word => trie.insert(word)); + +console.log(trie.root); + +console.log(trie.search(words[3])); +console.log(trie.search('word')); +console.log(trie.search(words[4])); +console.log(trie.search('random')); + module.exports = Trie; From ddec5a6c4f4772fa9db9fa1bf4c76a970d816779 Mon Sep 17 00:00:00 2001 From: Ashok Dey Date: Sat, 19 Oct 2019 11:09:38 +0530 Subject: [PATCH 3/3] update: entry in readme --- README.md | 8 ++++---- src/_DataStructures_/Trees/Trie/index.js | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ae8cbe0f..6d258a35 100644 --- a/README.md +++ b/README.md @@ -31,17 +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 kth 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 - [Find Height of BST](src/_DataStructures_/Trees/BinarySearchTree/height-of-bst) - [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) ### Logical Problems diff --git a/src/_DataStructures_/Trees/Trie/index.js b/src/_DataStructures_/Trees/Trie/index.js index 8975eb7e..58222c96 100644 --- a/src/_DataStructures_/Trees/Trie/index.js +++ b/src/_DataStructures_/Trees/Trie/index.js @@ -58,16 +58,16 @@ class Trie { } } -const words = ['bed', 'ball', 'apple', 'java', 'javascript']; -const trie = new Trie(); +// const words = ['bed', 'ball', 'apple', 'java', 'javascript']; +// const trie = new Trie(); -words.forEach(word => trie.insert(word)); +// words.forEach(word => trie.insert(word)); -console.log(trie.root); +// console.log(trie.root); -console.log(trie.search(words[3])); -console.log(trie.search('word')); -console.log(trie.search(words[4])); -console.log(trie.search('random')); +// console.log(trie.search(words[3])); +// console.log(trie.search('word')); +// console.log(trie.search(words[4])); +// console.log(trie.search('random')); module.exports = Trie;