Skip to content

Commit

Permalink
Take terminal node into consideration when calculating prefix (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
arghasarkar committed Jun 17, 2022
1 parent 02c5e77 commit efbac73
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
9 changes: 9 additions & 0 deletions spec/TrieSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

});
Expand Down
2 changes: 1 addition & 1 deletion src/Trie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand Down

0 comments on commit efbac73

Please sign in to comment.