An implementation of tree data structure

Fund package maintenance!

2.6.3 2020-03-11 12:27 UTC


Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Code Coverage Type Coverage Mutation testing badge License Donate!



A PHP implementation of tree data structure.

It provides different trees implementations:

  • Node: The base class.
  • N-ary node: (or K-ary tree) extends the base class and allows you to specify the capacity of a node, the maximum children a node can have.
  • Value node: extends the N-ary node and allows you to attach a value to the node.
  • KeyValue node: extends the Value node and allows you to attach a key and a value to the node.
  • Trie node: extends the KeyValue node, a simple Trie tree.
  • Auto-balanced node: extends the N-ary node and tries to keep the tree as symetric as possible. It automatically balance all the children as soon as they are added.
  • Merkle node: a tree in which every non-leaf node is labelled with the cryptographic hash of the labels of its child nodes.

4 trees traversal algorithms:

  • In order
  • Post order
  • Pre order
  • Breadth first

Exporters and importers:

  • Ascii: Export a tree into an ascii graphic, just for swag and visualisation fun.
  • Graph: Export a tree into a Graph using the graphp/graphp library.
  • GraphViz: Export a tree into a script in GraphViz format.
  • Text: Export a tree into a simple string, easy for storing in a database.
  • nikic/php-ast: Import a tree from an AST generated by the PHP extension AST.
  • nikic/php-parser: Import a tree from an AST generated by nikic/php-parser.
  • microsoft/tolerant-php-parser: Import a tree from an AST generated by microsoft/tolerant-php-parser.


  • Reverse: To reverse a tree, all the children are mirrored.


Blog post: https://not-a-number.io/2018/phptree-a-fast-tree-implementation


  • PHP >= 7.1


composer require loophp/phptree

Optional packages

  • drupol/launcher: To automatically open a resource using the proper application on your operating system.
  • graphp/graphp: To export a tree into a Graph.



declare(strict_types = 1);

use loophp\phptree\Exporter\Gv;
use loophp\phptree\Exporter\Image;
use loophp\phptree\Node\ValueNode;
use loophp\phptree\Exporter\Text;
use loophp\launcher\Launcher;

include './vendor/autoload.php';

// Create the root node.
$tree = new ValueNode('root', 2);

$nodes = [];
foreach (\range('A', 'Z') as $v) {
    $nodes[] = new ValueNode($v, 2);

// Add children to the root node.

// Export to text.
$textExporter = new Text();
$textExporter->export($tree); // [root[A[C[G[O][P]][H[Q][R]]][D[I[S][T]][J[U][V]]]][B[E[K[W][X]][L[Y][Z]]][F[M][N]]]]⏎

// Export to a GraphViz script with the Gv exporter
$graphvizExporter = new Gv();
$dotScript = $graphvizExporter->export($tree);
file_put_contents('tree.dot', $dotScript);
// Then do "dot -Tsvg tree.dot -o tree.svg" to export in SVG.

// Or use the Image exporter that does it for you.
$imageExporter = new Image();
$imageContent = $imageExporter->setFormat('png')->export($tree);
file_put_contents('tree.png', $imageContent);

// If you want to launch the image, you can use an optional package.
// do: composer require drupol/launcher
// then:

Code quality, tests and benchmarks

Every time changes are introduced into the library, Github run the tests and the benchmarks.

The library has tests written with PHPSpec. Feel free to check them out in the spec directory. Run composer phpspec to trigger the tests.

Before each commit some inspections are executed with GrumPHP, run ./vendor/bin/grumphp run to check manually.

PHPBench is used to benchmark the library, to run the benchmarks: composer bench

PHPInfection is used to ensure that your code is properly tested, run composer infection to test your code.


Feel free to contribute to this library by sending Github pull requests. I'm quite reactive :-)