Skip to content

Commit

Permalink
Added trie
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonheecs committed May 24, 2018
1 parent ccd42e6 commit 52cf4f0
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 19 deletions.
40 changes: 23 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"eslint-plugin-standard": "^3.0.1",
"husky": "^0.14.3",
"mocha": "^5.1.1",
"nyc": "^11.7.1"
"nyc": "^11.7.1",
"random-words": "^1.1.0"
}
}
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import CircularList from './circular-linked-list.js';
import Stack from './stack.js';
import Queue from './queue.js';
import BST from './binary-search-tree.js';
import Trie from './trie.js';

export {
List,
DoublyList,
CircularList,
Stack,
Queue,
BST
BST,
Trie
};
81 changes: 81 additions & 0 deletions src/trie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

class Node {
constructor (value = null, isCompleteWord = false) {
this.value = value;
this.children = new Map();
this.isCompleteWord = isCompleteWord;
}

/**
* @param {char} value
* @param {Node} node
*/
setChild (value, node) {
this.children.set(value, node);
}

/**
* @param {char} value
* @return {Node}
*/
getChild (value) {
return this.children.get(value);
}
}

export default class Trie {
constructor () {
this.root = new Node();
}

/**
* @param {string} word
*/
addWord (word) {
if (!this.isValidWord(word)) {
throw Error('Invalid Word');
}

let current = this.root;

Array.from(word).forEach((letter) => {
if (!current.getChild(letter)) {
current.setChild(letter, new Node(letter));
}

current = current.getChild(letter);
});

current.isCompleteWord = true;
}

/**
* @return {Array<string>}
*/
getWords () {
let words = [];

const getWords = (node, wordFragment) => {
node.children.forEach((child) => {
if (child.isCompleteWord) {
words.push(wordFragment + child.value);
}

getWords(child, wordFragment + child.value);
});
};

getWords(this.root, '');

return words;
}

/**
* @param {string} word
* @return {Boolean}
*/
isValidWord (word) {
return (typeof (word) === 'string' || word instanceof String) && word.match(/^[A-z]+$/);
}
}
29 changes: 29 additions & 0 deletions test/trie-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

import {Trie} from '../src/index.js';

const randomWords = require('random-words');
const expect = require('chai').expect;

describe('testing trie', function () {
it('test adding values', function () {
let trie = new Trie();
let words = randomWords(50);

words.forEach((word) => {
trie.addWord(word);
});

expect(trie.getWords()).to.include.members(words);
});

it('test adding invalid values', function () {
let trie = new Trie();

let invalidInputs = ['', 123, '$%^&', '123'];

invalidInputs.forEach((input) => {
expect(trie.addWord.bind(trie, input)).to.throw(Error, 'Invalid Word');
});
});
});

0 comments on commit 52cf4f0

Please sign in to comment.