• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

chdemko / php-sorted-collections / 42807395482

10 Nov 2026 11:25AM UTC coverage: 97.744% (-2.3%) from 100.0%
42807395482

push

github

chdemko
Merge branch 'release/1.0.13'

170 of 188 new or added lines in 8 files covered. (90.43%)

780 of 798 relevant lines covered (97.74%)

120.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.17
/src/SortedCollection/TreeNode.php
1
<?php
2

3
/**
4
 * chdemko\SortedCollection\TreeNode class
5
 *
6
 * @author    Christophe Demko <chdemko@gmail.com>
7
 * @copyright Copyright (C) 2012-2026 Christophe Demko. All rights reserved.
8
 *
9
 * @license BSD 3-Clause License
10
 *
11
 * This file is part of the php-sorted-collections package https://github.com/chdemko/php-sorted-collections
12
 */
13

14
// Declare chdemko\SortedCollection namespace
15
namespace chdemko\SortedCollection;
16

17
/**
18
 * TreeNode
19
 *
20
 * @package SortedCollection
21
 *
22
 * @since 1.0.0
23
 *
24
 * @property-read TreeNode       $first        The first node of the tree
25
 * @property-read TreeNode       $last         The last node of the tree
26
 * @property-read TreeNode|null  $predecessor  The predecessor node
27
 * @property-read TreeNode|null  $successor    The successor node
28
 * @property-read mixed          $key          The key
29
 * @property-read integer        $count        The number of elements in the tree
30
 */
31
class TreeNode implements \Countable
32
{
33
    /**
34
     * @var integer  Information associated to that node.
35
     *                   Bits of order 0 and 1 are reserved for the existence of left and right tree.
36
     *                   Other bits are for the balance
37
     *
38
     * @since 1.0.0
39
     */
40
    private $information = 0;
41

42
    /**
43
     * @var TreeNode|null  Left|Predecessor node
44
     *
45
     * @since 1.0.0
46
     */
47
    private $left;
48

49
    /**
50
     * @var TreeNode|null  Right|Successor node
51
     *
52
     * @since 1.0.0
53
     */
54
    private $right;
55

56
    /**
57
     * @var mixed  Node key
58
     *
59
     * @since 1.0.0
60
     */
61
    private $keyInternal;
62

63
    /**
64
     * @var mixed  Node value
65
     *
66
     * @since 1.0.0
67
     */
68
    public $value;
69

70
    /**
71
     * Create a node
72
     *
73
     * @param mixed $key   The node key
74
     * @param mixed $value The node value
75
     *
76
     * @return TreeNode A new node
77
     *
78
     * @since 1.0.0
79
     */
80
    public static function create($key, $value)
81
    {
82
        return new static($key, $value);
773 ✔
83
    }
84

85
    /**
86
     * Constructor
87
     *
88
     * @param mixed    $key         The node key
89
     * @param mixed    $value       The node value
90
     * @param TreeNode $predecessor The left node
91
     * @param TreeNode $successor   The right node
92
     *
93
     * @since 1.0.0
94
     */
95
    protected function __construct($key, $value, $predecessor = null, $successor = null)
96
    {
97
        $this->keyInternal = $key;
773 ✔
98
        $this->value = $value;
773 ✔
99
        $this->left = $predecessor;
773 ✔
100
        $this->right = $successor;
773 ✔
101
    }
102

103
    /**
104
     * Magic get method
105
     *
106
     * @param string $property The node property
107
     *
108
     * @return mixed The value associated to the property
109
     *
110
     * @throws RuntimeException If the property is undefined
111
     *
112
     * @since 1.0.0
113
     */
114
    public function __get($property)
115
    {
116
        switch ($property) {
117
            case 'first':
615 ✔
118
                return $this->first();
397 ✔
119
            case 'last':
615 ✔
120
                return $this->last();
368 ✔
121
            case 'predecessor':
615 ✔
122
                return $this->predecessor();
404 ✔
123
            case 'successor':
605 ✔
124
                return $this->successor();
446 ✔
125
            case 'key':
594 ✔
126
                return $this->keyInternal;
588 ✔
127
            case 'count':
8 ✔
128
                return $this->count();
7 ✔
129
            default:
130
                throw new \RuntimeException('Undefined property');
1 ✔
131
        }
132
    }
133

134
    /**
135
     * Get the first node
136
     *
137
     * @return TreeNode the first node
138
     *
139
     * @since 1.0.0
140
     */
141
    public function first()
142
    {
143
        $node = $this;
397 ✔
144

145
        while ($node->information & 2) {
397 ✔
146
            $node = $node->left;
392 ✔
147
        }
148

149
        return $node;
397 ✔
150
    }
151

152
    /**
153
     * Get the last node
154
     *
155
     * @return TreeNode the last node
156
     *
157
     * @since 1.0.0
158
     */
159
    public function last()
160
    {
161
        $node = $this;
368 ✔
162

163
        while ($node->information & 1) {
368 ✔
164
            $node = $node->right;
363 ✔
165
        }
166

167
        return $node;
368 ✔
168
    }
169

170
    /**
171
     * Get the predecessor
172
     *
173
     * @return TreeNode|null the predecessor node
174
     *
175
     * @since 1.0.0
176
     */
177
    public function predecessor()
178
    {
179
        if ($this->information & 2) {
404 ✔
180
            $node = $this->left;
374 ✔
181

182
            while ($node->information & 1) {
374 ✔
183
                $node = $node->right;
314 ✔
184
            }
185

186
            return $node;
374 ✔
187
        } else {
188
            return $this->left;
387 ✔
189
        }
190
    }
191

192
    /**
193
     * Get the successor
194
     *
195
     * @return TreeNode|null the successor node
196
     *
197
     * @since 1.0.0
198
     */
199
    public function successor()
200
    {
201
        if ($this->information & 1) {
446 ✔
202
            $node = $this->right;
410 ✔
203

204
            while ($node->information & 2) {
410 ✔
205
                $node = $node->left;
340 ✔
206
            }
207

208
            return $node;
410 ✔
209
        } else {
210
            return $this->right;
433 ✔
211
        }
212
    }
213

214
    /**
215
     * Count the number of key/value pair
216
     *
217
     * @return integer
218
     *
219
     * @since 1.0.0
220
     */
221
    public function count(): int
222
    {
223
        $count = 1;
7 ✔
224

225
        if ($this->information & 2) {
7 ✔
226
            $count += $this->left->count;
7 ✔
227
        }
228

229
        if ($this->information & 1) {
7 ✔
230
            $count += $this->right->count;
7 ✔
231
        }
232

233
        return $count;
7 ✔
234
    }
235

236
    /**
237
     * Get the node for a key
238
     *
239
     * @param mixed    $key        The key
240
     * @param callable $comparator The comparator function
241
     * @param integer  $type       The operation type
242
     *                             -2 for the
243
     *                             greatest key
244
     *                             lesser than the
245
     *                             given key -1 for
246
     *                             the greatest key
247
     *                             lesser than or
248
     *                             equal to the given
249
     *                             key 0 for the
250
     *                             given key +1 for
251
     *                             the lowest key
252
     *                             greater than or
253
     *                             equal to the given
254
     *                             key +2 for the
255
     *                             lowest key greater
256
     *                             than the given key
257
     *
258
     * @return mixed The node or null if not found
259
     *
260
     * @since 1.0.0
261
     */
262
    public function find($key, $comparator, $type = 0)
263
    {
264
        $node = $this;
258 ✔
265
        $cmp = 0;
258 ✔
266

267
        while (true) {
258 ✔
268
            $cmp = call_user_func($comparator, $key, $node->keyInternal);
258 ✔
269

270
            if ($cmp < 0 && $node->information & 2) {
258 ✔
271
                $node = $node->left;
119 ✔
272
            } elseif ($cmp > 0 && $node->information & 1) {
258 ✔
273
                $node = $node->right;
155 ✔
274
            } else {
275
                break;
258 ✔
276
            }
277
        }
278

279
        if ($cmp < 0) {
258 ✔
280
            if ($type < 0) {
30 ✔
281
                return $node->left;
13 ✔
282
            } elseif ($type > 0) {
18 ✔
283
                return $node;
13 ✔
284
            } else {
285
                return null;
5 ✔
286
            }
287
        } elseif ($cmp > 0) {
229 ✔
288
            if ($type < 0) {
40 ✔
289
                return $node;
12 ✔
290
            } elseif ($type > 0) {
28 ✔
291
                return $node->right;
15 ✔
292
            } else {
293
                return null;
13 ✔
294
            }
295
        } else {
296
            if ($type < -1) {
193 ✔
297
                return $node->predecessor;
41 ✔
298
            } elseif ($type > 1) {
159 ✔
299
                return $node->successor;
46 ✔
300
            } else {
301
                return $node;
120 ✔
302
            }
303
        }
304
    }
305

306
    /**
307
     * Rotate the node to the left
308
     *
309
     * @return TreeNode The rotated node
310
     *
311
     * @since 1.0.0
312
     */
313
    private function rotateLeft()
314
    {
315
        $right = $this->right;
633 ✔
316

317
        if ($right->information & 2) {
633 ✔
318
            $this->right = $right->left;
471 ✔
319
            $right->left = $this;
471 ✔
320
        } else {
321
            $right->information |= 2;
633 ✔
322
            $this->information &= ~ 1;
633 ✔
323
        }
324

325
        $this->information -= 4;
633 ✔
326

327
        if ($right->information >= 4) {
633 ✔
328
            $this->information -= $right->information & ~3;
631 ✔
329
        }
330

331
        $right->information -= 4;
633 ✔
332

333
        if ($this->information < 0) {
633 ✔
334
            $right->information += $this->information & ~3;
8 ✔
335
        }
336

337
        return $right;
633 ✔
338
    }
339

340
    /**
341
     * Rotate the node to the right
342
     *
343
     * @return TreeNode The rotated node
344
     *
345
     * @since 1.0.0
346
     */
347
    private function rotateRight()
348
    {
349
        $left = $this->left;
194 ✔
350

351
        if ($left->information & 1) {
194 ✔
352
            $this->left = $left->right;
60 ✔
353
            $left->right = $this;
60 ✔
354
        } else {
355
            $this->information &= ~ 2;
185 ✔
356
            $left->information |= 1;
185 ✔
357
        }
358

359
        $this->information += 4;
194 ✔
360

361
        if ($left->information < 0) {
194 ✔
362
            $this->information -= $left->information & ~3;
154 ✔
363
        }
364

365
        $left->information += 4;
194 ✔
366

367
        if ($this->information >= 4) {
194 ✔
368
            $left->information += $this->information & ~3;
8 ✔
369
        }
370

371
        return $left;
194 ✔
372
    }
373

374
    /**
375
     * Increment the balance of the node
376
     *
377
     * @return TreeNode $this or a rotated version of $this
378
     *
379
     * @since 1.0.0
380
     */
381
    private function incBalance()
382
    {
383
        $this->information += 4;
703 ✔
384

385
        if ($this->information >= 8) {
703 ✔
386
            $right = $this->right;
631 ✔
387

388
            if (!$right instanceof self) {
631 ✔
NEW
389
                return $this;
×
390
            }
391

392
            if ($right->information < 0) {
631 ✔
393
                $this->right = $right->rotateRight();
55 ✔
394
            }
395

396
            return $this->rotateLeft();
631 ✔
397
        }
398

399
        return $this;
703 ✔
400
    }
401

402
    /**
403
     * Decrement the balance of the node
404
     *
405
     * @return TreeNode $this or a rotated version of $this
406
     *
407
     * @since 1.0.0
408
     */
409
    private function decBalance()
410
    {
411
        $this->information -= 4;
393 ✔
412

413
        if ($this->information < - 4) {
393 ✔
414
            $left = $this->left;
166 ✔
415

416
            if (!$left instanceof self) {
166 ✔
NEW
417
                return $this;
×
418
            }
419

420
            if ($left->information >= 4) {
166 ✔
421
                $this->left = $left->rotateLeft();
55 ✔
422
            }
423

424
            return $this->rotateRight();
166 ✔
425
        }
426

427
        return $this;
393 ✔
428
    }
429

430
    /**
431
     * Insert a key/value pair
432
     *
433
     * @param mixed    $key        The key
434
     * @param mixed    $value      The value
435
     * @param callable $comparator The comparator function
436
     *
437
     * @return TreeNode The new root
438
     *
439
     * @since 1.0.0
440
     */
441
    public function insert($key, $value, $comparator)
442
    {
443
        $node = $this;
754 ✔
444
        $cmp = call_user_func($comparator, $key, $this->keyInternal);
754 ✔
445

446
        if ($cmp < 0) {
754 ✔
447
            if ($this->information & 2) {
387 ✔
448
                $left = $this->left;
317 ✔
449

450
                if (!$left instanceof self) {
317 ✔
NEW
451
                    return $node;
×
452
                }
453

454
                $leftBalance = $left->information & ~3;
317 ✔
455
                $this->left = $left->insert($key, $value, $comparator);
317 ✔
456

457
                if (
458
                    $this->left instanceof self
317 ✔
459
                    && ($this->left->information & ~3)
317 ✔
460
                    && ($this->left->information & ~3) != $leftBalance
317 ✔
461
                ) {
462
                    $node = $this->decBalance();
316 ✔
463
                }
464
            } else {
465
                $this->left = new static($key, $value, $this->left, $this);
338 ✔
466
                $this->information |= 2;
338 ✔
467
                $node = $this->decBalance();
338 ✔
468
            }
469
        } elseif ($cmp > 0) {
704 ✔
470
            if ($this->information & 1) {
701 ✔
471
                $right = $this->right;
631 ✔
472

473
                if (!$right instanceof self) {
631 ✔
NEW
474
                    return $node;
×
475
                }
476

477
                $rightBalance = $right->information & ~3;
631 ✔
478
                $this->right = $right->insert($key, $value, $comparator);
631 ✔
479

480
                if (
481
                    $this->right instanceof self
631 ✔
482
                    && ($this->right->information & ~3)
631 ✔
483
                    && ($this->right->information & ~3) != $rightBalance
631 ✔
484
                ) {
485
                    $node = $this->incBalance();
631 ✔
486
                }
487
            } else {
488
                $this->right = new static($key, $value, $this, $this->right);
701 ✔
489
                $this->information |= 1;
701 ✔
490
                $node = $this->incBalance();
701 ✔
491
            }
492
        } else {
493
            $this->value = $value;
4 ✔
494
        }
495

496
        return $node;
754 ✔
497
    }
498

499
    /**
500
     * Pull up the left most node of a node
501
     *
502
     * @return TreeNode The new root
503
     *
504
     * @since 1.0.0
505
     */
506
    private function pullUpLeftMost()
507
    {
508
        if ($this->information & 2) {
177 ✔
509
            $left = $this->left;
143 ✔
510

511
            if (!$left instanceof self) {
143 ✔
NEW
512
                return $this;
×
513
            }
514

515
            $leftBalance = $left->information & ~3;
143 ✔
516
            $this->left = $left->pullUpLeftMost();
143 ✔
517

518
            if (
519
                !($this->information & 2)
143 ✔
520
                || $leftBalance != 0 && ($this->left instanceof self) && ($this->left->information & ~3) == 0
143 ✔
521
            ) {
522
                return $this->incBalance();
143 ✔
523
            } else {
524
                return $this;
7 ✔
525
            }
526
        } else {
527
            $this->left->keyInternal = $this->keyInternal;
177 ✔
528
            $this->left->value = $this->value;
177 ✔
529

530
            if ($this->information & 1) {
177 ✔
531
                $this->right->left = $this->left;
57 ✔
532

533
                return $this->right;
57 ✔
534
            } else {
535
                if ($this->left->right == $this) {
177 ✔
536
                    $this->left->information &= ~ 1;
169 ✔
537

538
                    return $this->right;
169 ✔
539
                } else {
540
                    $this->right->information &= ~ 2;
132 ✔
541

542
                    return $this->left;
132 ✔
543
                }
544
            }
545
        }
546
    }
547

548
    /**
549
     * Remove a key
550
     *
551
     * @param mixed    $key        The key
552
     * @param callable $comparator The comparator function
553
     *
554
     * @return TreeNode The new root
555
     *
556
     * @since 1.0.0
557
     */
558
    public function remove($key, $comparator)
559
    {
560
        $cmp = call_user_func($comparator, $key, $this->keyInternal);
180 ✔
561

562
        if ($cmp < 0) {
180 ✔
563
            if ($this->information & 2) {
177 ✔
564
                $left = $this->left;
177 ✔
565

566
                if (!$left instanceof self) {
177 ✔
NEW
567
                    return $this;
×
568
                }
569

570
                $leftBalance = $left->information & ~3;
177 ✔
571
                $this->left = $left->remove($key, $comparator);
177 ✔
572

573
                if (
574
                    !($this->information & 2)
177 ✔
575
                    || $leftBalance != 0 && ($this->left instanceof self) && ($this->left->information & ~3) == 0
177 ✔
576
                ) {
577
                    return $this->incBalance();
175 ✔
578
                }
579
            }
580
        } elseif ($cmp > 0) {
180 ✔
581
            if ($this->information & 1) {
176 ✔
582
                $right = $this->right;
176 ✔
583

584
                if (!$right instanceof self) {
176 ✔
NEW
585
                    return $this;
×
586
                }
587

588
                $rightBalance = $right->information & ~3;
176 ✔
589
                $this->right = $right->remove($key, $comparator);
176 ✔
590

591
                if (
592
                    !($this->information & 1)
176 ✔
593
                    || $rightBalance != 0 && ($this->right instanceof self) && ($this->right->information & ~3) == 0
176 ✔
594
                ) {
595
                    return $this->decBalance();
174 ✔
596
                }
597
            }
598
        } else {
599
            if ($this->information & 1) {
180 ✔
600
                $right = $this->right;
177 ✔
601

602
                if (!$right instanceof self) {
177 ✔
NEW
603
                    return $this;
×
604
                }
605

606
                $rightBalance = $right->information & ~3;
177 ✔
607
                $this->right = $right->pullUpLeftMost();
177 ✔
608

609
                if (
610
                    !($this->information & 1)
177 ✔
611
                    || $rightBalance != 0 && ($this->right instanceof self) && ($this->right->information & ~3) == 0
177 ✔
612
                ) {
613
                    return $this->decBalance();
176 ✔
614
                }
615
            } else {
616
                $left = $this->left;
177 ✔
617
                $right = $this->right;
177 ✔
618

619
                if ($this->information & 2) {
177 ✔
620
                    $left->right = $right;
79 ✔
621

622
                    return $left;
79 ✔
623
                } else {
624
                    if ($left && $left->right == $this) {
177 ✔
625
                        $left->information &= ~ 1;
167 ✔
626

627
                        return $right;
167 ✔
628
                    } elseif ($right && $right->left == $this) {
170 ✔
629
                        $right->information &= ~ 2;
168 ✔
630

631
                        return $left;
168 ✔
632
                    } else {
633
                        return null;
2 ✔
634
                    }
635
                }
636
            }
637
        }
638

639
        return $this;
111 ✔
640
    }
641
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc