DFS   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 21
rs 10
c 0
b 0
f 0
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A travel() 0 16 4
1
<?php
2
3
/*
4
 * This file is part of the slince/tree-samples package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slince\Tree\Traversal;
13
14
use Slince\Tree\Node;
15
16
class DFS implements Traversal
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function travel(Node $node, $visitor)
22
    {
23
        $stack = new \SplStack();
24
        $stack->push($node);
25
26
        while (!$stack->isEmpty()) {
27
            $stack->top();
28
            $node = $stack->pop();
29
            $visitor($node);
30
31
            //压右子树
32
            if ($node->getRight()) {
33
                $stack->push($node->getRight());
34
            }
35
            if ($node->getLeft()) {
36
                $stack->push($node->getLeft());
37
            }
38
        }
39
    }
40
}