-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ccd42e6
commit 52cf4f0
Showing
5 changed files
with
138 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]+$/); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |