BFS   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A travel() 0 15 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 BFS implements Traversal
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function travel(Node $node, $visitor)
22
    {
23
        $queue = new \SplQueue();
24
        $queue->enqueue($node);
25
26
        while (!$queue->isEmpty()) {
27
            $node = $queue->dequeue();
28
            $visitor($node);
29
30
            //压左子树
31
            if ($node->getLeft()) {
32
                $queue->push($node->getLeft());
33
            }
34
            if ($node->getRight()) {
35
                $queue->push($node->getRight());
36
            }
37
        }
38
    }
39
}