1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2019 Sebastian Böttger <[email protected]> |
5
|
|
|
* You may use, distribute and modify this code under the |
6
|
|
|
* terms of the MIT license. |
7
|
|
|
* |
8
|
|
|
* You should have received a copy of the MIT license with |
9
|
|
|
* this file. If not, please visit: https://opensource.org/licenses/mit-license.php |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Seboettg\Forest\General; |
13
|
|
|
|
14
|
|
|
use Seboettg\Collection\ArrayList; |
15
|
|
|
use Seboettg\Collection\ArrayList\ArrayListInterface; |
16
|
|
|
use Seboettg\Forest\Visitor\InOrderVisitor; |
17
|
|
|
use Seboettg\Forest\Visitor\LevelOrderVisitor; |
18
|
|
|
use Seboettg\Forest\Visitor\PostOrderVisitor; |
19
|
|
|
use Seboettg\Forest\Visitor\PreOrderVisitor; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Trait TreeTraversalTrait |
23
|
|
|
* @package Seboettg\Forest\General |
24
|
|
|
* @property TreeNodeInterface $root |
25
|
|
|
*/ |
26
|
|
|
trait TreeTraversalTrait |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param int $orderStrategy |
31
|
|
|
* @return ArrayListInterface |
32
|
|
|
*/ |
33
|
30 |
|
public function toArrayList(int $orderStrategy = TreeTraversalInterface::TRAVERSE_PRE_ORDER): ArrayListInterface |
34
|
|
|
{ |
35
|
30 |
|
$result = new ArrayList(); |
36
|
|
|
switch ($orderStrategy) { |
37
|
30 |
|
case self::TRAVERSE_IN_ORDER: |
|
|
|
|
38
|
17 |
|
$result = $this->root->accept(new InOrderVisitor()); |
39
|
16 |
|
break; |
40
|
13 |
|
case self::TRAVERSE_PRE_ORDER: |
|
|
|
|
41
|
8 |
|
$result = $this->root->accept(new PreOrderVisitor()); |
42
|
8 |
|
break; |
43
|
5 |
|
case self::TRAVERSE_POST_ORDER: |
|
|
|
|
44
|
2 |
|
$result = $this->root->accept(new PostOrderVisitor()); |
45
|
2 |
|
break; |
46
|
3 |
|
case self::TRAVERSE_LEVEL_ORDER: |
|
|
|
|
47
|
3 |
|
$result = $this->root->accept(new LevelOrderVisitor()); |
48
|
|
|
} |
49
|
29 |
|
return $result; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|