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