mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Fix TrieNode.addChild so substrings get marked as complete words (#177)
This commit is contained in:
parent
872a38fac6
commit
eac3e81a21
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user