Fix TrieNode.addChild so substrings get marked as complete words (#177)

This commit is contained in:
Kevin Brewer 2018-08-25 05:27:44 -05:00 committed by Oleksii Trekhleb
parent 872a38fac6
commit eac3e81a21
2 changed files with 7 additions and 1 deletions

View File

@ -29,7 +29,10 @@ export default class TrieNode {
this.children.set(character, new TrieNode(character, isCompleteWord)); this.children.set(character, new TrieNode(character, isCompleteWord));
} }
return this.children.get(character); const childNode = this.children.get(character);
childNode.isCompleteWord = childNode.isCompleteWord || isCompleteWord;
return childNode;
} }
/** /**

View File

@ -41,11 +41,14 @@ describe('Trie', () => {
trie.addWord('cat'); trie.addWord('cat');
trie.addWord('cats'); trie.addWord('cats');
trie.addWord('carpet');
trie.addWord('car'); trie.addWord('car');
trie.addWord('caption'); trie.addWord('caption');
expect(trie.doesWordExist('cat')).toBe(true); expect(trie.doesWordExist('cat')).toBe(true);
expect(trie.doesWordExist('cats')).toBe(true); expect(trie.doesWordExist('cats')).toBe(true);
expect(trie.doesWordExist('carpet')).toBe(true);
expect(trie.doesWordExist('car')).toBe(true);
expect(trie.doesWordExist('cap')).toBe(false); expect(trie.doesWordExist('cap')).toBe(false);
expect(trie.doesWordExist('call')).toBe(false); expect(trie.doesWordExist('call')).toBe(false);
}); });