Download the PHP package nicmart/tree without Composer
On this page you can find all versions of the php package nicmart/tree. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package tree
Short Description A basic but flexible php tree data structure and a fluent tree builder implementation.
License MIT
Informations about the package tree
Tree
In Tree you can find a basic but flexible tree data structure for php together with and an handful Builder class, that enables you to build tree in a fluent way.
The tree data structure
The Tree\Node\NodeInterface
interface abstracts the concept of a tree node. In Tree
a Node has essentially two things:
a set of children (that implements the same NodeInterface
interface) and a value.
On the other hand, the Tree\Node\Node
gives a straight implementation for that interface.
Creating a node
Getting and setting the value of a node
Each node has a value property, that can be any php value.
Adding one or more children
Removing a child
Getting the array of all children
Overwriting the children set
Removing all children
Getting if the node is a leaf or not
A leaf is a node with no children.
Getting if the node is a child or not
A child is a node that has a parent.
Getting the parent of a node
Reference to the parent node is automatically managed by child-modifiers methods
Getting the ancestors of a node
Related Methods
getAncestorsAndSelf
retrieves ancestors of a node with the current node included.
Getting the root of a node
Getting the neighbors of a node
Related Methods
getNeighborsAndSelf
retrieves neighbors of current node and the node itself.
Getting the number of nodes in the tree
Getting the depth of a node
Getting the height of a node
The Builder
The builder provides a convenient way to build trees. It is provided by the class, but you can implement your own builder making an implementation of the class.
Example
Let's see how to build the following tree, where the nodes label are represents nodes values:
And here is the code:
The example should be self-explanatory, but here you are a brief description of the methods used above.
Builder::value($value)
Set the value of the current node to
Builder::leaf($value)
Add to the current node a new child whose value is .
Builder::tree($value)
Add to the current node a new child whose value is , and set the new node as the builder current node.
Builder::end()
Returns to the context the builder was before the call to method, i.e. make the builder go one level up.
Builder::getNode()
Returns the current node.
Traversing a tree
Yield
You can obtain the yield of a tree (i.e. the list of leaves in a pre-order traversal) using the YieldVisitor.
For example, if $node
is the tree built above, then
Pre-order Traversal
You can walk a tree in pre-order:
Post-order Traversal
You can walk a tree in post-order:
Install
Run
Tests
Changelog
Please have a look at CHANGELOG.md
.
Contributing
Please have a look at CONTRIBUTING.md
.
License
This package is licensed using the MIT License.
Please have a look at LICENSE.md
.