Skip to content
/ phptree Public

An implementation of tree data structure

License

Notifications You must be signed in to change notification settings

loophp/phptree

Repository files navigation

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

PhpTree

Description

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.

Modifier:

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

Documentation

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

Changelog

See CHANGELOG.MD

Requirements

  • PHP >= 7.1

Installation

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.

Usage

<?php

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.
$tree->add(...$nodes);

// 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:
Launcher::open('tree.png');

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.

Contributing

Feel free to contribute by sending pull requests. We are a usually very responsive team and we will help you going through your pull request from the beginning to the end.

For some reasons, if you can't contribute to the code and willing to help, sponsoring is a good, sound and safe way to show us some gratitude for the hours we invested in this package.

Sponsor me on Github and/or any of the contributors.

Changelog

See CHANGELOG.md for a changelog based on git commits.

For more detailed changelogs, please check the release changelogs.