The Tree
class in the Datatype package is a data structure that represents a tree, which is a collection of connected nodes.
Each node, or vertex, in the tree has zero or more child nodes, which are connected to it by edges.
You can use this class to define any tree structures.
Here you can see its API and see how to use it.
You can make a new instance of this class by passing a root to the constructor and then use the following methods:
$tree = new Tree('/');
assert_true($tree->root === '/');
assert_true($tree->vertices() instanceof Collection);
assert_true($tree->edges() instanceof Collection);
assert_true(['/'] === $tree->vertices()->items());
assert_true([] === $tree->edges()->items());
Here you can see a list of the available methods on the tree:
public function edge($vertex, $leaf): static
Adds an edge between the given vertex and leaf, adding the leaf to the vertices collection.
$tree = new Tree('/');
$tree->edge('/', 'home');
assert_true(['/', 'home'] === $tree->vertices()->items());
assert_true([new Pair('/', 'home')] == $tree->edges()->items());
public function edges(): Collection
Returns a collection of all edges in the tree.
$tree = new Tree('/');
$tree->edge('/', 'home');
assert_true([new Pair('/', 'home')] == $tree->edges()->items());
public function vertices(): Collection
Returns a collection of all vertices in the tree.
$tree = new Tree('/');
$tree->edge('/', 'home');
assert_true(['/', 'home'] === $tree->vertices()->items());