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

BinaryTreeAbstract   B

Complexity

Total Complexity 36

Size/Duplication

Total Lines 104
Duplicated Lines 42.31 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 0
dl 44
loc 104
rs 8.8
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A empty() 0 3 1
A size() 0 3 1
A put() 0 1 1
A putOrUpdate() 0 1 1
B get() 22 22 6
A getRoot() 0 3 1
B exists() 0 22 6
A floor() 0 1 1
A ceil() 0 1 1
A min() 0 1 1
A max() 0 1 1
A deleteMin() 0 1 1
A deleteMax() 0 1 1
A delete() 0 1 1
A count() 0 1 1
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
    public function empty() {
12
        return $this->root === null;
13
    }
14
15
    public function size() {
16
        return $this->size;
17
    }
18
19
    public function put($key, $data){}
20
    public function putOrUpdate($key, $data){}
21
22 View Code Duplication
    public function get($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...
23
        if($this->root === null) {
24
            return null;
25
        }
26
27
        if($this->root->key === $key) {
28
            return $this->root->data;
29
        } else {
30
            $node = $this->root;
31
            while($node !== null) {
32
                if($key < $node->left) {
33
                    $node = $node->left;
34
                } else if($key > $node->right) {
35
                    $node = $node->right;
36
                } else {
37
                    return $node->data;
38
                }
39
            }
40
        }
41
42
        return null;
43
    }
44
    public function getRoot(){
45
        return $this->root;
46
    }
47
48
    public function exists($key){
49
        if($this->root === null) {
50
            return false;
51
        }
52
53
        if($this->root->key === $key) {
54
            return true;
55
        } else {
56
            $node = $this->root;
57
            while($node !== null) {
58
                if($key < $node->left) {
59
                    $node = $node->left;
60
                } else if($key > $node->right) {
61
                    $node = $node->right;
62
                } else {
63
                    return true;
64
                }
65
            }
66
        }
67
68
        return false;
69
    }
70
71
    public function floor($key){}
72
    public function ceil($key){}
73
    public function min(){}
74
    public function max(){}
75
    public function deleteMin(){}
76
    public function deleteMax(){}
77
    public function delete($key){}
78
    public function count() {}
79
80 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...
81
        if($this->root === null) {
82
            return null;
83
        }
84
85
        if($this->root->key === $key) {
86
            return $this->root->data;
87
        } else {
88
            $node = $this->root;
89
            while($node !== null) {
90
                if($key < $node->left) {
91
                    $node = $node->left;
92
                } else if($key > $node->right) {
93
                    $node = $node->right;
94
                } else {
95
                    return $node->data;
96
                }
97
            }
98
        }
99
100
        return null;
101
    }
102
    
103
    public function isLeaf($node) { // BinaryTreeNode
104
        return ($node !== null && $node->left === null && $node->right === null);
105
    }
106
107
    public function isRoot($node) {
108
        return $node !== null && $node->parent === null;
109
    }
110
}