PHPackages                             matejlauko/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. matejlauko/tree

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

matejlauko/tree
===============

A basic but flexible php tree data structure and a fluent tree builder implementation. Fork nicmart/Tree

v0.2.5(11y ago)0466MITPHPPHP &gt;=5.4

Since Apr 22Pushed 11y ago2 watchersCompare

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

READMEChangelogDependenciesVersions (10)Used By (0)

Tree [![Build Status](https://camo.githubusercontent.com/e745dff7807119fa740a06eca95f98cfbcb67c0160857dbf9c314e77a03ac76d/68747470733a2f2f7472617669732d63692e6f72672f6d6174656a6c61756b6f2f547265652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/matejlauko/Tree)
=================================================================================================================================================================================================================================================================================

[](#tree-)

This is a fork of nicmart/tree php tree structure. It is used for personal projects.

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.

This package adds a Tree container which encapsulates the Nodes.

The tree data structure
-----------------------

[](#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

[](#creating-a-node)

```
use Tree\Node\Node;

$node = new Node('foo');
```

### Getting and setting the value of a node

[](#getting-and-setting-the-value-of-a-node)

Each node has a value property, that can be any php value.

```
$node->setValue('my value');
echo $node->getValue(); //Prints 'my value'
```

### Adding one or more children

[](#adding-one-or-more-children)

```
$child1 = new Node('child1');
$child2 = new Node('child2');

$node
    ->addChild($child1)
    ->addChild($child2);
```

### Removing a child

[](#removing-a-child)

```
$node->removeChild($child1);
```

### Getting the array of all children

[](#getting-the-array-of-all-children)

```
$children = $node->getChildren();
```

### Overwriting the children set

[](#overwriting-the-children-set)

```
$node->setChildren([new Node('foo'), new Node('bar')]);
```

### Removing all children

[](#removing-all-children)

```
$node->removeAllChildren();
```

### Getting if the node is a leaf or not

[](#getting-if-the-node-is-a-leaf-or-not)

A leaf is a node with no children.

```
$node->isLeaf();
```

### Getting if the node is a child or not

[](#getting-if-the-node-is-a-child-or-not)

A child is a node that has a parent.

```
$node->isChild();
```

### Getting the parent of a node

[](#getting-the-parent-of-a-node)

Reference to the parent node is automatically managed by child-modifiers methods

```
$root->addChild($node = new Node('child'));
$node->getParent(); // Returns $root
```

### Getting the ancestors of a node

[](#getting-the-ancestors-of-a-node)

```
$root = (new Node('root'))
    ->addChild($child = new Node('child'))
    ->addChild($grandChild = new Node('grandchild'))
;

$grandchild->getAncestors(); // Returns [$root, $child]
```

#### Related Methods

[](#related-methods)

- `getAncestorsAndSelf` retrieves ancestors of a node with the current node included.

### Getting the root of a node

[](#getting-the-root-of-a-node)

```
$root = $node->root();
```

### Getting the neighbors of a node

[](#getting-the-neighbors-of-a-node)

```
$root = (new Node('root'))
    ->addChild($child1 = new Node('child1'))
    ->addChild($child2 = new Node('child2'))
    ->addChild($child3 = new Node('child3'))
;

$child2->getNeighbors(); // Returns [$child1, $child3]
```

#### Related Methods

[](#related-methods-1)

- `getNeighborsAndSelf` retrieves neighbors of current node and the node itself.

### Getting the depth of a node

[](#getting-the-depth-of-a-node)

```
$node->getDepth();
```

### Getting the height of a node

[](#getting-the-height-of-a-node)

```
$node->getHeight();
```

The Builder
-----------

[](#the-builder)

The builder provides a convenient way to build trees. It is provided by the `Builder` class, but you can implement your own builder making an implementation of the `BuilderInterface`class.

### Example

[](#example)

Let's see how to build the following tree, where the nodes label are represents nodes values:

```
       A
      / \
     B   C
        /|\
       D E F
      /|
     G H

```

And here is the code:

```
$builder = new Tree\Builder\NodeBuilder;

$builder
    ->value('A')
    ->leaf('B')
    ->tree('C')
        ->tree('D')
            ->leaf('G')
            ->leaf('H')
            ->end()
        ->leaf('E')
        ->leaf('F')
        ->end()
;

$nodeA = $builder->getNode();
```

The example should be self-explanatory, but here you are a brief description of the methods used above.

### Builder::value($value)

[](#buildervaluevalue)

Set the value of the current node to `$value`

### Builder::leaf($value)

[](#builderleafvalue)

Add to the current node a new child whose value is `$value`.

### Builder::tree($value)

[](#buildertreevalue)

Add to the current node a new child whose value is `$value`, and set the new node as the builder current node.

### Builder::end()

[](#builderend)

Returns to the context the builder was before the call to `tree`method, i.e. make the builder go one level up.

### Builder::getNode()

[](#buildergetnode)

Returns the current node.

Yield of a tree
---------------

[](#yield-of-a-tree)

You can obtain the yield of a tree (i.e. the list of leaves in a preorder traversal) using the YieldVisitor. For example, if `$node` is the tree builded above, then

```
use Tree\Visitor\YieldVisitor;
$visitor = new YieldVisitor;

$yield = $node->accept($visitor);
// $yield will contain nodes B, G, H, E, F
```

Tests
=====

[](#tests)

```
phpunit

```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~82 days

Total

9

Last Release

4115d ago

PHP version history (2 changes)v0.1.0PHP &gt;=5.3.3

v0.2.0PHP &gt;=5.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/3d6097780b806e65e43382044ceb1426f3ce297a5ea9c72e3729959af16648f6?d=identicon)[matejlauko](/maintainers/matejlauko)

---

Top Contributors

[![nicmart](https://avatars.githubusercontent.com/u/443063?v=4)](https://github.com/nicmart "nicmart (35 commits)")[![jdeniau](https://avatars.githubusercontent.com/u/1398469?v=4)](https://github.com/jdeniau "jdeniau (2 commits)")

### Embed Badge

![Health badge](/badges/matejlauko-tree/health.svg)

```
[![Health](https://phpackages.com/badges/matejlauko-tree/health.svg)](https://phpackages.com/packages/matejlauko-tree)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
