mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-09-20 07:43:04 +08:00
Add annotations to Trie.
This commit is contained in:
parent
39acb2b65d
commit
80e3216609
@ -1,5 +1,6 @@
|
|||||||
import TrieNode from './TrieNode';
|
import TrieNode from './TrieNode';
|
||||||
|
|
||||||
|
// Character that we will use for trie tree root.
|
||||||
const HEAD_CHARACTER = '*';
|
const HEAD_CHARACTER = '*';
|
||||||
|
|
||||||
export default class Trie {
|
export default class Trie {
|
||||||
@ -7,15 +8,26 @@ export default class Trie {
|
|||||||
this.head = new TrieNode(HEAD_CHARACTER);
|
this.head = new TrieNode(HEAD_CHARACTER);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} word
|
||||||
|
* @return {Trie}
|
||||||
|
*/
|
||||||
addWord(word) {
|
addWord(word) {
|
||||||
const characters = Array.from(word);
|
const characters = Array.from(word);
|
||||||
let currentNode = this.head;
|
let currentNode = this.head;
|
||||||
|
|
||||||
for (let charIndex = 0; charIndex < characters.length; charIndex += 1) {
|
for (let charIndex = 0; charIndex < characters.length; charIndex += 1) {
|
||||||
const isComplete = charIndex === characters.length - 1;
|
const isComplete = charIndex === characters.length - 1;
|
||||||
currentNode = currentNode.addChild(characters[charIndex], isComplete);
|
currentNode = currentNode.addChild(characters[charIndex], isComplete);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} word
|
||||||
|
* @return {string[]}
|
||||||
|
*/
|
||||||
suggestNextCharacters(word) {
|
suggestNextCharacters(word) {
|
||||||
const lastCharacter = this.getLastCharacterNode(word);
|
const lastCharacter = this.getLastCharacterNode(word);
|
||||||
|
|
||||||
@ -26,17 +38,27 @@ export default class Trie {
|
|||||||
return lastCharacter.suggestChildren();
|
return lastCharacter.suggestChildren();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} word
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
doesWordExist(word) {
|
doesWordExist(word) {
|
||||||
return !!this.getLastCharacterNode(word);
|
return !!this.getLastCharacterNode(word);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} word
|
||||||
|
* @return {TrieNode}
|
||||||
|
*/
|
||||||
getLastCharacterNode(word) {
|
getLastCharacterNode(word) {
|
||||||
const characters = Array.from(word);
|
const characters = Array.from(word);
|
||||||
let currentNode = this.head;
|
let currentNode = this.head;
|
||||||
|
|
||||||
for (let charIndex = 0; charIndex < characters.length; charIndex += 1) {
|
for (let charIndex = 0; charIndex < characters.length; charIndex += 1) {
|
||||||
if (!currentNode.hasChild(characters[charIndex])) {
|
if (!currentNode.hasChild(characters[charIndex])) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
currentNode = currentNode.getChild(characters[charIndex]);
|
currentNode = currentNode.getChild(characters[charIndex]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,16 +1,29 @@
|
|||||||
import HashTable from '../hash-table/HashTable';
|
import HashTable from '../hash-table/HashTable';
|
||||||
|
|
||||||
export default class TrieNode {
|
export default class TrieNode {
|
||||||
|
/**
|
||||||
|
* @param {string} character
|
||||||
|
* @param {boolean} isCompleteWord
|
||||||
|
*/
|
||||||
constructor(character, isCompleteWord = false) {
|
constructor(character, isCompleteWord = false) {
|
||||||
this.character = character;
|
this.character = character;
|
||||||
this.isCompleteWord = isCompleteWord;
|
this.isCompleteWord = isCompleteWord;
|
||||||
this.children = new HashTable();
|
this.children = new HashTable();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} character
|
||||||
|
* @return {TrieNode}
|
||||||
|
*/
|
||||||
getChild(character) {
|
getChild(character) {
|
||||||
return this.children.get(character);
|
return this.children.get(character);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} character
|
||||||
|
* @param {boolean} isCompleteWord
|
||||||
|
* @return {TrieNode}
|
||||||
|
*/
|
||||||
addChild(character, isCompleteWord = false) {
|
addChild(character, isCompleteWord = false) {
|
||||||
if (!this.children.has(character)) {
|
if (!this.children.has(character)) {
|
||||||
this.children.set(character, new TrieNode(character, isCompleteWord));
|
this.children.set(character, new TrieNode(character, isCompleteWord));
|
||||||
@ -19,14 +32,24 @@ export default class TrieNode {
|
|||||||
return this.children.get(character);
|
return this.children.get(character);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} character
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
hasChild(character) {
|
hasChild(character) {
|
||||||
return this.children.has(character);
|
return this.children.has(character);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {string[]}
|
||||||
|
*/
|
||||||
suggestChildren() {
|
suggestChildren() {
|
||||||
return [...this.children.getKeys()];
|
return [...this.children.getKeys()];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return {string}
|
||||||
|
*/
|
||||||
toString() {
|
toString() {
|
||||||
let childrenAsString = this.suggestChildren().toString();
|
let childrenAsString = this.suggestChildren().toString();
|
||||||
childrenAsString = childrenAsString ? `:${childrenAsString}` : '';
|
childrenAsString = childrenAsString ? `:${childrenAsString}` : '';
|
||||||
|
@ -25,6 +25,7 @@ describe('TrieNode', () => {
|
|||||||
trieNode.addChild('o');
|
trieNode.addChild('o');
|
||||||
|
|
||||||
expect(trieNode.getChild('a').toString()).toBe('a');
|
expect(trieNode.getChild('a').toString()).toBe('a');
|
||||||
|
expect(trieNode.getChild('a').character).toBe('a');
|
||||||
expect(trieNode.getChild('o').toString()).toBe('o');
|
expect(trieNode.getChild('o').toString()).toBe('o');
|
||||||
expect(trieNode.getChild('b')).toBeUndefined();
|
expect(trieNode.getChild('b')).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user