@@ -18,6 +18,36 @@ 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+
2151 it ( 'should get child nodes' , ( ) => {
2252 const trieNode = new TrieNode ( 'c' ) ;
2353
0 commit comments