Skip to content

Commit

Permalink
Add an Auto Balanced Node.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 13, 2019
1 parent eb0e182 commit eb4efa8
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
69 changes: 69 additions & 0 deletions spec/drupol/phptree/Node/ABNodeSpec.php
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);
}
}
49 changes: 49 additions & 0 deletions src/Node/ABNode.php
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;
}
}

0 comments on commit eb4efa8

Please sign in to comment.