The Mbh\Tree\Interfaces\Node
interface abstracts the concept of a tree node. In Tree
a Node has essentially two things: a set of children (that implements the same Node
interface) and a value.
On the other hand, the Mbh\Tree\Node
gives a straight implementation for that interface.
<?php
use Mbh\Tree\Node;
$node = new Node('foo');
Each node has a value property, that can be any php value.
<?php
$node->setValue('my value');
echo $node->getValue(); //Prints 'my value'
<?php
$child1 = new Node('child1');
$child2 = new Node('child2');
$node
->addChild($child1)
->addChild($child2);
<?php
$node->removeChild($child1);
<?php
$children = $node->getChildren();
<?php
$node->setChildren([new Node('foo'), new Node('bar')]);
<?php
$node->removeAllChildren();
A leaf is a node with no children.
<?php
$node->isLeaf();
A child is a node that has a parent.
<?php
$node->isChild();
Reference to the parent node is automatically managed by child-modifiers methods
<?php
$root->addChild($node = new Node('child'));
$node->getParent(); // Returns $root
<?php
$root = (new Node('root'))
->addChild($child = new Node('child'))
->addChild($grandChild = new Node('grandchild'));
$grandchild->getAncestors(); // Returns [$root, $child]
getAncestorsAndSelf
retrieves ancestors of a node with the current node included.
<?php
$root = $node->root();
<?php
$root = (new Node('root'))
->addChild($child1 = new Node('child1'))
->addChild($child2 = new Node('child2'))
->addChild($child3 = new Node('child3'));
$child2->getNeighbors(); // Returns [$child1, $child3]
getNeighborsAndSelf
retrieves neighbors of current node and the node itself.
<?php
$node->getSize();
<?php
$node->getDepth();
<?php
$node->getHeight();