diff --git a/package.json b/package.json index c111a61..f58eb03 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "easy-trie", - "version": "1.0.1", + "version": "1.0.3", "description": "An easy to easy TypeScript implementation of the Trie data structure for ", "main": "bundle.js", "scripts": { diff --git a/spec/TrieSpec.ts b/spec/TrieSpec.ts index de90c0d..6dd2e98 100644 --- a/spec/TrieSpec.ts +++ b/spec/TrieSpec.ts @@ -147,6 +147,15 @@ describe("Testing class: Trie", () => { expect(actual).toEqual(expected); }) + + it("A long, common, prefix when the words are substrings of each other", () => { + let trie = new Trie(); + trie.addWords(["aaaaa", "aaaa", "aaa", "aaaab"]); + let expected = "aaa"; + let actual = trie.longestCommonPrefix(); + + expect(actual).toEqual(expected); + }); }); }); diff --git a/src/Trie.ts b/src/Trie.ts index 9008a84..4f47fbd 100644 --- a/src/Trie.ts +++ b/src/Trie.ts @@ -39,7 +39,7 @@ export = class Trie { word = acc.concat(node.letter); } - if (node.getChildNodeCount() != 1) { + if (node.getChildNodeCount() != 1 || node.isEndNode) { return word } let childNode = node.childNodes[Object.keys(node.childNodes)[0]];