PHPackages                             drupol/phptree - 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. drupol/phptree

Abandoned → [loophp/phptree](/?search=loophp%2Fphptree)Library[Utility &amp; Helpers](/categories/utility)

drupol/phptree
==============

An implementation of tree data structure

2.6.8(4y ago)9950.8k5[1 issues](https://github.com/loophp/phptree/issues)[9 PRs](https://github.com/loophp/phptree/pulls)MITPHPPHP &gt;= 7.1CI failing

Since Dec 7Pushed 3w ago3 watchersCompare

[ Source](https://github.com/loophp/phptree)[ Packagist](https://packagist.org/packages/drupol/phptree)[ GitHub Sponsors](https://github.com/drupol)[ Fund](https://www.paypal.me/drupol)[ RSS](/packages/drupol-phptree/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (10)Dependencies (11)Versions (44)Used By (0)

[![Latest Stable Version](https://camo.githubusercontent.com/029be34e767166bd44f7d5737e44c346249eb58ef58c199dcd435da76df544d5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6f6f7068702f706870747265652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/phptree)[![GitHub stars](https://camo.githubusercontent.com/2ea2da95854e0c65e2af5ea82b01f4bc1e7ceea4d8802827687b332f2c3b6055/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f6c6f6f7068702f706870747265652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/phptree)[![Total Downloads](https://camo.githubusercontent.com/45727fb9e193f8ba16afb9d84bb49e4cd6636422928c656e69f08efda12e0e19/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6f6f7068702f706870747265652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/phptree)[![GitHub Workflow Status](https://camo.githubusercontent.com/2ff12f728ef61cf3dbcdfe5df497dcba12a71944dbf12d6a5897bc4e207e0d87/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c6f6f7068702f706870747265652f556e697425323074657374733f7374796c653d666c61742d737175617265)](https://github.com/loophp/phptree/actions)[![Scrutinizer code quality](https://camo.githubusercontent.com/9a27af25882be5df8e0162a13dd22584f2637dfe6e374308cfbd4c08064332a2/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f7175616c6974792f672f6c6f6f7068702f706870747265652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/loophp/phptree/?branch=master)[![Type Coverage](https://camo.githubusercontent.com/6749bdf51260a718afd28e777f1845bcd3d8c883ac6f601a4992852795ce4e40/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f64796e616d69632f6a736f6e3f7374796c653d666c61742d73717561726526636f6c6f723d636f6c6f72266c6162656c3d54797065253230636f7665726167652671756572793d6d6573736167652675726c3d687474707325334125324625324673686570686572642e6465762532466769746875622532466c6f6f70687025324670687074726565253246636f766572616765)](https://shepherd.dev/github/loophp/phptree)[![Code Coverage](https://camo.githubusercontent.com/486bf240168884bdff8441f2e9f59772ea0ea346344cb6dfb190e49b9bdb3f9a/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6c6f6f7068702f706870747265652f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/loophp/phptree/?branch=master)[![Mutation testing badge](https://camo.githubusercontent.com/afac9a148be5f745bf7d2df6960f8df2e3d924a0a97c7a744b39f0b1aa7c242f/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742d7371756172652675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d2532466c6f6f706870253246706870747265652532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/loophp/phptree/master)[![License](https://camo.githubusercontent.com/36e0281d7d6493406498d2185f32b934591a42fedbe9bf28b1bad6e86c7d8b7f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c6f6f7068702f706870747265652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/loophp/phptree)[![Donate!](https://camo.githubusercontent.com/a71f45de7e408be2477113d166e9ee94c90bbf814a1373fd154aa5b6652302de/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f53706f6e736f722d4769746875622d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](https://github.com/sponsors/drupol)

PhpTree
=======

[](#phptree)

Description
-----------

[](#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](https://en.wikipedia.org/wiki/Trie).
- **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](https://en.wikipedia.org/wiki/Tree_traversal):

- **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](https://github.com/graphp/graph) library.
- **GraphViz**: Export a tree into a script in [GraphViz](http://www.graphviz.org/) 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](https://github.com/nikic/php-ast).
- **nikic/php-parser**: Import a tree from an AST generated by [nikic/php-parser](https://github.com/nikic/php-parser).
- **microsoft/tolerant-php-parser**: Import a tree from an AST generated by [microsoft/tolerant-php-parser](https://github.com/microsoft/tolerant-php-parser).

Modifier:

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

Documentation
-------------

[](#documentation)

Blog post:

Changelog
---------

[](#changelog)

See [CHANGELOG.MD](https://github.com/loophp/phptree/blob/master/CHANGELOG.md)

Requirements
------------

[](#requirements)

- PHP &gt;= 7.1

Installation
------------

[](#installation)

`composer require loophp/phptree`

Optional packages
-----------------

[](#optional-packages)

- [drupol/launcher](https://github.com/drupol/launcher): To automatically open a resource using the proper application on your operating system.
- [graphp/graphp](https://github.com/graphp/graph): To export a tree into a Graph.

Usage
-----

[](#usage)

```
