Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4104155

Browse files
committed
Simplify deletion method of TrieNode.
1 parenta7ffba1 commit4104155

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

‎src/data-structures/trie/TrieNode.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,16 @@ export default class TrieNode {
4242
*@return {TrieNode}
4343
*/
4444
removeChild(character){
45-
functionisSafeToDelete(node){
46-
return(
47-
node
48-
&&!node.isCompleteWord
49-
&&node.children.getKeys().length===0
50-
);
51-
}
52-
5345
constchildNode=this.getChild(character);
5446

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+
){
5955
this.children.delete(character);
6056
}
6157

@@ -70,6 +66,14 @@ export default class TrieNode {
7066
returnthis.children.has(character);
7167
}
7268

69+
/**
70+
* Check whether current TrieNode has children or not.
71+
*@return {boolean}
72+
*/
73+
hasChildren(){
74+
returnthis.children.getKeys().length!==0;
75+
}
76+
7377
/**
7478
*@return {string[]}
7579
*/

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ describe('TrieNode', () => {
3030
expect(trieNode.getChild('b')).toBeUndefined();
3131
});
3232

33+
it('should check if node has children',()=>{
34+
consttrieNode=newTrieNode('c');
35+
36+
expect(trieNode.hasChildren()).toBe(false);
37+
38+
trieNode.addChild('a');
39+
40+
expect(trieNode.hasChildren()).toBe(true);
41+
});
42+
3343
it('should check if node has specific child',()=>{
3444
consttrieNode=newTrieNode('c');
3545

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp