Skip to content

Commit e18accd

Browse files
committed
Fix updating parent max
1 parent 9abf82f commit e18accd

File tree

2 files changed

+12
-13
lines changed

2 files changed

+12
-13
lines changed

src/IntervalTree.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -341,18 +341,14 @@ private function treeSearch(Node $node, Node $searchNode): ?Node
341341
private function treeSearchInterval(Node $searchNode, Node $fromNode = null): Iterator
342342
{
343343
$fromNode = $fromNode ?? $this->root;
344-
$stack = [$fromNode];
345-
while (!empty($stack)) {
346-
$node = array_pop($stack);
347-
if ($node->getLeft() !== $this->nilNode && !$node->notIntersectLeftSubtree($searchNode)) {
348-
yield from $this->treeSearchInterval($searchNode, $node->getLeft());
349-
}
350-
if ($node->intersect($searchNode)) {
351-
yield $node;
352-
}
353-
if ($node->getRight() !== $this->nilNode && !$node->notIntersectRightSubtree($searchNode)) {
354-
yield from $this->treeSearchInterval($searchNode, $node->getRight());
355-
}
344+
if ($fromNode->getLeft() !== $this->nilNode && !$fromNode->notIntersectLeftSubtree($searchNode)) {
345+
yield from $this->treeSearchInterval($searchNode, $fromNode->getLeft());
346+
}
347+
if ($fromNode->intersect($searchNode)) {
348+
yield $fromNode;
349+
}
350+
if ($fromNode->getRight() !== $this->nilNode && !$fromNode->notIntersectRightSubtree($searchNode)) {
351+
yield from $this->treeSearchInterval($searchNode, $fromNode->getRight());
356352
}
357353
}
358354

@@ -445,7 +441,7 @@ private function rotateRight(Node $y): void
445441
/**
446442
* @return Iterator<Node>
447443
*/
448-
private function treeWalk(): Iterator
444+
public function treeWalk(): Iterator
449445
{
450446
if ($this->root !== null) {
451447
$stack = [$this->root];

src/Node.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ public function updateMax(): void
132132
if ($this->getLeft()->max !== null) {
133133
$this->max = $this->max->merge($this->getLeft()->max);
134134
}
135+
if ($this->getParent() !== null) {
136+
$this->getParent()->updateMax();
137+
}
135138
}
136139

137140
public function notIntersectLeftSubtree(Node $searchNode): bool

0 commit comments

Comments
 (0)