File tree 2 files changed +26
-12
lines changed
2 files changed +26
-12
lines changed Original file line number Diff line number Diff line change @@ -42,20 +42,16 @@ export default class TrieNode {
42
42
* @return {TrieNode }
43
43
*/
44
44
removeChild ( character ) {
45
- function isSafeToDelete ( node ) {
46
- return (
47
- node
48
- && ! node . isCompleteWord
49
- && node . children . getKeys ( ) . length === 0
50
- ) ;
51
- }
52
-
53
45
const childNode = this . getChild ( character ) ;
54
46
55
- // delete childNode only if:
56
- // - childNode has NO children
57
- // - childNode.isCompleteWord === false
58
- if ( isSafeToDelete ( childNode ) ) {
47
+ // Delete childNode only if:
48
+ // - childNode has NO children,
49
+ // - childNode.isCompleteWord === false.
50
+ if (
51
+ childNode
52
+ && ! childNode . isCompleteWord
53
+ && ! childNode . hasChildren ( )
54
+ ) {
59
55
this . children . delete ( character ) ;
60
56
}
61
57
@@ -70,6 +66,14 @@ export default class TrieNode {
70
66
return this . children . has ( character ) ;
71
67
}
72
68
69
+ /**
70
+ * Check whether current TrieNode has children or not.
71
+ * @return {boolean }
72
+ */
73
+ hasChildren ( ) {
74
+ return this . children . getKeys ( ) . length !== 0 ;
75
+ }
76
+
73
77
/**
74
78
* @return {string[] }
75
79
*/
Original file line number Diff line number Diff line change @@ -30,6 +30,16 @@ describe('TrieNode', () => {
30
30
expect ( trieNode . getChild ( 'b' ) ) . toBeUndefined ( ) ;
31
31
} ) ;
32
32
33
+ it ( 'should check if node has children' , ( ) => {
34
+ const trieNode = new TrieNode ( 'c' ) ;
35
+
36
+ expect ( trieNode . hasChildren ( ) ) . toBe ( false ) ;
37
+
38
+ trieNode . addChild ( 'a' ) ;
39
+
40
+ expect ( trieNode . hasChildren ( ) ) . toBe ( true ) ;
41
+ } ) ;
42
+
33
43
it ( 'should check if node has specific child' , ( ) => {
34
44
const trieNode = new TrieNode ( 'c' ) ;
35
45
You can’t perform that action at this time.
0 commit comments