| @@ 6-22 (lines=17) @@ | ||
| 3 | use Mbh\Tree\Interfaces\Node as NodeInterface; |
|
| 4 | use Mbh\Tree\Visitor; |
|
| 5 | ||
| 6 | class InOrder extends Visitor |
|
| 7 | { |
|
| 8 | public function visit(NodeInterface $node) |
|
| 9 | { |
|
| 10 | if ($node->isLeaf()) { |
|
| 11 | return [$node]; |
|
| 12 | } |
|
| 13 | ||
| 14 | $yield = []; |
|
| 15 | ||
| 16 | foreach ($node->getChildren() as $child) { |
|
| 17 | $yield = array_merge($yield, $child->accept($this)); |
|
| 18 | } |
|
| 19 | ||
| 20 | return $yield; |
|
| 21 | } |
|
| 22 | } |
|
| 23 | ||
| @@ 6-22 (lines=17) @@ | ||
| 3 | use Mbh\Tree\Interfaces\Node as NodeInterface; |
|
| 4 | use Mbh\Tree\Visitor; |
|
| 5 | ||
| 6 | class PostOrder extends Visitor |
|
| 7 | { |
|
| 8 | public function visit(NodeInterface $node) |
|
| 9 | { |
|
| 10 | $nodes = []; |
|
| 11 | foreach ($node->getChildren() as $child) { |
|
| 12 | $nodes = array_merge( |
|
| 13 | $nodes, |
|
| 14 | $child->accept($this) |
|
| 15 | ); |
|
| 16 | } |
|
| 17 | ||
| 18 | $nodes[] = $node; |
|
| 19 | ||
| 20 | return $nodes; |
|
| 21 | } |
|
| 22 | } |
|
| 23 | ||
| @@ 6-21 (lines=16) @@ | ||
| 3 | use Mbh\Tree\Interfaces\Node as NodeInterface; |
|
| 4 | use Mbh\Tree\Visitor; |
|
| 5 | ||
| 6 | class PreOrder extends Visitor |
|
| 7 | { |
|
| 8 | public function visit(NodeInterface $node) |
|
| 9 | { |
|
| 10 | $nodes = [$node]; |
|
| 11 | ||
| 12 | foreach ($node->getChildren() as $child) { |
|
| 13 | $nodes = array_merge( |
|
| 14 | $nodes, |
|
| 15 | $child->accept($this) |
|
| 16 | ); |
|
| 17 | } |
|
| 18 | ||
| 19 | return $nodes; |
|
| 20 | } |
|
| 21 | } |
|
| 22 | ||