Get support for loophp/phptree
If you're new to LTH, please see our FAQ for more information on what it is we do.
Support Options
Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:
View Open IssuesTake a look to see if anyone else has experienced the same issue as you and if they managed to solve it.
Open an IssueMake sure to read any relevant guidelines for opening issues on this repo before posting a new issue.
Sponsor directlyCheck out the page and see if there are any options to sponsor this project or it's developers directly.
loophp/phptree
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.
- 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.
Our Mission
We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.
From the Blog
Interesting Articles
-
Generating income from open source
Jun 23 • 8 min read
-
2023 State of OSS
Apr 23 • 45 min read ★
-
A funding experiment...
Aug 19 • 10 min read
-
But You Said I could
Aug 19 • 2 min read
Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated
p-e622a1a2