From 392cd9806dcc898b996a119552e545722afcc864 Mon Sep 17 00:00:00 2001 From: Oleg Date: Tue, 21 Aug 2018 23:00:11 +0300 Subject: [PATCH] Fix method Trie::doesWordExist() (#175) Method Trie::doesWordExist() return `true` when word is complete otherwise `false` --- src/data-structures/trie/Trie.js | 4 +++- src/data-structures/trie/__test__/Trie.test.js | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/data-structures/trie/Trie.js b/src/data-structures/trie/Trie.js index 8eff5a05..39f25820 100644 --- a/src/data-structures/trie/Trie.js +++ b/src/data-structures/trie/Trie.js @@ -43,7 +43,9 @@ export default class Trie { * @return {boolean} */ doesWordExist(word) { - return !!this.getLastCharacterNode(word); + const lastCharacter = this.getLastCharacterNode(word); + + return !!lastCharacter && lastCharacter.isCompleteWord; } /** diff --git a/src/data-structures/trie/__test__/Trie.test.js b/src/data-structures/trie/__test__/Trie.test.js index 75867143..b66db94a 100644 --- a/src/data-structures/trie/__test__/Trie.test.js +++ b/src/data-structures/trie/__test__/Trie.test.js @@ -45,7 +45,8 @@ describe('Trie', () => { trie.addWord('caption'); expect(trie.doesWordExist('cat')).toBe(true); - expect(trie.doesWordExist('cap')).toBe(true); + expect(trie.doesWordExist('cats')).toBe(true); + expect(trie.doesWordExist('cap')).toBe(false); expect(trie.doesWordExist('call')).toBe(false); }); });