mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 15:11:16 +08:00
Extend Trie and TrieNode tests.
This commit is contained in:
parent
d25eff49e6
commit
a7ffba157c
@ -28,11 +28,31 @@ describe('Trie', () => {
|
||||
|
||||
trie.addWord('carpet');
|
||||
trie.addWord('car');
|
||||
trie.addWord('cat');
|
||||
trie.addWord('cart');
|
||||
expect(trie.doesWordExist('carpet')).toBe(true);
|
||||
expect(trie.doesWordExist('car')).toBe(true);
|
||||
expect(trie.doesWordExist('cart')).toBe(true);
|
||||
expect(trie.doesWordExist('cat')).toBe(true);
|
||||
|
||||
trie.deleteWord('carpet');
|
||||
expect(trie.doesWordExist('carpet')).toEqual(false);
|
||||
expect(trie.doesWordExist('car')).toEqual(true);
|
||||
expect(trie.doesWordExist('cart')).toBe(true);
|
||||
expect(trie.doesWordExist('cat')).toBe(true);
|
||||
|
||||
trie.deleteWord('cat');
|
||||
expect(trie.doesWordExist('car')).toEqual(true);
|
||||
expect(trie.doesWordExist('cart')).toBe(true);
|
||||
expect(trie.doesWordExist('cat')).toBe(false);
|
||||
|
||||
trie.deleteWord('car');
|
||||
expect(trie.doesWordExist('car')).toEqual(false);
|
||||
expect(trie.doesWordExist('cart')).toBe(true);
|
||||
|
||||
trie.deleteWord('cart');
|
||||
expect(trie.doesWordExist('car')).toEqual(false);
|
||||
expect(trie.doesWordExist('cart')).toBe(false);
|
||||
});
|
||||
|
||||
it('should suggests next characters', () => {
|
||||
|
@ -18,36 +18,6 @@ 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');
|
||||
|
||||
@ -79,4 +49,32 @@ describe('TrieNode', () => {
|
||||
|
||||
expect(trieNode.suggestChildren()).toEqual(['a', 'o']);
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user