11
11
class BinarySearchTree implements \Iterator
12
12
{
13
13
/* the root of the tree */
14
- private $ root = null ;
14
+ private ? BstNode $ root = null ;
15
15
/* used for iterating over the tree */
16
- private $ iteratorPosition = false ;
16
+ private ? BstNode $ iteratorPosition = null ;
17
17
/* used for iterating through the tree */
18
- private $ iteratorStack = null ;
18
+ private ? Stack $ iteratorStack = null ;
19
19
/* if set, used to comparing non literal values */
20
20
private $ comparator = null ;
21
21
@@ -92,9 +92,7 @@ public function getRoot() : ?BstNode
92
92
*/
93
93
public function find ($ value , BstNode $ node = null ) : ?BstNode
94
94
{
95
- if ($ node === null ) {
96
- $ node = $ this ->root ;
97
- }
95
+ $ node ??= $ this ->root ;
98
96
99
97
if ($ this ->root === null ) {
100
98
return null ;
@@ -170,9 +168,7 @@ public function hasValues(...$values) : bool
170
168
*/
171
169
private function getMinNode (BstNode $ node = null ) : ?BstNode
172
170
{
173
- if ($ node === null ) {
174
- $ node = $ this ->root ;
175
- }
171
+ $ node ??= $ this ->root ;
176
172
177
173
if ($ this ->root === null ) {
178
174
return null ;
@@ -293,7 +289,7 @@ public function hasComparator() : bool
293
289
public function __clone ()
294
290
{
295
291
$ this ->root = clone $ this ->root ;
296
- $ this ->iteratorPosition = false ;
292
+ $ this ->iteratorPosition = null ;
297
293
$ this ->iteratorStack = null ;
298
294
}
299
295
@@ -332,12 +328,17 @@ public function next() : void
332
328
$ node = $ node ->getLeftChild ();
333
329
}
334
330
}
335
- $ this ->iteratorPosition = $ this ->iteratorStack ->peek ();
331
+ if ($ this ->iteratorStack ->peek () !== false ) {
332
+ $ this ->iteratorPosition = $ this ->iteratorStack ->peek ();
333
+ }
334
+ else {
335
+ $ this ->iteratorPosition = null ;
336
+ }
336
337
}
337
338
}
338
339
339
340
/**
340
- * sets the iterator to the start value, or the left ; most leaf of the tree
341
+ * sets the iterator to the start value, or the left most leaf of the tree
341
342
*/
342
343
public function rewind () : void
343
344
{
@@ -358,7 +359,7 @@ public function rewind() : void
358
359
*/
359
360
public function valid () : bool
360
361
{
361
- if ($ this ->iteratorPosition === false ) {
362
+ if ($ this ->iteratorPosition === null ) {
362
363
return false ;
363
364
}
364
365
return true ;
0 commit comments