@@ -181,10 +181,6 @@ private function treeInsert(Node $insertNode): void
181181 }
182182
183183 /**
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- *
188184 * @param Node<TPoint, TValue> $insertNode
189185 */
190186 private function insertFixup (Node $ insertNode ): void
@@ -241,7 +237,6 @@ private function treeDelete(Node $deleteNode): void
241237 $ cutNode = $ this ->treeSuccessor ($ deleteNode );
242238 }
243239
244- // fix_node if single child of cut_node
245240 if ($ cutNode ->getLeft () !== $ this ->nilNode ) {
246241 $ fixNode = $ cutNode ->getLeft ();
247242 } else {
@@ -258,17 +253,15 @@ private function treeDelete(Node $deleteNode): void
258253 } else {
259254 $ cutNode ->getParent ()->setRight ($ fixNode );
260255 }
261- $ cutNode ->getParent ()->updateMax (); // update max property of the parent
256+ $ cutNode ->getParent ()->updateMax ();
262257 }
263258
264- $ this ->recalculateMax ($ fixNode ); // update max property upward from fix_node to root
259+ $ this ->recalculateMax ($ fixNode );
265260
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
268261 if ($ cutNode !== $ deleteNode ) {
269262 $ 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 );
272265 }
273266
274267 if ($ cutNode ->getColor ()->isBlack ()) {
@@ -305,7 +298,6 @@ private function deleteFixup(Node $fixNode): void
305298 $ brotherNode ->setColor (NodeColor::red ());
306299 $ brotherNode ->getLeft ()->setColor (NodeColor::black ());
307300 $ this ->rotateRight ($ brotherNode );
308- $ brotherNode = $ currentNode ->getParent ()->getRight ();
309301 }
310302 $ brotherNode ->setColor ($ currentNode ->getParent ()->getColor ());
311303 $ currentNode ->getParent ()->setColor (NodeColor::black ());
@@ -329,7 +321,6 @@ private function deleteFixup(Node $fixNode): void
329321 $ brotherNode ->setColor (NodeColor::red ());
330322 $ brotherNode ->getRight ()->setColor (NodeColor::black ());
331323 $ this ->rotateLeft ($ brotherNode );
332- $ brotherNode = $ currentNode ->getParent ()->getLeft ();
333324 }
334325 $ brotherNode ->setColor ($ currentNode ->getParent ()->getColor ());
335326 $ currentNode ->getParent ()->setColor (NodeColor::black ());
@@ -344,25 +335,25 @@ private function deleteFixup(Node $fixNode): void
344335 }
345336
346337 /**
338+ * @param Node<TPoint, TValue> $startingNode
347339 * @param Node<TPoint, TValue> $node
348- * @param Node<TPoint, TValue> $searchNode
349340 * @return Node<TPoint, TValue>|null
350341 */
351- private function treeSearch (Node $ node , Node $ searchNode ): ?Node
342+ private function treeSearch (Node $ startingNode , Node $ node ): ?Node
352343 {
353- if ($ node === $ this ->nilNode ) {
344+ if ($ startingNode === $ this ->nilNode ) {
354345 return null ;
355346 }
356347
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 );
363354 }
364355
365- return $ this -> treeSearch ( $ node -> getRight (), $ searchNode ) ;
356+ return $ searchedNode ;
366357 }
367358
368359 /**
@@ -424,22 +415,22 @@ private function treeSuccessor(Node $node): ?Node
424415 private function rotateLeft (Node $ x ): void
425416 {
426417 $ y = $ x ->getRight ();
427- $ x ->setRight ($ y ->getLeft ()); // b goes to x.right
418+ $ x ->setRight ($ y ->getLeft ());
428419
429420 if ($ y ->getLeft () !== $ this ->nilNode ) {
430- $ y ->getLeft ()->setParent ($ x ); // x becomes parent of b
421+ $ y ->getLeft ()->setParent ($ x );
431422 }
432- $ y ->setParent ($ x ->getParent ()); // move parent
423+ $ y ->setParent ($ x ->getParent ());
433424
434425 if ($ x ->getParent () === null ) {
435- $ this ->root = $ y ; // y becomes root
426+ $ this ->root = $ y ;
436427 } elseif ($ x === $ x ->getParent ()->getLeft ()) {
437428 $ x ->getParent ()->setLeft ($ y );
438429 } else {
439430 $ x ->getParent ()->setRight ($ y );
440431 }
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 );
443434
444435 if ($ x !== $ this ->nilNode ) {
445436 $ x ->updateMax ();
@@ -459,22 +450,22 @@ private function rotateRight(Node $y): void
459450 {
460451 $ x = $ y ->getLeft ();
461452
462- $ y ->setLeft ($ x ->getRight ()); // b goes to y.left
453+ $ y ->setLeft ($ x ->getRight ());
463454
464455 if ($ x ->getRight () !== $ this ->nilNode ) {
465- $ x ->getRight ()->setParent ($ y ); // y becomes parent of b
456+ $ x ->getRight ()->setParent ($ y );
466457 }
467- $ x ->setParent ($ y ->getParent ()); // move parent
458+ $ x ->setParent ($ y ->getParent ());
468459
469- if ($ y ->getParent () === null ) { // x becomes root
460+ if ($ y ->getParent () === null ) {
470461 $ this ->root = $ x ;
471462 } elseif ($ y === $ y ->getParent ()->getLeft ()) {
472463 $ y ->getParent ()->setLeft ($ x );
473464 } else {
474465 $ y ->getParent ()->setRight ($ x );
475466 }
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 );
478469
479470 if ($ y !== $ this ->nilNode ) {
480471 $ y ->updateMax ();
@@ -489,7 +480,7 @@ private function rotateRight(Node $y): void
489480 /**
490481 * @return Iterator<Node<TPoint, TValue>>
491482 */
492- public function treeWalk (): Iterator
483+ private function treeWalk (): Iterator
493484 {
494485 if ($ this ->root !== null ) {
495486 $ stack = [$ this ->root ];
0 commit comments