@@ -80,7 +80,7 @@ public function getItems()
80
80
*/
81
81
public function isEmpty ()
82
82
{
83
- return ($ this ->root == null || $ this ->root == $ this ->nilNode );
83
+ return ($ this ->root === null || $ this ->root = == $ this ->nilNode );
84
84
}
85
85
86
86
/**
@@ -196,7 +196,7 @@ public function map($callback)
196
196
public function recalcMax ($ node )
197
197
{
198
198
$ nodeCurrent = $ node ;
199
- while ($ nodeCurrent ->parent != null ) {
199
+ while ($ nodeCurrent ->parent !== null ) {
200
200
$ nodeCurrent ->parent ->updateMax ();
201
201
$ nodeCurrent = $ nodeCurrent ->parent ;
202
202
}
@@ -207,10 +207,10 @@ public function treeInsert($insertNode)
207
207
$ currentNode = $ this ->root ;
208
208
$ parentNode = null ;
209
209
210
- if ($ this ->root == null || $ this ->root == $ this ->nilNode ) {
210
+ if ($ this ->root === null || $ this ->root = == $ this ->nilNode ) {
211
211
$ this ->root = $ insertNode ;
212
212
} else {
213
- while ($ currentNode != $ this ->nilNode ) {
213
+ while ($ currentNode !== $ this ->nilNode ) {
214
214
$ parentNode = $ currentNode ;
215
215
if ($ insertNode ->lessThan ($ currentNode )) {
216
216
$ currentNode = $ currentNode ->left ;
@@ -235,21 +235,21 @@ public function treeInsert($insertNode)
235
235
// Go upwords to the root and re-color until violation will be resolved
236
236
public function insertFixup ($ insertNode )
237
237
{
238
- $ currentNode ;
239
- $ uncleNode ;
238
+ $ currentNode = null ;
239
+ $ uncleNode = null ;
240
240
241
241
$ 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
244
244
$ 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
246
246
// re-color father and uncle into black
247
247
$ currentNode ->parent ->color = Node::COLOR_BLACK ;
248
248
$ uncleNode ->color = Node::COLOR_BLACK ;
249
249
$ currentNode ->parent ->parent ->color = Node::COLOR_RED ;
250
250
$ currentNode = $ currentNode ->parent ->parent ;
251
251
} 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
253
253
// This case is transformed into Case 3.
254
254
$ currentNode = $ currentNode ->parent ;
255
255
$ this ->rotateLeft ($ currentNode );
@@ -261,14 +261,14 @@ public function insertFixup($insertNode)
261
261
}
262
262
} else { // parent is right child of grandfather
263
263
$ 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
265
265
// re-color father and uncle into black
266
266
$ currentNode ->parent ->color = Node::COLOR_BLACK ;
267
267
$ uncleNode ->color = Node::COLOR_BLACK ;
268
268
$ currentNode ->parent ->parent ->color = Node::COLOR_RED ;
269
269
$ currentNode = $ currentNode ->parent ->parent ;
270
270
} 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
272
272
// Transform into case 6
273
273
$ currentNode = $ currentNode ->parent ;
274
274
$ this ->rotateRight ($ currentNode );
@@ -289,28 +289,28 @@ public function treeDelete($deleteNode)
289
289
$ cutNode ; // node to be cut - either delete_node or successor_node ("y" from 14.4)
290
290
$ fixNode ; // node to fix rb tree property ("x" from 14.4)
291
291
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
293
293
$ cutNode = $ deleteNode ;
294
294
} else { // delete_node has 2 children
295
295
$ cutNode = $ this ->treeSuccessor ($ deleteNode );
296
296
}
297
297
298
298
// fix_node if single child of cut_node
299
- if ($ cutNode ->left != $ this ->nilNode ) {
299
+ if ($ cutNode ->left !== $ this ->nilNode ) {
300
300
$ fixNode = $ cutNode ->left ;
301
301
} else {
302
302
$ fixNode = $ cutNode ->right ;
303
303
}
304
304
305
305
// remove cut_node from parent
306
- /*if (fix_node != this.nil_node) {*/
306
+ /*if (fix_node !== this.nil_node) {*/
307
307
$ fixNode ->parent = $ cutNode ->parent ;
308
308
/*}*/
309
309
310
- if ($ cutNode == $ this ->root ) {
310
+ if ($ cutNode === $ this ->root ) {
311
311
$ this ->root = $ fixNode ;
312
312
} else {
313
- if ($ cutNode == $ cutNode ->parent ->left ) {
313
+ if ($ cutNode === $ cutNode ->parent ->left ) {
314
314
$ cutNode ->parent ->left = $ fixNode ;
315
315
} else {
316
316
$ cutNode ->parent ->right = $ fixNode ;
@@ -323,13 +323,13 @@ public function treeDelete($deleteNode)
323
323
// COPY DATA !!!
324
324
// Delete_node becomes cut_node, it means that we cannot hold reference
325
325
// to node in outer structure and we will have to delete by key, additional search need
326
- if ($ cutNode != $ deleteNode ) {
326
+ if ($ cutNode !== $ deleteNode ) {
327
327
$ deleteNode ->copyData ($ cutNode );
328
328
$ deleteNode ->updateMax (); // update max property of the cut node at the new place
329
329
$ this ->recalcMax ($ deleteNode ); // update max property upward from delete_node to root
330
330
}
331
331
332
- if ( /*fix_node != this.nil_node && */ $ cutNode ->color == Node::COLOR_BLACK ) {
332
+ if ( /*fix_node !== this.nil_node && */ $ cutNode ->color = == Node::COLOR_BLACK ) {
333
333
$ this ->deleteFixup ($ fixNode );
334
334
}
335
335
}
@@ -339,22 +339,22 @@ public function deleteFixup($fixNode)
339
339
$ currentNode = $ fixNode ;
340
340
$ brotherNode ;
341
341
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
344
344
$ 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
346
346
$ brotherNode ->color = Node::COLOR_BLACK ; // re-color brother
347
347
$ currentNode ->parent ->color = Node::COLOR_RED ; // re-color father
348
348
$ this ->rotateLeft ($ currentNode ->parent );
349
349
$ brotherNode = $ currentNode ->parent ->right ; // update brother
350
350
}
351
351
// 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
354
354
$ brotherNode ->color = Node::COLOR_RED ; // re-color brother
355
355
$ currentNode = $ currentNode ->parent ; // continue iteration
356
356
} 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
358
358
$ brotherNode ->color = Node::COLOR_RED ; // re-color brother
359
359
$ brotherNode ->left ->color = Node::COLOR_BLACK ; // re-color nephew
360
360
$ this ->rotateRight ($ brotherNode );
@@ -370,19 +370,19 @@ public function deleteFixup($fixNode)
370
370
}
371
371
} else { // fix node is right child
372
372
$ 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
374
374
$ brotherNode ->color = Node::COLOR_BLACK ; // re-color brother
375
375
$ currentNode ->parent ->color = Node::COLOR_RED ; // re-color father
376
376
$ this ->rotateRight ($ currentNode ->parent );
377
377
$ brotherNode = $ currentNode ->parent ->left ; // update brother
378
378
}
379
379
// 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
382
382
$ brotherNode ->color = Node::COLOR_RED ; // re-color brother
383
383
$ currentNode = $ currentNode ->parent ; // continue iteration
384
384
} 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
386
386
$ brotherNode ->color = Node::COLOR_RED ; // re-color brother
387
387
$ brotherNode ->right ->color = Node::COLOR_BLACK ; // re-color nephew
388
388
$ this ->rotateLeft ($ brotherNode );
@@ -404,7 +404,7 @@ public function deleteFixup($fixNode)
404
404
405
405
public function treeSearch ($ node , $ searchNode )
406
406
{
407
- if ($ node == null || $ node == $ this ->nilNode ) {
407
+ if ($ node === null || $ node = == $ this ->nilNode ) {
408
408
return null ;
409
409
}
410
410
@@ -422,18 +422,18 @@ public function treeSearch($node, $searchNode)
422
422
// Search all intervals intersecting given one
423
423
public function treeSearchInterval ($ node , $ searchNode , &$ res = [])
424
424
{
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 )) {
428
428
yield from $ this ->treeSearchInterval ($ node ->left , $ searchNode , $ res );
429
429
}
430
430
// if (low <= node->high && node->low <= high) {
431
431
if ($ node ->intersect ($ searchNode )) {
432
432
$ res [] = $ node ;
433
433
yield $ node ;
434
434
}
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 )) {
437
437
yield from $ this ->treeSearchInterval ($ node ->right , $ searchNode , $ res );
438
438
}
439
439
}
@@ -442,7 +442,7 @@ public function treeSearchInterval($node, $searchNode, &$res = [])
442
442
public function localMinimum ($ node )
443
443
{
444
444
$ nodeMin = $ node ;
445
- while ($ nodeMin ->left != null && $ nodeMin ->left != $ this ->nilNode ) {
445
+ while ($ nodeMin ->left !== null && $ nodeMin ->left != = $ this ->nilNode ) {
446
446
$ nodeMin = $ nodeMin ->left ;
447
447
}
448
448
return $ nodeMin ;
@@ -452,7 +452,7 @@ public function localMinimum($node)
452
452
public function localMaximum ($ node )
453
453
{
454
454
$ nodeMax = $ node ;
455
- while ($ nodeMax ->right != null && $ nodeMax ->right != $ this ->nilNode ) {
455
+ while ($ nodeMax ->right !== null && $ nodeMax ->right != = $ this ->nilNode ) {
456
456
$ nodeMax = $ nodeMax ->right ;
457
457
}
458
458
return $ nodeMax ;
@@ -464,12 +464,12 @@ public function treeSuccessor($node)
464
464
$ currentNode ;
465
465
$ parentNode ;
466
466
467
- if ($ node ->right != $ this ->nilNode ) {
467
+ if ($ node ->right !== $ this ->nilNode ) {
468
468
$ nodeSuccessor = $ this ->localMinimum ($ node ->right );
469
469
} else {
470
470
$ currentNode = $ node ;
471
471
$ parentNode = $ node ->parent ;
472
- while ($ parentNode != null && $ parentNode ->right == $ currentNode ) {
472
+ while ($ parentNode !== null && $ parentNode ->right = == $ currentNode ) {
473
473
$ currentNode = $ parentNode ;
474
474
$ parentNode = $ parentNode ->parent ;
475
475
}
@@ -491,15 +491,15 @@ public function rotateLeft($x)
491
491
492
492
$ x ->right = $ y ->left ; // b goes to x.right
493
493
494
- if ($ y ->left != $ this ->nilNode ) {
494
+ if ($ y ->left !== $ this ->nilNode ) {
495
495
$ y ->left ->parent = $ x ; // x becomes parent of b
496
496
}
497
497
$ y ->parent = $ x ->parent ; // move parent
498
498
499
- if ($ x == $ this ->root ) {
499
+ if ($ x === $ this ->root ) {
500
500
$ this ->root = $ y ; // y becomes root
501
501
} else { // y becomes child of x.parent
502
- if ($ x == $ x ->parent ->left ) {
502
+ if ($ x === $ x ->parent ->left ) {
503
503
$ x ->parent ->left = $ y ;
504
504
} else {
505
505
$ x ->parent ->right = $ y ;
@@ -508,12 +508,12 @@ public function rotateLeft($x)
508
508
$ y ->left = $ x ; // x becomes left child of y
509
509
$ x ->parent = $ y ; // and y becomes parent of x
510
510
511
- if ($ x != null && $ x != $ this ->nilNode ) {
511
+ if ($ x !== null && $ x != = $ this ->nilNode ) {
512
512
$ x ->updateMax ();
513
513
}
514
514
515
515
$ y = $ x ->parent ;
516
- if ($ y != null && $ y != $ this ->nilNode ) {
516
+ if ($ y !== null && $ y != = $ this ->nilNode ) {
517
517
$ y ->updateMax ();
518
518
}
519
519
}
@@ -524,15 +524,15 @@ public function rotateRight($y)
524
524
525
525
$ y ->left = $ x ->right ; // b goes to y.left
526
526
527
- if ($ x ->right != $ this ->nilNode ) {
527
+ if ($ x ->right !== $ this ->nilNode ) {
528
528
$ x ->right ->parent = $ y ; // y becomes parent of b
529
529
}
530
530
$ x ->parent = $ y ->parent ; // move parent
531
531
532
- if ($ y == $ this ->root ) { // x becomes root
532
+ if ($ y === $ this ->root ) { // x becomes root
533
533
$ this ->root = $ x ;
534
534
} else { // y becomes child of x.parent
535
- if ($ y == $ y ->parent ->left ) {
535
+ if ($ y === $ y ->parent ->left ) {
536
536
$ y ->parent ->left = $ x ;
537
537
} else {
538
538
$ y ->parent ->right = $ x ;
@@ -541,19 +541,19 @@ public function rotateRight($y)
541
541
$ x ->right = $ y ; // y becomes right child of x
542
542
$ y ->parent = $ x ; // and x becomes parent of y
543
543
544
- if ($ y != null && $ y != $ this ->nilNode ) {
544
+ if ($ y !== null && $ y != = $ this ->nilNode ) {
545
545
$ y ->updateMax ();
546
546
}
547
547
548
548
$ x = $ y ->parent ;
549
- if ($ x != null && $ x != $ this ->nilNode ) {
549
+ if ($ x !== null && $ x != = $ this ->nilNode ) {
550
550
$ y ->updateMax ();
551
551
}
552
552
}
553
553
554
554
public function treeWalk ($ node , $ action )
555
555
{
556
- if ($ node != null && $ node != $ this ->nilNode ) {
556
+ if ($ node !== null && $ node != = $ this ->nilNode ) {
557
557
$ this ->treeWalk ($ node ->left , $ action );
558
558
// arr.push(node.toArray());
559
559
$ action ($ node );
@@ -566,8 +566,8 @@ public function testRedBlackProperty()
566
566
{
567
567
$ res = true ;
568
568
$ 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 )) {
571
571
$ res = false ;
572
572
}
573
573
}
@@ -581,20 +581,20 @@ public function testBlackHeightProperty($node)
581
581
$ height = 0 ;
582
582
$ heightLeft = 0 ;
583
583
$ heightRight = 0 ;
584
- if ($ node ->color == Node::COLOR_BLACK ) {
584
+ if ($ node ->color === Node::COLOR_BLACK ) {
585
585
$ height ++;
586
586
}
587
- if ($ node ->left != $ this ->nilNode ) {
587
+ if ($ node ->left !== $ this ->nilNode ) {
588
588
$ heightLeft = $ this ->testBlackHeightProperty ($ node ->left );
589
589
} else {
590
590
$ heightLeft = 1 ;
591
591
}
592
- if ($ node ->right != $ this ->nilNode ) {
592
+ if ($ node ->right !== $ this ->nilNode ) {
593
593
$ heightRight = $ this ->testBlackHeightProperty ($ node ->right );
594
594
} else {
595
595
$ heightRight = 1 ;
596
596
}
597
- if ($ heightLeft != $ heightRight ) {
597
+ if ($ heightLeft !== $ heightRight ) {
598
598
throw new Error ('Red-black height property violated ' );
599
599
}
600
600
$ height += $ heightLeft ;
0 commit comments