Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
SearchDepthFirstPostorder | |
100.00% |
21 / 21 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
rewind | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getMovementDirection | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
8 |
1 | <?php |
2 | |
3 | /** |
4 | * @author: Doug Wilbourne (dougwilbourne@gmail.com) |
5 | */ |
6 | declare(strict_types=1); |
7 | |
8 | namespace pvc\struct\treesearch; |
9 | |
10 | use pvc\interfaces\struct\treesearch\NodeVisitableInterface; |
11 | use pvc\interfaces\struct\treesearch\VisitStatus; |
12 | |
13 | /** |
14 | * Class SearchStrategyDepthFirstPostorder |
15 | * @template NodeType of NodeVisitableInterface |
16 | * @extends SearchDepthFirst<NodeType> |
17 | */ |
18 | class SearchDepthFirstPostorder extends SearchDepthFirst |
19 | { |
20 | public function rewind(): void |
21 | { |
22 | parent::rewind(); |
23 | /** |
24 | * The rewind method of the parent sets the current node to be the start node. This is correct for |
25 | * breadth first and depth first preorder searches. But for depth-first-postorder searches, we want to recurse |
26 | * to the bottom of the tree so that the first node returned is at the bottom of the tree. |
27 | */ |
28 | $this->next(); |
29 | } |
30 | |
31 | /** |
32 | * getMovementDirection |
33 | * @return Direction |
34 | * |
35 | * returns MOVE_DOWN if we should keep iterating by recursing down through child nodes, |
36 | * returns STOP if we should stop iterating |
37 | * returns MOVE_UP if we should continue iterating by returning up to the parent |
38 | * |
39 | * in post order mode, we go from never visited to partially visited and keep moving down if we can move down. |
40 | * if we cannot, we go to fully visited and stop |
41 | * |
42 | * in post order mode we go from partially visited to fully visited if all the children have been visited |
43 | * otherwise we move down. |
44 | * |
45 | * if a node is fully visited we always keep going up. |
46 | * |
47 | */ |
48 | protected function getMovementDirection(): Direction |
49 | { |
50 | assert(!is_null($this->current())); |
51 | |
52 | switch ($this->current()->getVisitStatus()) { |
53 | |
54 | case VisitStatus::NEVER_VISITED: |
55 | if ($this->allChildrenFullyVisited() || $this->atMaxLevels()) { |
56 | $this->current()->setVisitStatus(VisitStatus::FULLY_VISITED); |
57 | $direction = Direction::DONT_MOVE; |
58 | } |
59 | else { |
60 | $this->current()->setVisitStatus(VisitStatus::PARTIALLY_VISITED); |
61 | $direction = Direction::MOVE_DOWN; |
62 | } |
63 | break; |
64 | |
65 | case VisitStatus::PARTIALLY_VISITED: |
66 | if ($this->allChildrenFullyVisited() || $this->atMaxLevels()) { |
67 | $this->current()->setVisitStatus(VisitStatus::FULLY_VISITED); |
68 | $direction = Direction::DONT_MOVE; |
69 | } else { |
70 | $direction = Direction::MOVE_DOWN; |
71 | } |
72 | break; |
73 | |
74 | case VisitStatus::FULLY_VISITED: |
75 | $direction = Direction::MOVE_UP; |
76 | break; |
77 | } |
78 | |
79 | return $direction; |
80 | } |
81 | } |