forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplayTree.php
446 lines (396 loc) · 14.7 KB
/
SplayTree.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
/*
* Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) in Pull Request: #168
* https://github.com/TheAlgorithms/PHP/pull/168
*
* Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request addressing bugs/corrections to this file.
* Thank you!
*/
namespace DataStructures\SplayTree;
require_once 'SplayTreeRotations.php';
use LogicException;
class SplayTree extends SplayTreeRotations
{
protected ?SplayTreeNode $root = null;
private int $counter = 0;
/**
* Initializes the splay tree with an optional array.
* Otherwise, with manual insert() after initialization.
*
* @param array $initialData The input array for the splay tree
*/
public function __construct(array $initialData = [])
{
foreach ($initialData as $key => $value) {
$this->insert($key, $value);
}
}
/**
* @return SplayTreeNode|NULL The root node of the Splay Tree.
*/
public function getRoot(): ?SplayTreeNode
{
return $this->root;
}
/**
* Set the root node of the Splay Tree.
* @param SplayTreeNode $node
*/
protected function setRoot(SplayTreeNode $node): void
{
$this->root = $node;
}
/**
* Get the number of nodes in the Splay Tree.
*/
public function size(): int
{
return $this->counter;
}
/**
* Check if current splay tree is empty
*/
public function isEmpty(): bool
{
return $this->root === null;
}
/**
* Splay the given node to the root of the tree.
* Keep rotating until the node becomes the root.
*
* Time complexity: Amortized O(log n)
*
* @param SplayTreeNode|NULL $node The current node being splayed
* @param int $key The node's key being searched for and splayed
* @return SplayTreeNode|NULL Returns the new root after splaying
*/
protected function splay(?SplayTreeNode $node, int $key): ?SplayTreeNode
{
if ($node === null || $node->key === $key) {
return $node;
}
return $node->key > $key
? $this->splayLeft($node, $key) // Key is in the Left subtree
: $this->splayRight($node, $key); // Key is in the Right subtree
}
/**
* Handles the splay operation when the key is located in the left subtree.
* This includes Zig-Zig (Left-Left), Zig-Zag (Left-Right), and Zig (Left) cases.
*
* Time complexity: Amortized O(log n).
*
* @param SplayTreeNode|null $node The root node of the subtree to splay
* @param int $key The key to splay to the root
* @return SplayTreeNode|null Returns the new root after splaying
*/
private function splayLeft(?SplayTreeNode $node, int $key): ?SplayTreeNode
{
if ($node->left === null) {
return $node; // Key not found in the left subtree
}
if ($node->left->key > $key) { // Zig-Zig (Left-Left case)
$node->left->left = $this->splay($node->left->left, $key);
return $this->zigZig($node);
} elseif ($node->left->key < $key) { // Zig-Zag (Left-Right case)
$node->left->right = $this->splay($node->left->right, $key);
if ($node->left->right !== null) {
return $this->zigZag($node);
}
}
// Zig (Left case)
return $node->left === null
? $node
: $this->zig($node);
}
/**
* Handles the splay operation when the key is located in the right subtree.
* This includes Zag-Zag (Right-Right), Zag-Zig (Right-Left), and Zag (Right) cases.
*
* Time complexity: Amortized O(log n).
*
* @param SplayTreeNode|null $node The root node of the subtree to splay
* @param int $key The key to splay to the root
* @return SplayTreeNode|null Returns the new root after splaying
*/
private function splayRight(?SplayTreeNode $node, int $key): ?SplayTreeNode
{
if ($node->right === null) {
return $node;
}
if ($node->right->key < $key) { // Zag-Zag (Right-Right case)
$node->right->right = $this->splay($node->right->right, $key);
return $this->zagZag($node);
} elseif ($node->right->key > $key) { // Zag-Zig (Right-Left case)
$node->right->left = $this->splay($node->right->left, $key);
if ($node->right->left !== null) {
return $this->zagZig($node);
}
}
// Zag (Right case)
return $node->right === null
? $node
: $this->zag($node);
}
/**
* Insert a new element into the splay tree following binary search tree insertion.
* Then, apply rotations to bring the newly inserted node to the root of the tree.
*
* Time complexity: O(log n) for the splay operation.
*
* @param int $key The node's key to insert
* @param mixed $value The value associated with the key
* @return SplayTreeNode|null Returns the new root after insertion and splaying
* @throws LogicException If the key already exists
*/
public function insert(int $key, $value): ?SplayTreeNode
{
$this->root = $this->insertNode($this->root, $key, $value);
$this->counter++;
return $this->splay($this->root, $key);
}
/**
* Insert a key-value pair into the Splay Tree.
* Recursively inserts a node in the BST fashion and update parent references.
*
* Time complexity: O(log n) for binary search tree insertion.
*
* @param SplayTreeNode|null $node The (sub)tree's root node to start inserting after
* @param int $key The node's key to insert
* @param mixed $value The value associated with the key
* @return SplayTreeNode|null Returns the new root after insertion
* @throws LogicException If the key already exists
*/
private function insertNode(?SplayTreeNode $node, int $key, $value): SplayTreeNode
{
if ($node === null) {
return new SplayTreeNode($key, $value);
}
if ($key < $node->key) {
$node->left = $this->insertNode($node->left, $key, $value);
$node->left->parent = $node;
} elseif ($key > $node->key) {
$node->right = $this->insertNode($node->right, $key, $value);
$node->right->parent = $node;
} else {
throw new LogicException("Duplicate key: " . $key);
}
return $node;
}
/**
* Search for an element in the tree by performing a binary search tree search.
* If the node is found, apply rotations to bring it to the root of the tree.
* Otherwise, apply rotations to the last node visited in the search.
*
* Time complexity: O(log n) for the splay operation.
*
* @param int $key The node's key being searched
* @return SplayTreeNode|null Returns the new root of either the found node or the last visited
*/
public function search(int $key): ?SplayTreeNode
{
if ($this->isEmpty()) {
return null;
}
//
$lastVisited = null;
$node = $this->searchNode($this->root, $key, $lastVisited);
$this->root = $node !== null
? $this->splay($this->root, $key)
: $this->splay($this->root, $lastVisited->key);
return $this->root;
}
/**
* Recursively searches for a node in the BST fashion.
*
* Time complexity: O(log n) for binary search tree search.
*
* @param SplayTreeNode|null $node The (sub)tree's root node to start searching from
* @param int $key The node's key being searched
* @param SplayTreeNode|null $lastVisited Keep track of the last visited node
* @return SplayTreeNode|null Returns the new root of the found node or Null
*/
private function searchNode(?SplayTreeNode $node, int $key, ?SplayTreeNode &$lastVisited): ?SplayTreeNode
{
if ($node === null) {
return null;
}
$lastVisited = $node;
if ($key < $node->key) {
return $this->searchNode($node->left, $key, $lastVisited);
} elseif ($key > $node->key) {
return $this->searchNode($node->right, $key, $lastVisited);
} else {
return $node;
}
}
/**
* Check if a node with the given key exists in the tree.
* Apply rotations to the searched node or to the last visited node to the root of the tree.
*
* Time complexity: O(log n) for the splay operation.
*
* @param int $key The key of the node being searched
* @return bool True if the node exists, false otherwise
*/
public function isFound(int $key): bool
{
$foundNode = $this->search($key);
return $foundNode && $foundNode->key === $key;
}
/**
* Updates the value of the node with the given key.
* If the node is found, update its value and apply rotations to bring it to the root of the tree.
* Otherwise, apply rotations to the last node visited in the search.
*
* Time complexity: O(log n) for the splay operation.
*
* @param int $key The key of the node to update
* @param mixed $value The new value to set
* @return SplayTreeNode|null Returns the root of the tree after the update or the last visited
*/
public function update(int $key, $value): ?SplayTreeNode
{
if ($this->isFound($key)) {
$this->root->value = $value;
}
return $this->root;
}
/**
* Deletes the node with the given key from the tree.
* Splays the node to be deleted to the root, then isolates it and restructures the tree.
* If the key doesn't exist, apply rotations to bring the closest node to the root.
*
* Time complexity: O(log n) for the splay operation and O(log n) for restructuring the tree.
*
* @param int $key The key of the node to delete
* @return SplayTreeNode|null The new root after the deletion
* @throws LogicException If the key is not found in the tree
*/
public function delete(int $key): ?SplayTreeNode
{
if (!$this->isFound($key)) {
throw new LogicException("Key: " . $key . " not found in tree. Splayed the last visited node.");
}
$leftSubtree = $this->root->left;
$rightSubtree = $this->root->right;
$this->isolateRoot();
$this->counter--;
return $this->restructureAfterDeletion($leftSubtree, $rightSubtree);
}
/**
* Isolates the root node by breaking its connections.
*/
private function isolateRoot(): void
{
if ($this->root->left !== null) {
$this->root->left->parent = null;
}
if ($this->root->right !== null) {
$this->root->right->parent = null;
}
$this->root->left = null;
$this->root->right = null;
}
/**
* Restructures the tree after the root node has been isolated for deletion.
*
* @return SplayTreeNode|null The new root after restructuring
*/
private function restructureAfterDeletion(?SplayTreeNode $leftSubtree, ?SplayTreeNode $rightSubtree): ?SplayTreeNode
{
if ($leftSubtree === null) {
return $this->handleEmptyLeftSubtree($rightSubtree);
}
return $this->mergeSubtrees($leftSubtree, $rightSubtree);
}
/**
* Handles the case when the left subtree is empty after deletion.
*
* @param SplayTreeNode|null $rightSubtreeRoot The root of the right subtree
* @return SplayTreeNode|null The new root
*/
private function handleEmptyLeftSubtree(?SplayTreeNode $rightSubtreeRoot): ?SplayTreeNode
{
$this->root = $rightSubtreeRoot;
if ($this->root !== null) {
$this->root->parent = null;
}
return $this->root;
}
/**
* Merges the left and right subtrees after deletion.
*
* @param SplayTreeNode $leftSubtreeRoot The root of the left subtree
* @param SplayTreeNode|null $rightSubtreeRoot The root of the right subtree
* @return SplayTreeNode The new root after merging
*/
private function mergeSubtrees(SplayTreeNode $leftSubtreeRoot, ?SplayTreeNode $rightSubtreeRoot): SplayTreeNode
{
$maxLeftNode = $this->maxNode($leftSubtreeRoot);
$this->root = $maxLeftNode;
$this->detachMaxNodeFromLeftSubtree($maxLeftNode, $leftSubtreeRoot);
$this->attachRightSubtree($rightSubtreeRoot);
return $this->root;
}
/**
* Detaches the max node from the left subtree and reattaches the left subtree to the new root.
*
* @param SplayTreeNode $maxLeftNode The max node of the left subtree
* @param SplayTreeNode $leftSubtreeRoot The root of the left subtree
*/
private function detachMaxNodeFromLeftSubtree(SplayTreeNode $maxLeftNode, SplayTreeNode $leftSubtreeRoot): void
{
$maxLeftNodeParent = $maxLeftNode->parent;
if ($maxLeftNodeParent !== null) {
$maxLeftNodeParent->right = null;
$this->root->left = $leftSubtreeRoot;
$leftSubtreeRoot->parent = $this->root;
}
$maxLeftNode->parent = null;
}
/**
* Attaches the right subtree to the new root.
*
* @param SplayTreeNode|null $rightSubtreeRoot The root of the right subtree
*/
private function attachRightSubtree(?SplayTreeNode $rightSubtreeRoot): void
{
$this->root->right = $rightSubtreeRoot;
if ($rightSubtreeRoot !== null) {
$rightSubtreeRoot->parent = $this->root;
}
}
/**
* Finds the node with the maximum key in the given subtree.
*
* Time complexity: O(log n) for finding the maximum node in the subtree.
*
* @param SplayTreeNode|null $node The subtree to search for the maximum node
* @return SplayTreeNode|null The node with the maximum key, or null if the subtree is empty
*/
public function maxNode(?SplayTreeNode $node): ?SplayTreeNode
{
if ($node === null) {
return null;
}
return $node->right === null
? $node
: $this->maxNode($node->right);
}
/**
* Perform an in-order traversal of the Splay Tree.
*
* @param SplayTreeNode|null $node The root node to start traversing from
* @return array Representation of the traversed nodes
*/
public function inOrderTraversal(?SplayTreeNode $node): array
{
$result = [];
if ($node !== null) {
$result = array_merge($result, $this->inOrderTraversal($node->left));
$result[] = [$node->key => $node->value];
$result = array_merge($result, $this->inOrderTraversal($node->right));
}
return $result;
}
}