Completed
Push — master ( b8dd19...10cc44 )
by Siro Díaz
02:16
created

BinaryTreeAbstract::search()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 22
Code Lines 15

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 22
loc 22
rs 8.6737
cc 6
eloc 15
nc 6
nop 1
1
<?php
2
3
namespace DataStructures\Trees;
4
5
use DataStructures\Trees\Interfaces\TreeInterface;
6
7
abstract class BinaryTreeAbstract implements TreeInterface {
8
    protected $root;
9
    protected $size;
10
11 View Code Duplication
    public function search($key) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
12
        if($this->root === null) {
13
            return null;
14
        }
15
16
        if($this->root->key === $key) {
17
            return $this->root->data;
18
        } else {
19
            $node = $this->root;
20
            while($node !== null) {
21
                if($key < $node->left) {
22
                    $node = $node->left;
23
                } else if($key > $node->right) {
24
                    $node = $node->right;
25
                } else {
26
                    return $node->data;
27
                }
28
            }
29
        }
30
31
        return null;
32
    }
33
    
34
    public function isLeaf(BinaryTreeNode $node) {
35
        return ($node !== null && $node->left === null && $node->right === null);
36
    }
37
38
    public function isRoot($node) {
39
        return $node !== null && $node->parent === null;
40
    }
41
}