BFS::travel()   A
last analyzed

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 5
nop 2
dl 0
loc 15
rs 9.9666
c 0
b 0
f 0
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
}