-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace spec\drupol\phptree\Node; | ||
|
||
use drupol\phptree\Node\ABNode; | ||
|
||
class ABNodeSpec extends NodeObjectBehavior | ||
{ | ||
public function it_balance_a_tree() | ||
{ | ||
$this->beConstructedWith(1); | ||
|
||
$data = \range(0, 40); | ||
|
||
$nodes = []; | ||
foreach ($data as $key => $value) { | ||
$nodes[$key] = new ABNode(2); | ||
} | ||
|
||
$this | ||
->add(...$nodes); | ||
|
||
$this->offsetGet(0)->count()->shouldReturn(40); | ||
$this | ||
->offsetGet(0) | ||
->offsetGet(0) | ||
->count() | ||
->shouldReturn(19); | ||
$this | ||
->offsetGet(0) | ||
->offsetGet(1) | ||
->count() | ||
->shouldReturn(19); | ||
|
||
$this | ||
->add(new ABNode(2)); | ||
|
||
$this | ||
->offsetGet(0) | ||
->offsetGet(0) | ||
->count() | ||
->shouldReturn(19); | ||
$this | ||
->offsetGet(0) | ||
->offsetGet(1) | ||
->count() | ||
->shouldReturn(20); | ||
|
||
$this | ||
->add(new ABNode(2)); | ||
|
||
$this | ||
->offsetGet(0) | ||
->offsetGet(0) | ||
->count() | ||
->shouldReturn(20); | ||
$this | ||
->offsetGet(0) | ||
->offsetGet(1) | ||
->count() | ||
->shouldReturn(20); | ||
} | ||
public function it_is_initializable() | ||
{ | ||
$this->shouldHaveType(ABNode::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace drupol\phptree\Node; | ||
|
||
/** | ||
* Class ABNode. | ||
* | ||
* An auto-balanced node. | ||
*/ | ||
class ABNode extends NaryNode | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function add(NodeInterface ...$nodes): NodeInterface | ||
{ | ||
foreach ($nodes as $node) { | ||
if (0 === $this->count()) { | ||
parent::add($node); | ||
|
||
continue; | ||
} | ||
|
||
$count = []; | ||
|
||
foreach ($this->children() as $child) { | ||
$count[$child->count()] = $child; | ||
} | ||
|
||
$keys = \array_keys($count); | ||
$keys[] = 0; | ||
|
||
if (\min($keys) === \max($keys)) { | ||
parent::add($node); | ||
|
||
continue; | ||
} | ||
|
||
\ksort($count); | ||
|
||
$child = \array_shift($count); | ||
$child->add($node); | ||
} | ||
|
||
return $this; | ||
} | ||
} |