1
1
<?php
2
+
2
3
namespace Danon \IntervalTree ;
3
4
5
+ use Iterator ;
6
+
4
7
class IntervalTree
5
8
{
6
9
public $ root ;
@@ -11,7 +14,6 @@ class IntervalTree
11
14
*/
12
15
public function __construct ()
13
16
{
14
- $ this ->root = null ;
15
17
$ this ->nilNode = new Node ();
16
18
}
17
19
@@ -88,9 +90,9 @@ public function isEmpty()
88
90
* Iterator of nodes which keys intersect with given interval
89
91
* If no values stored in the tree, returns array of keys which intersect given interval
90
92
* @param array $interval
91
- * @return iterable
93
+ * @return Iterator
92
94
*/
93
- public function iterateIntersections (array $ interval ): iterable
95
+ public function iterateIntersections (array $ interval ): Iterator
94
96
{
95
97
$ searchNode = new Node ($ interval );
96
98
yield from $ this ->treeSearchInterval ($ this ->root , $ searchNode );
@@ -179,7 +181,7 @@ public function remove($key, $value): bool
179
181
*/
180
182
public function foreach ($ visitor )
181
183
{
182
- $ this ->treeWalk ($ this ->root , function ($ node ) {
184
+ $ this ->treeWalk ($ this ->root , function ($ node ) use ( $ visitor ) {
183
185
return $ visitor ($ node ->item ->key , $ node ->item ->value );
184
186
});
185
187
}
@@ -237,7 +239,7 @@ public function treeInsert($insertNode)
237
239
}
238
240
239
241
// After insertion insert_node may have red-colored parent, and this is a single possible violation
240
- // Go upwords to the root and re-color until violation will be resolved
242
+ // Go upwards to the root and re-color until violation will be resolved
241
243
public function insertFixup ($ insertNode )
242
244
{
243
245
$ currentNode = null ;
@@ -291,8 +293,8 @@ public function insertFixup($insertNode)
291
293
292
294
public function treeDelete ($ deleteNode )
293
295
{
294
- $ cutNode ; // node to be cut - either delete_node or successor_node ("y" from 14.4)
295
- $ fixNode ; // node to fix rb tree property ("x" from 14.4)
296
+ $ cutNode = null ; // node to be cut - either delete_node or successor_node ("y" from 14.4)
297
+ $ fixNode = null ; // node to fix rb tree property ("x" from 14.4)
296
298
297
299
if ($ deleteNode ->left === $ this ->nilNode || $ deleteNode ->right === $ this ->nilNode ) { // delete_node has less then 2 children
298
300
$ cutNode = $ deleteNode ;
@@ -307,10 +309,7 @@ public function treeDelete($deleteNode)
307
309
$ fixNode = $ cutNode ->right ;
308
310
}
309
311
310
- // remove cut_node from parent
311
- /*if (fix_node !== this.nil_node) {*/
312
312
$ fixNode ->parent = $ cutNode ->parent ;
313
- /*}*/
314
313
315
314
if ($ cutNode === $ this ->root ) {
316
315
$ this ->root = $ fixNode ;
@@ -341,7 +340,6 @@ public function treeDelete($deleteNode)
341
340
public function deleteFixup ($ fixNode )
342
341
{
343
342
$ currentNode = $ fixNode ;
344
- $ brotherNode ;
345
343
346
344
while ($ currentNode !== $ this ->root && $ currentNode ->parent !== null && $ currentNode ->color === Node::COLOR_BLACK ) {
347
345
if ($ currentNode === $ currentNode ->parent ->left ) { // fix node is left child
@@ -353,8 +351,10 @@ public function deleteFixup($fixNode)
353
351
$ brotherNode = $ currentNode ->parent ->right ; // update brother
354
352
}
355
353
// Derive to cases 2..4: brother is black
356
- if ($ brotherNode ->left ->color === Node::COLOR_BLACK &&
357
- $ brotherNode ->right ->color === Node::COLOR_BLACK ) { // case 2: both nephews black
354
+ if (
355
+ $ brotherNode ->left ->color === Node::COLOR_BLACK &&
356
+ $ brotherNode ->right ->color === Node::COLOR_BLACK
357
+ ) { // case 2: both nephews black
358
358
$ brotherNode ->color = Node::COLOR_RED ; // re-color brother
359
359
$ currentNode = $ currentNode ->parent ; // continue iteration
360
360
} else {
@@ -381,8 +381,10 @@ public function deleteFixup($fixNode)
381
381
$ brotherNode = $ currentNode ->parent ->left ; // update brother
382
382
}
383
383
// Go to cases 2..4
384
- if ($ brotherNode ->left ->color === Node::COLOR_BLACK &&
385
- $ brotherNode ->right ->color === Node::COLOR_BLACK ) { // case 2
384
+ if (
385
+ $ brotherNode ->left ->color === Node::COLOR_BLACK &&
386
+ $ brotherNode ->right ->color === Node::COLOR_BLACK
387
+ ) { // case 2
386
388
$ brotherNode ->color = Node::COLOR_RED ; // re-color brother
387
389
$ currentNode = $ currentNode ->parent ; // continue iteration
388
390
} else {
@@ -464,10 +466,6 @@ public function localMaximum($node)
464
466
465
467
public function treeSuccessor ($ node )
466
468
{
467
- $ nodeSuccessor ;
468
- $ currentNode ;
469
- $ parentNode ;
470
-
471
469
if ($ node ->right !== $ this ->nilNode ) {
472
470
$ nodeSuccessor = $ this ->localMinimum ($ node ->right );
473
471
} else {
@@ -599,7 +597,7 @@ public function testBlackHeightProperty($node)
599
597
$ heightRight = 1 ;
600
598
}
601
599
if ($ heightLeft !== $ heightRight ) {
602
- throw new Error ('Red-black height property violated ' );
600
+ throw new \ Exception ('Red-black height property violated ' );
603
601
}
604
602
$ height += $ heightLeft ;
605
603
return $ height ;
0 commit comments