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