Skip to content

Commit daa785b

Browse files
authored
Merge pull request #2 from dan-on/deep-nesting-problem-fix
Fix deep nesting problem
2 parents 41c3e3b + c0b3b21 commit daa785b

File tree

2 files changed

+72
-60
lines changed

2 files changed

+72
-60
lines changed

src/IntervalTree.php

+56-56
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function getItems()
8080
*/
8181
public function isEmpty()
8282
{
83-
return ($this->root == null || $this->root == $this->nilNode);
83+
return ($this->root === null || $this->root === $this->nilNode);
8484
}
8585

8686
/**
@@ -196,7 +196,7 @@ public function map($callback)
196196
public function recalcMax($node)
197197
{
198198
$nodeCurrent = $node;
199-
while ($nodeCurrent->parent != null) {
199+
while ($nodeCurrent->parent !== null) {
200200
$nodeCurrent->parent->updateMax();
201201
$nodeCurrent = $nodeCurrent->parent;
202202
}
@@ -207,10 +207,10 @@ public function treeInsert($insertNode)
207207
$currentNode = $this->root;
208208
$parentNode = null;
209209

210-
if ($this->root == null || $this->root == $this->nilNode) {
210+
if ($this->root === null || $this->root === $this->nilNode) {
211211
$this->root = $insertNode;
212212
} else {
213-
while ($currentNode != $this->nilNode) {
213+
while ($currentNode !== $this->nilNode) {
214214
$parentNode = $currentNode;
215215
if ($insertNode->lessThan($currentNode)) {
216216
$currentNode = $currentNode->left;
@@ -235,21 +235,21 @@ public function treeInsert($insertNode)
235235
// Go upwords to the root and re-color until violation will be resolved
236236
public function insertFixup($insertNode)
237237
{
238-
$currentNode;
239-
$uncleNode;
238+
$currentNode = null;
239+
$uncleNode = null;
240240

241241
$currentNode = $insertNode;
242-
while ($currentNode != $this->root && $currentNode->parent->color == Node::COLOR_RED) {
243-
if ($currentNode->parent == $currentNode->parent->parent->left) { // parent is left child of grandfather
242+
while ($currentNode !== $this->root && $currentNode->parent->color === Node::COLOR_RED) {
243+
if ($currentNode->parent === $currentNode->parent->parent->left) { // parent is left child of grandfather
244244
$uncleNode = $currentNode->parent->parent->right; // right brother of parent
245-
if ($uncleNode->color == Node::COLOR_RED) { // Case 1. Uncle is red
245+
if ($uncleNode->color === Node::COLOR_RED) { // Case 1. Uncle is red
246246
// re-color father and uncle into black
247247
$currentNode->parent->color = Node::COLOR_BLACK;
248248
$uncleNode->color = Node::COLOR_BLACK;
249249
$currentNode->parent->parent->color = Node::COLOR_RED;
250250
$currentNode = $currentNode->parent->parent;
251251
} else { // Case 2 & 3. Uncle is black
252-
if ($currentNode == $currentNode->parent->right) { // Case 2. Current if right child
252+
if ($currentNode === $currentNode->parent->right) { // Case 2. Current if right child
253253
// This case is transformed into Case 3.
254254
$currentNode = $currentNode->parent;
255255
$this->rotateLeft($currentNode);
@@ -261,14 +261,14 @@ public function insertFixup($insertNode)
261261
}
262262
} else { // parent is right child of grandfather
263263
$uncleNode = $currentNode->parent->parent->left; // left brother of parent
264-
if ($uncleNode->color == Node::COLOR_RED) { // Case 4. Uncle is red
264+
if ($uncleNode->color === Node::COLOR_RED) { // Case 4. Uncle is red
265265
// re-color father and uncle into black
266266
$currentNode->parent->color = Node::COLOR_BLACK;
267267
$uncleNode->color = Node::COLOR_BLACK;
268268
$currentNode->parent->parent->color = Node::COLOR_RED;
269269
$currentNode = $currentNode->parent->parent;
270270
} else {
271-
if ($currentNode == $currentNode->parent->left) { // Case 5. Current is left child
271+
if ($currentNode === $currentNode->parent->left) { // Case 5. Current is left child
272272
// Transform into case 6
273273
$currentNode = $currentNode->parent;
274274
$this->rotateRight($currentNode);
@@ -289,28 +289,28 @@ public function treeDelete($deleteNode)
289289
$cutNode; // node to be cut - either delete_node or successor_node ("y" from 14.4)
290290
$fixNode; // node to fix rb tree property ("x" from 14.4)
291291

292-
if ($deleteNode->left == $this->nilNode || $deleteNode->right == $this->nilNode) { // delete_node has less then 2 children
292+
if ($deleteNode->left === $this->nilNode || $deleteNode->right === $this->nilNode) { // delete_node has less then 2 children
293293
$cutNode = $deleteNode;
294294
} else { // delete_node has 2 children
295295
$cutNode = $this->treeSuccessor($deleteNode);
296296
}
297297

298298
// fix_node if single child of cut_node
299-
if ($cutNode->left != $this->nilNode) {
299+
if ($cutNode->left !== $this->nilNode) {
300300
$fixNode = $cutNode->left;
301301
} else {
302302
$fixNode = $cutNode->right;
303303
}
304304

305305
// remove cut_node from parent
306-
/*if (fix_node != this.nil_node) {*/
306+
/*if (fix_node !== this.nil_node) {*/
307307
$fixNode->parent = $cutNode->parent;
308308
/*}*/
309309

310-
if ($cutNode == $this->root) {
310+
if ($cutNode === $this->root) {
311311
$this->root = $fixNode;
312312
} else {
313-
if ($cutNode == $cutNode->parent->left) {
313+
if ($cutNode === $cutNode->parent->left) {
314314
$cutNode->parent->left = $fixNode;
315315
} else {
316316
$cutNode->parent->right = $fixNode;
@@ -323,13 +323,13 @@ public function treeDelete($deleteNode)
323323
// COPY DATA !!!
324324
// Delete_node becomes cut_node, it means that we cannot hold reference
325325
// to node in outer structure and we will have to delete by key, additional search need
326-
if ($cutNode != $deleteNode) {
326+
if ($cutNode !== $deleteNode) {
327327
$deleteNode->copyData($cutNode);
328328
$deleteNode->updateMax(); // update max property of the cut node at the new place
329329
$this->recalcMax($deleteNode); // update max property upward from delete_node to root
330330
}
331331

332-
if ( /*fix_node != this.nil_node && */$cutNode->color == Node::COLOR_BLACK) {
332+
if ( /*fix_node !== this.nil_node && */$cutNode->color === Node::COLOR_BLACK) {
333333
$this->deleteFixup($fixNode);
334334
}
335335
}
@@ -339,22 +339,22 @@ public function deleteFixup($fixNode)
339339
$currentNode = $fixNode;
340340
$brotherNode;
341341

342-
while ($currentNode != $this->root && $currentNode->parent != null && $currentNode->color == Node::COLOR_BLACK) {
343-
if ($currentNode == $currentNode->parent->left) { // fix node is left child
342+
while ($currentNode !== $this->root && $currentNode->parent !== null && $currentNode->color === Node::COLOR_BLACK) {
343+
if ($currentNode === $currentNode->parent->left) { // fix node is left child
344344
$brotherNode = $currentNode->parent->right;
345-
if ($brotherNode->color == Node::COLOR_RED) { // Case 1. Brother is red
345+
if ($brotherNode->color === Node::COLOR_RED) { // Case 1. Brother is red
346346
$brotherNode->color = Node::COLOR_BLACK; // re-color brother
347347
$currentNode->parent->color = Node::COLOR_RED; // re-color father
348348
$this->rotateLeft($currentNode->parent);
349349
$brotherNode = $currentNode->parent->right; // update brother
350350
}
351351
// Derive to cases 2..4: brother is black
352-
if ($brotherNode->left->color == Node::COLOR_BLACK &&
353-
$brotherNode->right->color == Node::COLOR_BLACK) { // case 2: both nephews black
352+
if ($brotherNode->left->color === Node::COLOR_BLACK &&
353+
$brotherNode->right->color === Node::COLOR_BLACK) { // case 2: both nephews black
354354
$brotherNode->color = Node::COLOR_RED; // re-color brother
355355
$currentNode = $currentNode->parent; // continue iteration
356356
} else {
357-
if ($brotherNode->right->color == Node::COLOR_BLACK) { // case 3: left nephew red, right nephew black
357+
if ($brotherNode->right->color === Node::COLOR_BLACK) { // case 3: left nephew red, right nephew black
358358
$brotherNode->color = Node::COLOR_RED; // re-color brother
359359
$brotherNode->left->color = Node::COLOR_BLACK; // re-color nephew
360360
$this->rotateRight($brotherNode);
@@ -370,19 +370,19 @@ public function deleteFixup($fixNode)
370370
}
371371
} else { // fix node is right child
372372
$brotherNode = $currentNode->parent->left;
373-
if ($brotherNode->color == Node::COLOR_RED) { // Case 1. Brother is red
373+
if ($brotherNode->color === Node::COLOR_RED) { // Case 1. Brother is red
374374
$brotherNode->color = Node::COLOR_BLACK; // re-color brother
375375
$currentNode->parent->color = Node::COLOR_RED; // re-color father
376376
$this->rotateRight($currentNode->parent);
377377
$brotherNode = $currentNode->parent->left; // update brother
378378
}
379379
// Go to cases 2..4
380-
if ($brotherNode->left->color == Node::COLOR_BLACK &&
381-
$brotherNode->right->color == Node::COLOR_BLACK) { // case 2
380+
if ($brotherNode->left->color === Node::COLOR_BLACK &&
381+
$brotherNode->right->color === Node::COLOR_BLACK) { // case 2
382382
$brotherNode->color = Node::COLOR_RED; // re-color brother
383383
$currentNode = $currentNode->parent; // continue iteration
384384
} else {
385-
if ($brotherNode->left->color == Node::COLOR_BLACK) { // case 3: right nephew red, left nephew black
385+
if ($brotherNode->left->color === Node::COLOR_BLACK) { // case 3: right nephew red, left nephew black
386386
$brotherNode->color = Node::COLOR_RED; // re-color brother
387387
$brotherNode->right->color = Node::COLOR_BLACK; // re-color nephew
388388
$this->rotateLeft($brotherNode);
@@ -404,7 +404,7 @@ public function deleteFixup($fixNode)
404404

405405
public function treeSearch($node, $searchNode)
406406
{
407-
if ($node == null || $node == $this->nilNode) {
407+
if ($node === null || $node === $this->nilNode) {
408408
return null;
409409
}
410410

@@ -422,18 +422,18 @@ public function treeSearch($node, $searchNode)
422422
// Search all intervals intersecting given one
423423
public function treeSearchInterval($node, $searchNode, &$res = [])
424424
{
425-
if ($node != null && $node != $this->nilNode) {
426-
// if (node->left != this.nil_node && node->left->max >= low) {
427-
if ($node->left != $this->nilNode && !$node->notIntersectLeftSubtree($searchNode)) {
425+
if ($node !== null && $node !== $this->nilNode) {
426+
// if (node->left !== this.nil_node && node->left->max >= low) {
427+
if ($node->left !== $this->nilNode && !$node->notIntersectLeftSubtree($searchNode)) {
428428
yield from $this->treeSearchInterval($node->left, $searchNode, $res);
429429
}
430430
// if (low <= node->high && node->low <= high) {
431431
if ($node->intersect($searchNode)) {
432432
$res[] = $node;
433433
yield $node;
434434
}
435-
// if (node->right != this.nil_node && node->low <= high) {
436-
if ($node->right != $this->nilNode && !$node->notIntersectRightSubtree($searchNode)) {
435+
// if (node->right !== this.nil_node && node->low <= high) {
436+
if ($node->right !== $this->nilNode && !$node->notIntersectRightSubtree($searchNode)) {
437437
yield from $this->treeSearchInterval($node->right, $searchNode, $res);
438438
}
439439
}
@@ -442,7 +442,7 @@ public function treeSearchInterval($node, $searchNode, &$res = [])
442442
public function localMinimum($node)
443443
{
444444
$nodeMin = $node;
445-
while ($nodeMin->left != null && $nodeMin->left != $this->nilNode) {
445+
while ($nodeMin->left !== null && $nodeMin->left !== $this->nilNode) {
446446
$nodeMin = $nodeMin->left;
447447
}
448448
return $nodeMin;
@@ -452,7 +452,7 @@ public function localMinimum($node)
452452
public function localMaximum($node)
453453
{
454454
$nodeMax = $node;
455-
while ($nodeMax->right != null && $nodeMax->right != $this->nilNode) {
455+
while ($nodeMax->right !== null && $nodeMax->right !== $this->nilNode) {
456456
$nodeMax = $nodeMax->right;
457457
}
458458
return $nodeMax;
@@ -464,12 +464,12 @@ public function treeSuccessor($node)
464464
$currentNode;
465465
$parentNode;
466466

467-
if ($node->right != $this->nilNode) {
467+
if ($node->right !== $this->nilNode) {
468468
$nodeSuccessor = $this->localMinimum($node->right);
469469
} else {
470470
$currentNode = $node;
471471
$parentNode = $node->parent;
472-
while ($parentNode != null && $parentNode->right == $currentNode) {
472+
while ($parentNode !== null && $parentNode->right === $currentNode) {
473473
$currentNode = $parentNode;
474474
$parentNode = $parentNode->parent;
475475
}
@@ -491,15 +491,15 @@ public function rotateLeft($x)
491491

492492
$x->right = $y->left; // b goes to x.right
493493

494-
if ($y->left != $this->nilNode) {
494+
if ($y->left !== $this->nilNode) {
495495
$y->left->parent = $x; // x becomes parent of b
496496
}
497497
$y->parent = $x->parent; // move parent
498498

499-
if ($x == $this->root) {
499+
if ($x === $this->root) {
500500
$this->root = $y; // y becomes root
501501
} else { // y becomes child of x.parent
502-
if ($x == $x->parent->left) {
502+
if ($x === $x->parent->left) {
503503
$x->parent->left = $y;
504504
} else {
505505
$x->parent->right = $y;
@@ -508,12 +508,12 @@ public function rotateLeft($x)
508508
$y->left = $x; // x becomes left child of y
509509
$x->parent = $y; // and y becomes parent of x
510510

511-
if ($x != null && $x != $this->nilNode) {
511+
if ($x !== null && $x !== $this->nilNode) {
512512
$x->updateMax();
513513
}
514514

515515
$y = $x->parent;
516-
if ($y != null && $y != $this->nilNode) {
516+
if ($y !== null && $y !== $this->nilNode) {
517517
$y->updateMax();
518518
}
519519
}
@@ -524,15 +524,15 @@ public function rotateRight($y)
524524

525525
$y->left = $x->right; // b goes to y.left
526526

527-
if ($x->right != $this->nilNode) {
527+
if ($x->right !== $this->nilNode) {
528528
$x->right->parent = $y; // y becomes parent of b
529529
}
530530
$x->parent = $y->parent; // move parent
531531

532-
if ($y == $this->root) { // x becomes root
532+
if ($y === $this->root) { // x becomes root
533533
$this->root = $x;
534534
} else { // y becomes child of x.parent
535-
if ($y == $y->parent->left) {
535+
if ($y === $y->parent->left) {
536536
$y->parent->left = $x;
537537
} else {
538538
$y->parent->right = $x;
@@ -541,19 +541,19 @@ public function rotateRight($y)
541541
$x->right = $y; // y becomes right child of x
542542
$y->parent = $x; // and x becomes parent of y
543543

544-
if ($y != null && $y != $this->nilNode) {
544+
if ($y !== null && $y !== $this->nilNode) {
545545
$y->updateMax();
546546
}
547547

548548
$x = $y->parent;
549-
if ($x != null && $x != $this->nilNode) {
549+
if ($x !== null && $x !== $this->nilNode) {
550550
$y->updateMax();
551551
}
552552
}
553553

554554
public function treeWalk($node, $action)
555555
{
556-
if ($node != null && $node != $this->nilNode) {
556+
if ($node !== null && $node !== $this->nilNode) {
557557
$this->treeWalk($node->left, $action);
558558
// arr.push(node.toArray());
559559
$action($node);
@@ -566,8 +566,8 @@ public function testRedBlackProperty()
566566
{
567567
$res = true;
568568
$this->treeWalk($this->root, function ($node) use (&$res) {
569-
if ($node->color == Node::COLOR_RED) {
570-
if (!($node->left->color == Node::COLOR_BLACK && $node->right->color == Node::COLOR_BLACK)) {
569+
if ($node->color === Node::COLOR_RED) {
570+
if (!($node->left->color === Node::COLOR_BLACK && $node->right->color === Node::COLOR_BLACK)) {
571571
$res = false;
572572
}
573573
}
@@ -581,20 +581,20 @@ public function testBlackHeightProperty($node)
581581
$height = 0;
582582
$heightLeft = 0;
583583
$heightRight = 0;
584-
if ($node->color == Node::COLOR_BLACK) {
584+
if ($node->color === Node::COLOR_BLACK) {
585585
$height++;
586586
}
587-
if ($node->left != $this->nilNode) {
587+
if ($node->left !== $this->nilNode) {
588588
$heightLeft = $this->testBlackHeightProperty($node->left);
589589
} else {
590590
$heightLeft = 1;
591591
}
592-
if ($node->right != $this->nilNode) {
592+
if ($node->right !== $this->nilNode) {
593593
$heightRight = $this->testBlackHeightProperty($node->right);
594594
} else {
595595
$heightRight = 1;
596596
}
597-
if ($heightLeft != $heightRight) {
597+
if ($heightLeft !== $heightRight) {
598598
throw new Error('Red-black height property violated');
599599
}
600600
$height += $heightLeft;

0 commit comments

Comments
 (0)