Skip to content

Commit 5ee6e1c

Browse files
committed
Update InOrder traverser.
1 parent 58c4ae3 commit 5ee6e1c

File tree

7 files changed

+66
-11
lines changed

7 files changed

+66
-11
lines changed

spec/drupol/phptree/Exporter/GraphSpec.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace spec\drupol\phptree\Exporter;
66

77
use drupol\phptree\Exporter\Graph;
8-
use drupol\phptree\Node\Node;
98
use drupol\phptree\Node\ValueNode;
109
use drupol\phptree\Traverser\BreadthFirst;
1110
use PhpSpec\ObjectBehavior;
@@ -20,9 +19,9 @@ public function it_is_initializable()
2019
public function it_can_generate_a_graph()
2120
{
2221
$root = new ValueNode('root');
23-
$child1 = new Node();
24-
$child2 = new Node();
25-
$child3 = new Node();
22+
$child1 = new ValueNode();
23+
$child2 = new ValueNode();
24+
$child3 = new ValueNode();
2625
$root
2726
->add($child1, $child2, $child3);
2827

spec/drupol/phptree/Exporter/SimpleArraySpec.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function it_can_export_to_an_array()
2525

2626
$nodes = [];
2727
foreach (\range('A', 'J') as $value) {
28-
$nodes[$value] = new ValueNode($value);
28+
$nodes[$value] = new ValueNode($value, 2);
2929
}
3030

3131
$tree->add(...\array_values($nodes));

spec/drupol/phptree/Exporter/TextSpec.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function it_can_export_to_text()
2525

2626
$nodes = [];
2727
foreach (\range('A', 'J') as $value) {
28-
$nodes[$value] = new ValueNode($value);
28+
$nodes[$value] = new ValueNode($value, 2);
2929
}
3030

3131
$tree->add(...\array_values($nodes));

spec/drupol/phptree/Node/NodeSpec.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public function it_can_get_its_depth()
237237

238238
$nodes = [];
239239
foreach (\range('A', 'Z') as $v) {
240-
$nodes[] = new \drupol\phptree\Node\ValueNode($v);
240+
$nodes[] = new \drupol\phptree\Node\ValueNode($v, 2);
241241
}
242242

243243
$tree->add(...$nodes);

spec/drupol/phptree/Traverser/InOrderSpec.php

+47-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function it_is_initializable()
1515
$this->shouldHaveType(InOrder::class);
1616
}
1717

18-
public function it_can_traverse_a_tree()
18+
public function it_can_traverse_a_tree_of_degree2()
1919
{
2020
$tree = new ValueNode('root', 2);
2121

@@ -39,4 +39,50 @@ public function it_can_traverse_a_tree()
3939
->traverse($tree)
4040
->shouldYield(new \ArrayIterator($nodes));
4141
}
42+
43+
public function it_can_traverse_a_tree_of_degree4()
44+
{
45+
$tree = new ValueNode('root', 4);
46+
47+
foreach (\range('A', 'Z') as $key => $value) {
48+
$nodes[$value] = new ValueNode($value, 4);
49+
}
50+
51+
$tree->add(...\array_values($nodes));
52+
53+
$nodes['root'] = $tree;
54+
$nodes = [
55+
$nodes['U'],
56+
$nodes['V'],
57+
$nodes['E'],
58+
$nodes['W'],
59+
$nodes['X'],
60+
$nodes['Y'],
61+
$nodes['F'],
62+
$nodes['Z'],
63+
$nodes['A'],
64+
$nodes['G'],
65+
$nodes['H'],
66+
$nodes['I'],
67+
$nodes['J'],
68+
$nodes['B'],
69+
$nodes['K'],
70+
$nodes['L'],
71+
$nodes['root'],
72+
$nodes['M'],
73+
$nodes['N'],
74+
$nodes['C'],
75+
$nodes['O'],
76+
$nodes['P'],
77+
$nodes['Q'],
78+
$nodes['R'],
79+
$nodes['D'],
80+
$nodes['S'],
81+
$nodes['T'],
82+
];
83+
84+
$this
85+
->traverse($tree)
86+
->shouldYield(new \ArrayIterator($nodes));
87+
}
4288
}

src/Node/NaryNode.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function capacity(): int
6161
public function add(NodeInterface ...$nodes): NodeInterface
6262
{
6363
foreach ($nodes as $node) {
64+
/** @var \drupol\phptree\Node\Node $parent */
6465
$parent = $this->findFirstAvailableNode();
6566
$parent->storage['children'][] = $node->setParent($parent);
6667
}
@@ -83,8 +84,14 @@ public function getTraverser()
8384
*/
8485
private function findFirstAvailableNode(): NodeInterface
8586
{
87+
$capacity = $this->capacity();
88+
8689
foreach ($this->getTraverser()->traverse($this) as $node) {
87-
if ($node->degree() >= $node->capacity()) {
90+
if (\method_exists($node, 'capacity')) {
91+
$capacity = $node->capacity();
92+
}
93+
94+
if ($node->degree() >= $capacity) {
8895
continue;
8996
}
9097

src/Traverser/InOrder.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ public function traverse(NodeInterface $node): \Traversable
3131
*/
3232
private function doTraverse(NodeInterface $node): \Traversable
3333
{
34-
foreach ($node->children() as $child) {
35-
if ($node->lastChild() === $child) {
34+
$countChildren = $node->degree();
35+
$middle = \floor($countChildren/2);
36+
37+
foreach ($node->children() as $key => $child) {
38+
if ((int) $key === (int) $middle) {
3639
yield $this->index => $node;
3740
$this->index++;
3841
}

0 commit comments

Comments
 (0)