Add annotations to Trie.

This commit is contained in:
Oleksii Trekhleb 2018-07-26 18:02:34 +03:00
parent 39acb2b65d
commit 80e3216609
3 changed files with 46 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import TrieNode from './TrieNode';
// Character that we will use for trie tree root.
const HEAD_CHARACTER = '*';
export default class Trie {
@ -7,15 +8,26 @@ export default class Trie {
this.head = new TrieNode(HEAD_CHARACTER);
}
/**
* @param {string} word
* @return {Trie}
*/
addWord(word) {
const characters = Array.from(word);
let currentNode = this.head;
for (let charIndex = 0; charIndex < characters.length; charIndex += 1) {
const isComplete = charIndex === characters.length - 1;
currentNode = currentNode.addChild(characters[charIndex], isComplete);
}
return this;
}
/**
* @param {string} word
* @return {string[]}
*/
suggestNextCharacters(word) {
const lastCharacter = this.getLastCharacterNode(word);
@ -26,17 +38,27 @@ export default class Trie {
return lastCharacter.suggestChildren();
}
/**
* @param {string} word
* @return {boolean}
*/
doesWordExist(word) {
return !!this.getLastCharacterNode(word);
}
/**
* @param {string} word
* @return {TrieNode}
*/
getLastCharacterNode(word) {
const characters = Array.from(word);
let currentNode = this.head;
for (let charIndex = 0; charIndex < characters.length; charIndex += 1) {
if (!currentNode.hasChild(characters[charIndex])) {
return null;
}
currentNode = currentNode.getChild(characters[charIndex]);
}

View File

@ -1,16 +1,29 @@
import HashTable from '../hash-table/HashTable';
export default class TrieNode {
/**
* @param {string} character
* @param {boolean} isCompleteWord
*/
constructor(character, isCompleteWord = false) {
this.character = character;
this.isCompleteWord = isCompleteWord;
this.children = new HashTable();
}
/**
* @param {string} character
* @return {TrieNode}
*/
getChild(character) {
return this.children.get(character);
}
/**
* @param {string} character
* @param {boolean} isCompleteWord
* @return {TrieNode}
*/
addChild(character, isCompleteWord = false) {
if (!this.children.has(character)) {
this.children.set(character, new TrieNode(character, isCompleteWord));
@ -19,14 +32,24 @@ export default class TrieNode {
return this.children.get(character);
}
/**
* @param {string} character
* @return {boolean}
*/
hasChild(character) {
return this.children.has(character);
}
/**
* @return {string[]}
*/
suggestChildren() {
return [...this.children.getKeys()];
}
/**
* @return {string}
*/
toString() {
let childrenAsString = this.suggestChildren().toString();
childrenAsString = childrenAsString ? `:${childrenAsString}` : '';

View File

@ -25,6 +25,7 @@ describe('TrieNode', () => {
trieNode.addChild('o');
expect(trieNode.getChild('a').toString()).toBe('a');
expect(trieNode.getChild('a').character).toBe('a');
expect(trieNode.getChild('o').toString()).toBe('o');
expect(trieNode.getChild('b')).toBeUndefined();
});