Completed
Push — master ( 7164ce...f4198e )
by Siro Díaz
01:38
created

AVLTree   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 7.69 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 2
dl 7
loc 91
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createNode() 0 3 1
A rightRotation() 0 3 1
B leftRotation() 7 22 4
A doubleRightRotation() 0 4 1
A doubleLeftRotation() 0 4 1
A put() 0 3 1
A adjustHeight() 0 3 1

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
 * DataStructures for PHP
4
 *
5
 * @link      https://github.com/SiroDiaz/DataStructures
6
 * @copyright Copyright (c) 2017 Siro Díaz Palazón
7
 * @license   https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License)
8
 */
9
namespace DataStructures\Trees;
10
11
use DataStructures\Trees\BinaryTreeAbstract;
12
use DataStructures\Trees\Nodes\AVLNode;
13
14
/**
15
 * AVLTree
16
 *
17
 * This class is an implementation of a AVL tree. It has as main feature that access,
18
 * insertion and deletion is O(log n). This is like a BST but it has a slightly difference
19
 * in nodes: contains a height attribute, used to detect when is needed to rebalance the
20
 * tree to keep the O(log n).
21
 *
22
 * @author Siro Diaz Palazon <[email protected]>
23
 */
24
class AVLTree extends BinaryTreeAbstract {
25
    
26
    /**
27
     * Creates a AVLNode.
28
     *
29
     * @param int|string $key the key used to store.
30
     * @param mixed $data the data.
31
     * @param DataStructures\Trees\Nodes\AVLNode|null $parent the parent node.
32
     * @param DataStructures\Trees\Nodes\AVLNode|null $left the left child node.
33
     * @param DataStructures\Trees\Nodes\AVLNode|null $right the right child node.
34
     *
35
     * @return DataStructures\Trees\Nodes\AVLNode the new node created.
36
     */
37
    public function createNode($key, $data, $parent = null, $left = null, $right = null) {
38
        return new AVLNode($key, $data, $parent, $left, $right);
39
    }
40
41
    /**
42
     * Does a right rotation.
43
     * Example, rotate Y
44
     *    k2                   k1
45
     *   /  \                 /  \
46
     *  k1   Z     ==>       X   k2
47
     * / \                      /  \
48
     *X   Y                    Y    Z
49
     *
50
     */
51
    private function rightRotation(AVLNode $node) {
0 ignored issues
show
Unused Code introduced by
The parameter $node is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
        
53
    }
54
55
    /*  Does a right rotation.
56
     *    k2                       k1
57
     *  /  \                     /  \
58
     * X    k1         ==>      k2   Z
59
     *     /  \                /  \
60
     *    Y    Z              X    Y
61
     */
62
    private function leftRotation(AVLNode $node) {
63
        $temp = &$node->right;
64 View Code Duplication
        if($node->parent !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
65
            if($node->parent->right === $node) {
66
                $node->parent->right = &$node;
67
            } else {
68
                $node->parent->left = &$node;
69
            }
70
        }
71
        $temp->parent = &$node->parent;
72
        $node->parent = &$temp;
73
        $node->right = $temp->left;
74
        if($node->right !== null) {
75
            $node->right->parent = &$node;
76
        }
77
        $temp->left = &$node;
78
79
        $this->adjustHeight($node);
80
        $this->adjustHeight($temp);
81
82
        return $temp;
83
    }
84
85
    /**
86
     * Double right rotation does first a left rotation of right child node
87
     * that detects the imbalance and finally does a right rotation
88
     * in the subtree root that detects the imbalance.
89
     * Case Right-Left.
90
     */
91
    private function doubleRightRotation(AVLNode $node) {
92
        $this->leftRotation($node->right);
93
        $this->rightRotation($node);
0 ignored issues
show
Unused Code introduced by
The call to the method DataStructures\Trees\AVLTree::rightRotation() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
94
    }
95
96
    /**
97
     * Double left rotation does first a right rotation of left child node
98
     * that detects the imbalance and finally does a left rotation
99
     * in the subtree root that detects the imbalance.
100
     * Case Left-Right.
101
     */
102
    private function doubleLeftRotation(AVLNode $node) {
103
        $this->rightRotation($node->left);
0 ignored issues
show
Unused Code introduced by
The call to the method DataStructures\Trees\AVLTree::rightRotation() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
104
        $this->leftRotation($node);
105
    }
106
107
    public function put($key, $data, $update = false) {
108
        $nodeInserted = parent::put($key, $data, $update);
0 ignored issues
show
Unused Code introduced by
$nodeInserted is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
109
    }
110
111
    private function adjustHeight(AVLNode $node) {
112
        $node->height = 1 + max($node->left->height, $node->right->height);
113
    }
114
}