Skip to content

Commit a7ffba1

Browse files
committed
Extend Trie and TrieNode tests.
1 parent d25eff4 commit a7ffba1

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed

src/data-structures/trie/__test__/Trie.test.js

+20
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,31 @@ describe('Trie', () => {
2828

2929
trie.addWord('carpet');
3030
trie.addWord('car');
31+
trie.addWord('cat');
32+
trie.addWord('cart');
3133
expect(trie.doesWordExist('carpet')).toBe(true);
34+
expect(trie.doesWordExist('car')).toBe(true);
35+
expect(trie.doesWordExist('cart')).toBe(true);
36+
expect(trie.doesWordExist('cat')).toBe(true);
3237

3338
trie.deleteWord('carpet');
3439
expect(trie.doesWordExist('carpet')).toEqual(false);
3540
expect(trie.doesWordExist('car')).toEqual(true);
41+
expect(trie.doesWordExist('cart')).toBe(true);
42+
expect(trie.doesWordExist('cat')).toBe(true);
43+
44+
trie.deleteWord('cat');
45+
expect(trie.doesWordExist('car')).toEqual(true);
46+
expect(trie.doesWordExist('cart')).toBe(true);
47+
expect(trie.doesWordExist('cat')).toBe(false);
48+
49+
trie.deleteWord('car');
50+
expect(trie.doesWordExist('car')).toEqual(false);
51+
expect(trie.doesWordExist('cart')).toBe(true);
52+
53+
trie.deleteWord('cart');
54+
expect(trie.doesWordExist('car')).toEqual(false);
55+
expect(trie.doesWordExist('cart')).toBe(false);
3656
});
3757

3858
it('should suggests next characters', () => {

src/data-structures/trie/__test__/TrieNode.test.js

+28-30
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,6 @@ describe('TrieNode', () => {
1818
expect(trieNode.toString()).toBe('c:a,o');
1919
});
2020

21-
describe('removing child nodes', () => {
22-
it('should delete child node if the child node has NO children', () => {
23-
const trieNode = new TrieNode('c');
24-
trieNode.addChild('a');
25-
expect(trieNode.hasChild('a')).toBe(true);
26-
27-
trieNode.removeChild('a');
28-
expect(trieNode.hasChild('a')).toBe(false);
29-
});
30-
31-
it('should NOT delete child node if the child node has children', () => {
32-
const trieNode = new TrieNode('c');
33-
trieNode.addChild('a');
34-
const childNode = trieNode.getChild('a');
35-
childNode.addChild('r');
36-
37-
trieNode.removeChild('a');
38-
expect(trieNode.hasChild('a')).toEqual(true);
39-
});
40-
41-
it('should NOT delete child node if the child node completes a word', () => {
42-
const trieNode = new TrieNode('c');
43-
const IS_COMPLETE_WORD = true;
44-
trieNode.addChild('a', IS_COMPLETE_WORD);
45-
46-
trieNode.removeChild('a');
47-
expect(trieNode.hasChild('a')).toEqual(true);
48-
});
49-
});
50-
5121
it('should get child nodes', () => {
5222
const trieNode = new TrieNode('c');
5323

@@ -79,4 +49,32 @@ describe('TrieNode', () => {
7949

8050
expect(trieNode.suggestChildren()).toEqual(['a', 'o']);
8151
});
52+
53+
it('should delete child node if the child node has NO children', () => {
54+
const trieNode = new TrieNode('c');
55+
trieNode.addChild('a');
56+
expect(trieNode.hasChild('a')).toBe(true);
57+
58+
trieNode.removeChild('a');
59+
expect(trieNode.hasChild('a')).toBe(false);
60+
});
61+
62+
it('should NOT delete child node if the child node has children', () => {
63+
const trieNode = new TrieNode('c');
64+
trieNode.addChild('a');
65+
const childNode = trieNode.getChild('a');
66+
childNode.addChild('r');
67+
68+
trieNode.removeChild('a');
69+
expect(trieNode.hasChild('a')).toEqual(true);
70+
});
71+
72+
it('should NOT delete child node if the child node completes a word', () => {
73+
const trieNode = new TrieNode('c');
74+
const IS_COMPLETE_WORD = true;
75+
trieNode.addChild('a', IS_COMPLETE_WORD);
76+
77+
trieNode.removeChild('a');
78+
expect(trieNode.hasChild('a')).toEqual(true);
79+
});
8280
});

0 commit comments

Comments
 (0)