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

BinaryTreeAbstract   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 35
Duplicated Lines 62.86 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 0
dl 22
loc 35
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B search() 22 22 6
A isLeaf() 0 3 3
A isRoot() 0 3 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
}