forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplayTreeRotations.php
175 lines (150 loc) · 5.79 KB
/
SplayTreeRotations.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
<?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;
abstract class SplayTreeRotations
{
abstract protected function splay(?SplayTreeNode $node, int $key): ?SplayTreeNode;
abstract protected function setRoot(SplayTreeNode $node): void;
/**
* Zig rotation (single right rotation).
* Performs a right rotation on the given node.
* A case where the node is directly a left child of its parent.
*
* @param SplayTreeNode $node The node to be rotated.
* @return SplayTreeNode The new root of the subtree after rotation.
*/
protected function zig(SplayTreeNode $node): SplayTreeNode
{
return $this->rotateRight($node);
}
/**
* Zag rotation (single left rotation).
* Performs a left rotation on the given node.
* A case where the node is directly a right child of its parent.
*
* @param SplayTreeNode $node The node to be rotated.
* @return SplayTreeNode The new root of the subtree after rotation.
*/
protected function zag(SplayTreeNode $node): SplayTreeNode
{
return $this->rotateLeft($node);
}
/**
* Zig-Zig rotation (double right rotation).
* Performs two consecutive right rotations on the given node. The first right rotation is applied to
* the node’s parent, and the second one to the node’s new parent (the previous grandparent).
*
* @param SplayTreeNode $node The node to be rotated.
* @return SplayTreeNode The new root of the subtree after the rotations.
*/
protected function zigZig(SplayTreeNode $node): SplayTreeNode
{
$node = $this->rotateRight($node);
return $this->rotateRight($node);
}
/**
* Zag-Zag rotation (double left rotation).
* Performs two consecutive left rotations on the given node. The first left rotation is applied to
* the node’s parent, and the second one to the node’s new parent (the previous grandparent).
*
* @param SplayTreeNode $node The node to be rotated.
* @return SplayTreeNode The new root of the subtree after the rotations.
*/
protected function zagZag(SplayTreeNode $node): SplayTreeNode
{
$node = $this->rotateLeft($node);
return $this->rotateLeft($node);
}
/**
* Zig-Zag rotation (left-right rotation).
* Performs a left rotation on the left child followed by a right rotation on the node itself.
*
* A case when the target key is in the right subtree of the left child.
*
* @param SplayTreeNode $node The node to be rotated.
* @return SplayTreeNode The new root of the subtree after the rotations.
*/
protected function zigZag(SplayTreeNode $node): SplayTreeNode
{
$node->left = $this->rotateLeft($node->left);
return $this->rotateRight($node);
}
/**
* Zag-Zig rotation (right-left rotation).
* Performs a right rotation on the right child followed by a left rotation on the node itself.
*
* A case when the target key is in the left subtree of the right child.
*
* @param SplayTreeNode $node The node to be rotated.
* @return SplayTreeNode The new root of the subtree after the rotations.
*/
protected function zagZig(SplayTreeNode $node): SplayTreeNode
{
$node->right = $this->rotateRight($node->right);
return $this->rotateLeft($node);
}
/**
* Rotates the given node to the left, bringing its right child up to take its place.
* The left subtree of the node's right child will become the new right subtree of the node.
*
* @param SplayTreeNode $node The node to be rotated.
* @return SplayTreeNode The new root of the subtree after the rotation (the former right child).
*/
private function rotateLeft(SplayTreeNode $node): SplayTreeNode
{
$rightChild = $node->right;
if ($rightChild === null) {
return $node; // No rotation possible
}
$node->right = $rightChild->left;
if ($rightChild->left !== null) {
$rightChild->left->parent = $node;
}
$rightChild->parent = $node->parent;
if ($node->parent === null) {
static::setRoot($rightChild);
} elseif ($node === $node->parent->left) {
$node->parent->left = $rightChild;
} else {
$node->parent->right = $rightChild;
}
$rightChild->left = $node;
$node->parent = $rightChild;
return $rightChild;
}
/**
* Rotates the given node to the right, bringing its left child up to take its place.
* The right subtree of the node's left child will become the new left subtree of the node.
*
* @param SplayTreeNode $node The node to be rotated.
* @return SplayTreeNode The new root of the subtree after the rotation (the former left child).
*/
private function rotateRight(SplayTreeNode $node): SplayTreeNode
{
$leftChild = $node->left;
if ($leftChild === null) {
return $node; // No rotation possible
}
$node->left = $leftChild->right;
if ($leftChild->right !== null) {
$leftChild->right->parent = $node;
}
$leftChild->parent = $node->parent;
if ($node->parent === null) {
static::setRoot($leftChild);
} elseif ($node === $node->parent->right) {
$node->parent->right = $leftChild;
} else {
$node->parent->left = $leftChild;
}
$leftChild->right = $node;
$node->parent = $leftChild;
return $leftChild;
}
}