|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (C) 2019 Sebastian Böttger <[email protected]> |
|
5
|
|
|
* You may use, distribute and modify this code under the |
|
6
|
|
|
* terms of the MIT license. |
|
7
|
|
|
* |
|
8
|
|
|
* You should have received a copy of the MIT license with |
|
9
|
|
|
* this file. If not, please visit: https://opensource.org/licenses/mit-license.php |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Seboettg\Forest\BinaryTree; |
|
13
|
|
|
|
|
14
|
|
|
use Seboettg\Collection\ArrayList\ArrayListInterface; |
|
15
|
|
|
use Seboettg\Forest\General\TreeNode; |
|
16
|
|
|
use Seboettg\Forest\General\TreeNodeInterface; |
|
17
|
|
|
use Seboettg\Forest\Item\ItemInterface; |
|
18
|
|
|
use Seboettg\Forest\Visitor\VisitorInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class BinaryNode |
|
22
|
|
|
* @package Seboettg\Forest\BinaryTree |
|
23
|
|
|
*/ |
|
24
|
|
|
class BinaryNode extends TreeNode implements BinaryNodeInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var BinaryNodeInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $left; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var BinaryNodeInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $right; |
|
35
|
|
|
|
|
36
|
37 |
|
public function __construct(ItemInterface $item) |
|
37
|
|
|
{ |
|
38
|
37 |
|
parent::__construct($item); |
|
39
|
37 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return BinaryNode |
|
43
|
|
|
*/ |
|
44
|
37 |
|
final public function getLeft(): ?BinaryNodeInterface |
|
45
|
|
|
{ |
|
46
|
37 |
|
return $this->left; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param BinaryNodeInterface $left |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
37 |
|
final public function setLeft(?BinaryNodeInterface $left): void |
|
54
|
|
|
{ |
|
55
|
37 |
|
if ($left !== null) { |
|
56
|
37 |
|
$left->setParent($this); |
|
57
|
|
|
} |
|
58
|
37 |
|
$this->left = $left; |
|
59
|
37 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return BinaryNode |
|
63
|
|
|
*/ |
|
64
|
37 |
|
final public function getRight(): ?BinaryNodeInterface |
|
65
|
|
|
{ |
|
66
|
37 |
|
return $this->right; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param BinaryNodeInterface $right |
|
71
|
|
|
* @return void |
|
72
|
|
|
*/ |
|
73
|
37 |
|
final public function setRight(?BinaryNodeInterface $right): void |
|
74
|
|
|
{ |
|
75
|
37 |
|
if ($right !== null) { |
|
76
|
37 |
|
$right->setParent($this); |
|
77
|
|
|
} |
|
78
|
37 |
|
$this->right = $right; |
|
79
|
37 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
*/ |
|
84
|
37 |
|
final public function getItem(): ?ItemInterface |
|
85
|
|
|
{ |
|
86
|
37 |
|
return $this->item; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @codeCoverageIgnore |
|
91
|
|
|
* @param mixed $item |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
final public function setItem(ItemInterface $item): void |
|
95
|
|
|
{ |
|
96
|
|
|
$this->item = $item; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return TreeNodeInterface[] |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getChildren(): array |
|
103
|
|
|
{ |
|
104
|
24 |
|
return array_filter([$this->left, $this->right], function(?BinaryNodeInterface $item) { |
|
105
|
24 |
|
return !empty($item); |
|
106
|
24 |
|
}); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return TreeNodeInterface |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getParent(): ?TreeNodeInterface |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->parent; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param TreeNodeInterface $parent |
|
119
|
|
|
*/ |
|
120
|
37 |
|
public function setParent(?TreeNodeInterface $parent): void |
|
121
|
|
|
{ |
|
122
|
37 |
|
$this->parent = $parent; |
|
123
|
37 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* For visitors |
|
127
|
|
|
* @param VisitorInterface $visitor |
|
128
|
|
|
* @return ArrayListInterface |
|
129
|
|
|
*/ |
|
130
|
28 |
|
public function accept(VisitorInterface $visitor): ArrayListInterface |
|
131
|
|
|
{ |
|
132
|
28 |
|
return $visitor->visit($this); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|