PHPackages                             iwink/tree - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. iwink/tree

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

iwink/tree
==========

Tree data structure for PHP

v1.0.1(4y ago)349.7k↓38.4%[1 PRs](https://github.com/iwink/tree/pulls)MITPHPPHP ^7.4|^8.0CI passing

Since Jan 7Pushed 3mo ago4 watchersCompare

[ Source](https://github.com/iwink/tree)[ Packagist](https://packagist.org/packages/iwink/tree)[ RSS](/packages/iwink-tree/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (4)Used By (0)

iWink Tree
==========

[](#iwink-tree)

[![License](https://camo.githubusercontent.com/4c85dd75a2cb73ffe7db07cd89df728d9e5ebaabf659ce3e356fe10decb818a0/68747470733a2f2f706f7365722e707567782e6f72672f6977696e6b2f747265652f6c6963656e73652e706e67)](https://packagist.org/packages/iwink/tree)[![Tag](https://camo.githubusercontent.com/38d473f5a62e85bfe616128c72f4508cbcebbb8b79e357ff012991bdce6122b8/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f6977696e6b2f74726565)](https://github.com/iwink/tree/releases)

This component provides a [Tree](https://en.wikipedia.org/wiki/Tree_%28data_structure%29) that can be traversed and modified.

Tree
----

[](#tree)

The [`Tree`](src/TreeInterface.php) receives a root [`Node`](src/Node/NodeInterface.php) and contains methods to traverse the nodes contained in the root node:

- `visitPostOrder` Traverses its children first (left to right) and then itself.
- `visitInOrder` Traverses its left children first, then itself, and then its right children.
- `visitPreOrder` Traverses itself first, and then it's children (left to right).
- `visitLevelOrder` Traverses per level.

Visitors
--------

[](#visitors)

The tree implements the [Visitor pattern](https://en.wikipedia.org/wiki/Visitor_pattern). A number of common visitors are included in this component:

- [`ValueVisitor`](src/Visitor/ValueVisitor.php) returns an array containing the value of each node.
- [`SerializerVisitor`](src/Visitor/SerializerVisitor.php) returns a serialized representation of the tree.

There are also 2 abstract visitors that provide a base implementation:

- [`Visitor`](src/Visitor/Visitor.php) implements empty `beforeVisiting()` and `afterVisiting()` methods.
- [`ArrayVisitor`](src/Visitor/ArrayVisitor.php) keeps a record of the result for every visit that can be read by calling `getResult(): iterable` after the visitor is done.

Creating your own visitor
-------------------------

[](#creating-your-own-visitor)

To create your own visitor you need to let it implement `VisitorInterface` or extend the abstract `Visitor` or `ArrayVisitor` class.

Sometimes you need to perform some action before the visitor starts visiting or after it is done visiting. For these situations you can use `beforeVisiting()` and `afterVisiting()` methods. E.g. The `ArrayVisitor` always resets the internal array on `beforeVisiting()` to prevent result stacking on multiple calls.

Example
-------

[](#example)

### Building the tree

[](#building-the-tree)

```
