diff --git a/src/data-structures/trie/TrieNode.js b/src/data-structures/trie/TrieNode.js index cf89fdd6..f24910f1 100644 --- a/src/data-structures/trie/TrieNode.js +++ b/src/data-structures/trie/TrieNode.js @@ -29,7 +29,10 @@ export default class TrieNode { 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; } /** diff --git a/src/data-structures/trie/__test__/Trie.test.js b/src/data-structures/trie/__test__/Trie.test.js index b66db94a..194ca16a 100644 --- a/src/data-structures/trie/__test__/Trie.test.js +++ b/src/data-structures/trie/__test__/Trie.test.js @@ -41,11 +41,14 @@ describe('Trie', () => { trie.addWord('cat'); trie.addWord('cats'); + trie.addWord('carpet'); trie.addWord('car'); trie.addWord('caption'); expect(trie.doesWordExist('cat')).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('call')).toBe(false); });