forked from 2lovecode/code-segment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedBlackTree.php
316 lines (239 loc) · 7.34 KB
/
RedBlackTree.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/**
* code-segment
*
* @author liu hao<[email protected]>
* @copyright liu hao<[email protected]>
*
* 红黑树
* 1.节点是红色或黑色
* 2.根是黑色
* 3.所有的叶子节点都是黑色
* 4.每个红色节点必须有两个黑色节点
* 5.从任一节点到其叶子节点的简单路径都包含相同数量的黑色节点,黑高
*
*/
class Node
{
private $id;
/**
* @var
* 1 => red
* 2 => black
*/
private $color = 1;
private $value = null;
public $parent = 0;
public $left = 0;
public $right = 0;
public function __construct($value)
{
$this->id = uniqid();
$this->value = $value;
}
public function getID()
{
return $this->id;
}
public function getValue()
{
return $this->value;
}
public function setColor($color)
{
$this->color = $color;
}
public function getColor()
{
return $this->color;
}
}
class RedBlackTree
{
public $root = 0;
private $nodePool = [];
private $nil;
public function __construct()
{
$nilNode = new Node(null);
$nilNode->setColor(2);
$this->addNodeToPool($nilNode->getID(), $nilNode);
$this->nil = $nilNode->getID();
$this->root = $this->nil;
}
public function addNodeToPool($id, $object)
{
$this->nodePool[$id] = $object;
}
/**
* @param $id
* @return Node
*/
public function getNodeFromPool($id)
{
return isset($this->nodePool[$id]) ? $this->nodePool[$id] : null;
}
public function find($value)
{
$tmp = $this->getNodeFromPool($this->root);
while (!is_null($tmp->getValue())) {
if ($tmp->getValue() > $value) {
$tmp = $this->getNodeFromPool($tmp->left);
} else if ($tmp->getValue() < $value) {
$tmp = $this->getNodeFromPool($tmp->right);
} else {
return true;
}
}
return false;
}
public function walk($nodeID)
{
var_dump($this->root);
var_dump($this->nodePool);
}
public function insert($value)
{
$tmpNode = $this->getNodeFromPool($this->root);
$prevNode = $tmpNode;
while (!is_null($tmpNode->getValue())) {
$prevNode = $tmpNode;
if ($tmpNode->getValue() > $value) {
$tmpNode = $this->getNodeFromPool($tmpNode->left);
} else {
$tmpNode = $this->getNodeFromPool($tmpNode->right);
}
}
$newNode = new Node($value);
$newNode->left = $this->nil;
$newNode->right = $this->nil;
$newNode->parent = $prevNode->getID();
$this->addNodeToPool($newNode->getID(), $newNode);
if (is_null($prevNode->getValue())) {
//第一个节点
$newNode->setColor(2);
$this->root = $newNode->getID();
} else if ($prevNode->getValue() > $newNode->getValue()){
$prevNode->left = $newNode->getID();
} else {
$prevNode->right = $newNode->getID();
}
$this->insertFix($newNode->getID());
return true;
}
protected function insertFix($node)
{
$tmpNode = $this->getNodeFromPool($node);
while (!is_null($tmpNode->getValue()) && ($this->getNodeFromPool($tmpNode->parent)->getColor() == 1)) {
$grandP = $this->getNodeFromPool($this->getNodeFromPool($tmpNode->parent)->parent);
$parent = $this->getNodeFromPool($tmpNode->parent);
if ($tmpNode->parent == $grandP->left) {
$rightUncle = $this->getNodeFromPool($grandP->right);
if ($rightUncle->getColor() == 1) {
$parent->setColor(2);
$rightUncle->setColor(2);
$grandP->setColor(1);
$tmpNode = $grandP;
} else if ($tmpNode->getID() == $parent->right) {
$this->leftRotate($tmpNode->getID());
$tmpNode = $parent;
$this->rightRotate($tmpNode->parent, true);
} else {
$this->rightRotate($tmpNode->getID(), true);
}
} else {
$leftUncle = $this->getNodeFromPool($grandP->left);
if ($leftUncle->getColor() == 1) {
$parent->setColor(2);
$leftUncle->setColor(2);
$grandP->setColor(1);
$tmpNode = $grandP;
} else if ($tmpNode->getID() == $parent->left) {
$this->rightRotate($tmpNode->getID());
$tmpNode = $parent;
$this->leftRotate($tmpNode->parent, true);
} else {
$this->leftRotate($tmpNode->getID(), true);
}
}
$this->getNodeFromPool($this->root)->setColor(2);
}
}
protected function leftRotate($nodeID, $changeColor = false)
{
$node = $this->getNodeFromPool($nodeID);
$parentID = $node->parent;
$parent = $this->getNodeFromPool($parentID);
$grandPID = $parent->parent;
$grandP = $this->getNodeFromPool($grandPID);
if (is_null($grandP->getValue())) {
$this->root = $nodeID;
} else {
if ($grandP->left == $parentID) {
$grandP->left = $nodeID;
} else {
$grandP->right = $nodeID;
}
}
if ($changeColor) {
$node->setColor(2);
$parent->setColor(1);
}
$leftNode = $this->getNodeFromPool($node->left);
if (!is_null($leftNode->getValue())) {
$leftNode->parent = $parentID;
}
$parent->right = $node->left;
$parent->parent = $nodeID;
$node->parent = $grandPID;
$node->left = $parentID;
}
protected function rightRotate($nodeID, $changeColor = false)
{
$node = $this->getNodeFromPool($nodeID);
$parentID = $node->parent;
$parent = $this->getNodeFromPool($parentID);
$grandPID = $parent->parent;
$grandP = $this->getNodeFromPool($grandPID);
if (is_null($grandP->getValue())) {
$this->root = $nodeID;
} else {
if ($grandP->left == $parentID) {
$grandP->left = $nodeID;
} else {
$grandP->right = $nodeID;
}
}
if ($changeColor) {
$node->setColor(2);
$parent->setColor(1);
}
$rightNode = $this->getNodeFromPool($node->right);
if (!is_null($rightNode->getValue())) {
$rightNode->parent = $parentID;
}
$parent->left = $node->right;
$parent->parent = $nodeID;
$node->parent = $grandPID;
$node->right = $parentID;
}
public function delete($value)
{
//TODO
}
}
$test = [12, 550, 300, 20, 1, 50, 44, 34, 24, 25, 79, 11, 21, 1000, 3000, 99];
$testH = [12, 550, 300, 20, 1, 50, 44, 34, 24, 25, 79, 19, 99];
echo '<pre>';
$tree = new RedBlackTree();
foreach ($test as $value) {
$tree->insert($value);
}
foreach ($testH as $v) {
if ($tree->find($v)) {
echo 'true;';
} else {
echo 'false;';
}
}
//$tree->walk($tree->root);