Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SearchDepthFirstPostorder
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 rewind
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getMovementDirection
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
72
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 * @template NodeIdType of array-key
15 * @template NodeType of NodeVisitableInterface
16 * @extends SearchDepthFirst<NodeIdType, NodeType>
17 */
18class 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     *
34     * @return Direction
35     *
36     * returns MOVE_DOWN if we should keep iterating by recursing down through child nodes,
37     * returns STOP if we should stop iterating
38     * returns MOVE_UP if we should continue iterating by returning up to the parent
39     *
40     * in post order mode, we go from never visited to partially visited and keep moving down if we can move down.
41     * if we cannot, we go to fully visited and stop
42     *
43     * in post order mode we go from partially visited to fully visited if all the children have been visited
44     * otherwise we move down.
45     *
46     * if a node is fully visited we always keep going up.
47     *
48     */
49    protected function getMovementDirection(): Direction
50    {
51        assert(!is_null($this->current()));
52
53        switch ($this->current()->getVisitStatus()) {
54            case VisitStatus::NEVER_VISITED:
55                if ($this->allChildrenFullyVisited() || $this->atMaxLevels()) {
56                    $this->current()->setVisitStatus(
57                        VisitStatus::FULLY_VISITED
58                    );
59                    $direction = Direction::DONT_MOVE;
60                } else {
61                    $this->current()->setVisitStatus(
62                        VisitStatus::PARTIALLY_VISITED
63                    );
64                    $direction = Direction::MOVE_DOWN;
65                }
66                break;
67
68            case VisitStatus::PARTIALLY_VISITED:
69                if ($this->allChildrenFullyVisited() || $this->atMaxLevels()) {
70                    $this->current()->setVisitStatus(
71                        VisitStatus::FULLY_VISITED
72                    );
73                    $direction = Direction::DONT_MOVE;
74                } else {
75                    $direction = Direction::MOVE_DOWN;
76                }
77                break;
78
79            case VisitStatus::FULLY_VISITED:
80                $direction = Direction::MOVE_UP;
81                break;
82        }
83
84        return $direction;
85    }
86}