-
-
Notifications
You must be signed in to change notification settings - Fork 569
Expand file tree
/
Copy pathNode.php
More file actions
68 lines (58 loc) · 1.45 KB
/
Node.php
File metadata and controls
68 lines (58 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace DataStructures;
/**
* Internal Node class representing a single node in the Red-Black Tree
*
* @internal
*/
class Node
{
const RED = 'RED';
const BLACK = 'BLACK';
public $value;
public $color;
public $left;
public $right;
public $parent;
/**
* Node constructor
*
* @param mixed $value
* @param string $color
* @param Node|null $parent
*/
public function __construct($value, $color = self::RED, $parent = null)
{
$this->value = $value;
$this->color = $color;
$this->parent = $parent;
$this->left = null;
$this->right = null;
}
public function isRed(): bool
{
return $this->color === self::RED;
}
public function isBlack(): bool
{
return $this->color === self::BLACK;
}
public function sibling(): ?Node
{
if ($this->parent === null) return null;
return $this->parent->left === $this ? $this->parent->right : $this->parent->left;
}
public function isLeftChild(): bool
{
return $this->parent !== null && $this->parent->left === $this;
}
public function isRightChild(): bool
{
return $this->parent !== null && $this->parent->right === $this;
}
public function hasRedChild(): bool
{
return ($this->left !== null && $this->left->isRed()) ||
($this->right !== null && $this->right->isRed());
}
}