Skip to content

Commit 9abf82f

Browse files
committed
refactoring (use methods)
1 parent 3c6d155 commit 9abf82f

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

src/IntervalTree.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,32 +180,31 @@ private function insertFixup(Node $insertNode): void
180180
$currentNode->getParent()->setColor(NodeColor::black());
181181
$uncleNode->setColor(NodeColor::black());
182182
$grandfather->setColor(NodeColor::red());
183-
$currentNode = $currentNode->getParent()->getParent();
183+
$currentNode = $grandfather;
184184
} else {
185185
if ($currentNode === $currentNode->getParent()->getRight()) {
186186
$currentNode = $currentNode->getParent();
187187
$this->rotateLeft($currentNode);
188188
}
189189
$currentNode->getParent()->setColor(NodeColor::black());
190190
$grandfather->setColor(NodeColor::red());
191-
$this->rotateRight($currentNode->getParent()->getParent());
191+
$this->rotateRight($grandfather);
192192
}
193193
} else {
194-
$grandfather = $currentNode->getParent()->getParent();
195194
$uncleNode = $grandfather->getLeft();
196195
if ($uncleNode->getColor()->isRed()) {
197196
$currentNode->getParent()->setColor(NodeColor::black());
198197
$uncleNode->setColor(NodeColor::black());
199198
$grandfather->setColor(NodeColor::red());
200-
$currentNode = $currentNode->getParent()->getParent();
199+
$currentNode = $grandfather;
201200
} else {
202201
if ($currentNode === $currentNode->getParent()->getLeft()) {
203202
$currentNode = $currentNode->getParent();
204203
$this->rotateRight($currentNode);
205204
}
206205
$currentNode->getParent()->setColor(NodeColor::black());
207206
$grandfather->setColor(NodeColor::red());
208-
$this->rotateLeft($currentNode->getParent()->getParent());
207+
$this->rotateLeft($grandfather);
209208
}
210209
}
211210
}
@@ -385,7 +384,6 @@ private function treeSuccessor(Node $node): ?Node
385384
private function rotateLeft(Node $x): void
386385
{
387386
$y = $x->getRight();
388-
389387
$x->setRight($y->getLeft()); // b goes to x.right
390388

391389
if ($y->getLeft() !== $this->nilNode) {
@@ -449,7 +447,10 @@ private function rotateRight(Node $y): void
449447
*/
450448
private function treeWalk(): Iterator
451449
{
452-
$stack = [$this->root];
450+
if ($this->root !== null) {
451+
$stack = [$this->root];
452+
yield $this->root;
453+
}
453454
while (!empty($stack)) {
454455
$node = array_pop($stack);
455456
if ($node->getLeft() !== $this->nilNode) {

src/Node.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,23 @@ public function copyPairFrom(Node $otherNode): void
126126
public function updateMax(): void
127127
{
128128
$this->max = $this->getPair()->getInterval();
129-
130129
if ($this->getRight()->max !== null) {
131130
$this->max = $this->max->merge($this->getRight()->max);
132131
}
133-
if ($this->left->max !== null) {
132+
if ($this->getLeft()->max !== null) {
134133
$this->max = $this->max->merge($this->getLeft()->max);
135134
}
136135
}
137136

138-
// Other_node does not intersect any node of left subtree, if this.left.max < other_node.item.key.low
139137
public function notIntersectLeftSubtree(Node $searchNode): bool
140138
{
141139
$high = $this->getLeft()->max->getHigh() ?? $this->getLeft()->getPair()->getInterval()->getHigh();
142140
return $high < $searchNode->getPair()->getInterval()->getLow();
143141
}
144142

145-
// Other_node does not intersect right subtree if other_node.item.key.high < this.right.key.low
146143
public function notIntersectRightSubtree(Node $searchNode): bool
147144
{
148-
//const comparable_less_than = this.item.key.constructor.comparable_less_than; // static method
149-
$low = $this->right->max->getLow() ?? $this->getRight()->getPair()->getInterval()->getLow();
145+
$low = $this->getRight()->max->getLow() ?? $this->getRight()->getPair()->getInterval()->getLow();
150146
return $searchNode->getPair()->getInterval()->getHigh() < $low;
151147
}
152148
}

0 commit comments

Comments
 (0)