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
|
|
View Code Duplication |
private function rightRotation(AVLNode $node) { |
|
|
|
|
52
|
|
|
$temp = &$node->left; |
53
|
|
|
|
54
|
|
|
if($node->parent !== null) { |
55
|
|
|
if($node->parent->right !== null) { |
56
|
|
|
$node->parent->right = &$temp; |
57
|
|
|
} else { |
58
|
|
|
$node->parent->left = &$temp; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$temp->parent = &$node->parent; |
63
|
|
|
$node->parent = &$temp; |
64
|
|
|
$node->left = $temp->right; |
65
|
|
|
if($node->left !== null) { |
66
|
|
|
$node->left->parent = &$node; |
67
|
|
|
} |
68
|
|
|
$temp->right = &$node; |
69
|
|
|
|
70
|
|
|
$this->adjustHeight($node); |
71
|
|
|
$this->adjustHeight($temp); |
72
|
|
|
|
73
|
|
|
return $temp; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/* Does a right rotation. |
77
|
|
|
* k2 k1 |
78
|
|
|
* / \ / \ |
79
|
|
|
* X k1 ==> k2 Z |
80
|
|
|
* / \ / \ |
81
|
|
|
* Y Z X Y |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
private function leftRotation(AVLNode $node) { |
|
|
|
|
84
|
|
|
$temp = &$node->right; |
85
|
|
|
if($node->parent !== null) { |
86
|
|
|
if($node->parent->right === $node) { |
87
|
|
|
$node->parent->right = &$node; |
88
|
|
|
} else { |
89
|
|
|
$node->parent->left = &$node; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
$temp->parent = &$node->parent; |
93
|
|
|
$node->parent = &$temp; |
94
|
|
|
$node->right = $temp->left; |
95
|
|
|
if($node->right !== null) { |
96
|
|
|
$node->right->parent = &$node; |
97
|
|
|
} |
98
|
|
|
$temp->left = &$node; |
99
|
|
|
|
100
|
|
|
$this->adjustHeight($node); |
101
|
|
|
$this->adjustHeight($temp); |
102
|
|
|
|
103
|
|
|
return $temp; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Double right rotation does first a left rotation of right child node |
108
|
|
|
* that detects the imbalance and finally does a right rotation |
109
|
|
|
* in the subtree root that detects the imbalance. |
110
|
|
|
* Case Right-Left. |
111
|
|
|
*/ |
112
|
|
|
private function doubleRightRotation(AVLNode $node) { |
113
|
|
|
$this->leftRotation($node->right); |
114
|
|
|
$this->rightRotation($node); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Double left rotation does first a right rotation of left child node |
119
|
|
|
* that detects the imbalance and finally does a left rotation |
120
|
|
|
* in the subtree root that detects the imbalance. |
121
|
|
|
* Case Left-Right. |
122
|
|
|
*/ |
123
|
|
|
private function doubleLeftRotation(AVLNode $node) { |
124
|
|
|
$this->rightRotation($node->left); |
125
|
|
|
$this->leftRotation($node); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function put($key, $data, $update = false) { |
129
|
|
|
$nodeInserted = parent::put($key, $data, $update); |
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function adjustHeight(AVLNode $node) { |
133
|
|
|
$node->height = 1 + max($node->left->height, $node->right->height); |
134
|
|
|
} |
135
|
|
|
} |
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.