loophp / phptree

An implementation of tree data structure
MIT License
93 stars 5 forks source link
binary-tree data-structure tree

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:

4 trees traversal algorithms:

Exporters and importers:

Modifier:

Documentation

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

Changelog

See CHANGELOG.MD

Requirements

Installation

composer require loophp/phptree

Optional packages

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.