Completed
Push — master ( ae7247...d37ef5 )
by Siro Díaz
02:14
created

TrieNode::isLeaf()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
1
<?php
2
/**
3
 * DataStructures for PHP
4
 *
5
 * @link      https://github.com/SiroDiaz/DataStructures
6
 * @copyright Copyright (c) 2017 Siro Díaz Palazón
7
 * @license   https://github.com/SiroDiaz/DataStructures/blob/master/README.md (MIT License)
8
 */
9
namespace DataStructures\Trees\Nodes;
10
11
use Countable;
12
13
/**
14
 * TrieNode.
15
 *
16
 * The TrieNode class represents the trie node. It uses an array to store all
17
 * children nodes.
18
 *
19
 * @author Siro Diaz Palazon <[email protected]>
20
 */
21
class TrieNode implements Countable {
22
    public $char;
23
    public $isWord;
24
    public $children;
25
    public $parent;
26
27
    public function __construct($char = '', &$parent = null, $isWord = false) {
28
        $this->char = $char;
29
        $this->isWord = $isWord;
30
        $this->children = [];
31
        $this->parent = &$parent;
32
    }
33
34
    public function hasChildren() : bool {
35
        return count($this->children) > 0;
36
    }
37
38
    public function isLeaf() : bool {
39
        return $this->parent !== null && $this->hasChildren() === 0;
40
    }
41
42
    public function isRoot() : bool {
43
        return $this->parent === null;
44
    }
45
46
47
    public function count() {
48
        return count($this->children);
49
    }
50
}