Passed
Push — master ( 7d3520...28b042 )
by Sebastian
24:23 queued 16:06
created

BinaryNode   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 13
eloc 19
dl 0
loc 109
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getItem() 0 3 1
A accept() 0 3 1
A setParent() 0 3 1
A getLeft() 0 3 1
A setLeft() 0 6 2
A getRight() 0 3 1
A getChildren() 0 4 1
A __construct() 0 3 1
A getParent() 0 3 1
A setRight() 0 6 2
A setItem() 0 3 1
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