@@ -30,18 +30,18 @@ class Tree {
30
30
const queue = [ this . root ] ;
31
31
while ( queue . length ) {
32
32
const node = queue . shift ( ) ;
33
- for ( let i = 0 ; i < node . children . length ; i ++ ) {
34
- if ( node . children [ i ] . data === data ) {
35
- node . children . splice ( i , 1 ) ;
33
+ for ( let [ index , child ] of node . children . entries ( ) ) {
34
+ if ( child . data === data ) {
35
+ node . children . splice ( index , 1 ) ;
36
36
} else {
37
- queue . push ( node . children [ i ] ) ;
37
+ queue . push ( child ) ;
38
38
}
39
39
}
40
40
}
41
41
}
42
42
43
43
contains ( data ) {
44
- return this . findBFS ( data ) ? true : false ;
44
+ return ! ! this . findBFS ( data ) ;
45
45
}
46
46
47
47
findBFS ( data ) {
@@ -51,8 +51,8 @@ class Tree {
51
51
if ( node . data === data ) {
52
52
return node ;
53
53
}
54
- for ( let i = 0 ; i < node . children . length ; i ++ ) {
55
- queue . push ( node . children [ i ] ) ;
54
+ for ( const child of node . children ) {
55
+ queue . push ( child ) ;
56
56
}
57
57
}
58
58
return null ;
@@ -63,16 +63,16 @@ class Tree {
63
63
if ( fn ) {
64
64
fn ( node ) ;
65
65
}
66
- for ( let i = 0 ; i < node . children . length ; i ++ ) {
67
- this . _preOrder ( node . children [ i ] , fn ) ;
66
+ for ( const child of node . children ) {
67
+ this . _preOrder ( child , fn ) ;
68
68
}
69
69
}
70
70
}
71
71
72
72
_postOrder ( node , fn ) {
73
73
if ( node ) {
74
- for ( let i = 0 ; i < node . children . length ; i ++ ) {
75
- this . _postOrder ( node . children [ i ] , fn ) ;
74
+ for ( const child of node . children ) {
75
+ this . _postOrder ( child , fn ) ;
76
76
}
77
77
if ( fn ) {
78
78
fn ( node ) ;
@@ -96,8 +96,8 @@ class Tree {
96
96
if ( fn ) {
97
97
fn ( node ) ;
98
98
}
99
- for ( let i = 0 ; i < node . children . length ; i ++ ) {
100
- queue . push ( node . children [ i ] ) ;
99
+ for ( const child of node . children ) {
100
+ queue . push ( child ) ;
101
101
}
102
102
}
103
103
}
@@ -115,8 +115,8 @@ class Tree {
115
115
if ( node === newline && queue . length ) {
116
116
queue . push ( newline ) ;
117
117
}
118
- for ( let i = 0 ; i < node . children . length ; i ++ ) {
119
- queue . push ( node . children [ i ] ) ;
118
+ for ( const child of node . children ) {
119
+ queue . push ( child ) ;
120
120
}
121
121
}
122
122
console . log ( string . slice ( 0 , - 2 ) . trim ( ) ) ;
@@ -135,8 +135,8 @@ class Tree {
135
135
if ( node === newline && queue . length ) {
136
136
queue . push ( newline ) ;
137
137
}
138
- for ( let i = 0 ; i < node . children . length ; i ++ ) {
139
- queue . push ( node . children [ i ] ) ;
138
+ for ( const child of node . children ) {
139
+ queue . push ( child ) ;
140
140
}
141
141
}
142
142
console . log ( string . trim ( ) ) ;
0 commit comments