mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2026-03-13 08:51:02 +08:00
Add Trie.deleteWord and TrieNode.removeChild (#181)
This commit is contained in:
committed by
Oleksii Trekhleb
parent
6e2ff9b604
commit
d25eff49e6
@@ -23,6 +23,18 @@ describe('Trie', () => {
|
||||
expect(trie.head.getChild('c').getChild('a').getChild('t').toString()).toBe('t*');
|
||||
});
|
||||
|
||||
it('should delete words from trie', () => {
|
||||
const trie = new Trie();
|
||||
|
||||
trie.addWord('carpet');
|
||||
trie.addWord('car');
|
||||
expect(trie.doesWordExist('carpet')).toBe(true);
|
||||
|
||||
trie.deleteWord('carpet');
|
||||
expect(trie.doesWordExist('carpet')).toEqual(false);
|
||||
expect(trie.doesWordExist('car')).toEqual(true);
|
||||
});
|
||||
|
||||
it('should suggests next characters', () => {
|
||||
const trie = new Trie();
|
||||
|
||||
|
||||
@@ -18,6 +18,36 @@ describe('TrieNode', () => {
|
||||
expect(trieNode.toString()).toBe('c:a,o');
|
||||
});
|
||||
|
||||
describe('removing child nodes', () => {
|
||||
it('should delete child node if the child node has NO children', () => {
|
||||
const trieNode = new TrieNode('c');
|
||||
trieNode.addChild('a');
|
||||
expect(trieNode.hasChild('a')).toBe(true);
|
||||
|
||||
trieNode.removeChild('a');
|
||||
expect(trieNode.hasChild('a')).toBe(false);
|
||||
});
|
||||
|
||||
it('should NOT delete child node if the child node has children', () => {
|
||||
const trieNode = new TrieNode('c');
|
||||
trieNode.addChild('a');
|
||||
const childNode = trieNode.getChild('a');
|
||||
childNode.addChild('r');
|
||||
|
||||
trieNode.removeChild('a');
|
||||
expect(trieNode.hasChild('a')).toEqual(true);
|
||||
});
|
||||
|
||||
it('should NOT delete child node if the child node completes a word', () => {
|
||||
const trieNode = new TrieNode('c');
|
||||
const IS_COMPLETE_WORD = true;
|
||||
trieNode.addChild('a', IS_COMPLETE_WORD);
|
||||
|
||||
trieNode.removeChild('a');
|
||||
expect(trieNode.hasChild('a')).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
it('should get child nodes', () => {
|
||||
const trieNode = new TrieNode('c');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user