@@ -181,10 +181,6 @@ private function treeInsert(Node $insertNode): void
181
181
}
182
182
183
183
/**
184
- * After insertion insert_node may have red-colored parent
185
- * And this is a single possible violation
186
- * Go upwards to the root and re-color until violation will be resolved
187
- *
188
184
* @param Node<TPoint, TValue> $insertNode
189
185
*/
190
186
private function insertFixup (Node $ insertNode ): void
@@ -241,7 +237,6 @@ private function treeDelete(Node $deleteNode): void
241
237
$ cutNode = $ this ->treeSuccessor ($ deleteNode );
242
238
}
243
239
244
- // fix_node if single child of cut_node
245
240
if ($ cutNode ->getLeft () !== $ this ->nilNode ) {
246
241
$ fixNode = $ cutNode ->getLeft ();
247
242
} else {
@@ -258,17 +253,15 @@ private function treeDelete(Node $deleteNode): void
258
253
} else {
259
254
$ cutNode ->getParent ()->setRight ($ fixNode );
260
255
}
261
- $ cutNode ->getParent ()->updateMax (); // update max property of the parent
256
+ $ cutNode ->getParent ()->updateMax ();
262
257
}
263
258
264
- $ this ->recalculateMax ($ fixNode ); // update max property upward from fix_node to root
259
+ $ this ->recalculateMax ($ fixNode );
265
260
266
- // deleteNode becomes cutNode, it means that we cannot hold reference
267
- // to node in outer structure and we will have to delete by key, additional search need
268
261
if ($ cutNode !== $ deleteNode ) {
269
262
$ deleteNode ->copyPairFrom ($ cutNode );
270
- $ deleteNode ->updateMax (); // update max property of the cut node at the new place
271
- $ this ->recalculateMax ($ deleteNode ); // update max property upward from deleteNode to root
263
+ $ deleteNode ->updateMax ();
264
+ $ this ->recalculateMax ($ deleteNode );
272
265
}
273
266
274
267
if ($ cutNode ->getColor ()->isBlack ()) {
@@ -305,7 +298,6 @@ private function deleteFixup(Node $fixNode): void
305
298
$ brotherNode ->setColor (NodeColor::red ());
306
299
$ brotherNode ->getLeft ()->setColor (NodeColor::black ());
307
300
$ this ->rotateRight ($ brotherNode );
308
- $ brotherNode = $ currentNode ->getParent ()->getRight ();
309
301
}
310
302
$ brotherNode ->setColor ($ currentNode ->getParent ()->getColor ());
311
303
$ currentNode ->getParent ()->setColor (NodeColor::black ());
@@ -329,7 +321,6 @@ private function deleteFixup(Node $fixNode): void
329
321
$ brotherNode ->setColor (NodeColor::red ());
330
322
$ brotherNode ->getRight ()->setColor (NodeColor::black ());
331
323
$ this ->rotateLeft ($ brotherNode );
332
- $ brotherNode = $ currentNode ->getParent ()->getLeft ();
333
324
}
334
325
$ brotherNode ->setColor ($ currentNode ->getParent ()->getColor ());
335
326
$ currentNode ->getParent ()->setColor (NodeColor::black ());
@@ -344,25 +335,25 @@ private function deleteFixup(Node $fixNode): void
344
335
}
345
336
346
337
/**
338
+ * @param Node<TPoint, TValue> $startingNode
347
339
* @param Node<TPoint, TValue> $node
348
- * @param Node<TPoint, TValue> $searchNode
349
340
* @return Node<TPoint, TValue>|null
350
341
*/
351
- private function treeSearch (Node $ node , Node $ searchNode ): ?Node
342
+ private function treeSearch (Node $ startingNode , Node $ node ): ?Node
352
343
{
353
- if ($ node === $ this ->nilNode ) {
344
+ if ($ startingNode === $ this ->nilNode ) {
354
345
return null ;
355
346
}
356
347
357
- if ($ searchNode ->equalTo ($ node )) {
358
- return $ node ;
359
- }
360
-
361
- if ( $ searchNode -> lessThan ( $ node )) {
362
- return $ this ->treeSearch ($ node -> getLeft (), $ searchNode );
348
+ if ($ node ->equalTo ($ startingNode )) {
349
+ $ searchedNode = $ startingNode ;
350
+ } elseif ( $ node -> lessThan ( $ startingNode )) {
351
+ $ searchedNode = $ this -> treeSearch ( $ startingNode -> getLeft (), $ node );
352
+ } else {
353
+ $ searchedNode = $ this ->treeSearch ($ startingNode -> getRight (), $ node );
363
354
}
364
355
365
- return $ this -> treeSearch ( $ node -> getRight (), $ searchNode ) ;
356
+ return $ searchedNode ;
366
357
}
367
358
368
359
/**
@@ -424,22 +415,22 @@ private function treeSuccessor(Node $node): ?Node
424
415
private function rotateLeft (Node $ x ): void
425
416
{
426
417
$ y = $ x ->getRight ();
427
- $ x ->setRight ($ y ->getLeft ()); // b goes to x.right
418
+ $ x ->setRight ($ y ->getLeft ());
428
419
429
420
if ($ y ->getLeft () !== $ this ->nilNode ) {
430
- $ y ->getLeft ()->setParent ($ x ); // x becomes parent of b
421
+ $ y ->getLeft ()->setParent ($ x );
431
422
}
432
- $ y ->setParent ($ x ->getParent ()); // move parent
423
+ $ y ->setParent ($ x ->getParent ());
433
424
434
425
if ($ x ->getParent () === null ) {
435
- $ this ->root = $ y ; // y becomes root
426
+ $ this ->root = $ y ;
436
427
} elseif ($ x === $ x ->getParent ()->getLeft ()) {
437
428
$ x ->getParent ()->setLeft ($ y );
438
429
} else {
439
430
$ x ->getParent ()->setRight ($ y );
440
431
}
441
- $ y ->setLeft ($ x ); // x becomes left child of y
442
- $ x ->setParent ($ y ); // and y becomes parent of x
432
+ $ y ->setLeft ($ x );
433
+ $ x ->setParent ($ y );
443
434
444
435
if ($ x !== $ this ->nilNode ) {
445
436
$ x ->updateMax ();
@@ -459,22 +450,22 @@ private function rotateRight(Node $y): void
459
450
{
460
451
$ x = $ y ->getLeft ();
461
452
462
- $ y ->setLeft ($ x ->getRight ()); // b goes to y.left
453
+ $ y ->setLeft ($ x ->getRight ());
463
454
464
455
if ($ x ->getRight () !== $ this ->nilNode ) {
465
- $ x ->getRight ()->setParent ($ y ); // y becomes parent of b
456
+ $ x ->getRight ()->setParent ($ y );
466
457
}
467
- $ x ->setParent ($ y ->getParent ()); // move parent
458
+ $ x ->setParent ($ y ->getParent ());
468
459
469
- if ($ y ->getParent () === null ) { // x becomes root
460
+ if ($ y ->getParent () === null ) {
470
461
$ this ->root = $ x ;
471
462
} elseif ($ y === $ y ->getParent ()->getLeft ()) {
472
463
$ y ->getParent ()->setLeft ($ x );
473
464
} else {
474
465
$ y ->getParent ()->setRight ($ x );
475
466
}
476
- $ x ->setRight ($ y ); // y becomes right child of x
477
- $ y ->setParent ($ x ); // and x becomes parent of y
467
+ $ x ->setRight ($ y );
468
+ $ y ->setParent ($ x );
478
469
479
470
if ($ y !== $ this ->nilNode ) {
480
471
$ y ->updateMax ();
@@ -489,7 +480,7 @@ private function rotateRight(Node $y): void
489
480
/**
490
481
* @return Iterator<Node<TPoint, TValue>>
491
482
*/
492
- public function treeWalk (): Iterator
483
+ private function treeWalk (): Iterator
493
484
{
494
485
if ($ this ->root !== null ) {
495
486
$ stack = [$ this ->root ];
0 commit comments