• Stars
    star
    546
  • Rank 81,396 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A basic but flexible tree data structure for php and a fluent tree builder implementation.

Tree

Integrate Release

Code Coverage Type Coverage

Latest Stable Version Total Downloads Monthly Downloads

In Tree you can find a basic but flexible tree data structure for php together with and an handful Builder class, that enables you to build tree in a fluent way.

The tree data structure

The Tree\Node\NodeInterface interface abstracts the concept of a tree node. In Tree a Node has essentially two things: a set of children (that implements the same NodeInterface interface) and a value.

On the other hand, the Tree\Node\Node gives a straight implementation for that interface.

Creating a node

use Tree\Node\Node;

$node = new Node('foo');

Getting and setting the value of a node

Each node has a value property, that can be any php value.

$node->setValue('my value');
echo $node->getValue(); //Prints 'my value'

Adding one or more children

$child1 = new Node('child1');
$child2 = new Node('child2');

$node
    ->addChild($child1)
    ->addChild($child2);

Removing a child

$node->removeChild($child1);

Getting the array of all children

$children = $node->getChildren();

Overwriting the children set

$node->setChildren([new Node('foo'), new Node('bar')]);

Removing all children

$node->removeAllChildren();

Getting if the node is a leaf or not

A leaf is a node with no children.

$node->isLeaf();

Getting if the node is a child or not

A child is a node that has a parent.

$node->isChild();

Getting the parent of a node

Reference to the parent node is automatically managed by child-modifiers methods

$root->addChild($node = new Node('child'));
$node->getParent(); // Returns $root

Getting the ancestors of a node

$root = (new Node('root'))
    ->addChild($child = new Node('child'))
    ->addChild($grandChild = new Node('grandchild'))
;

$grandchild->getAncestors(); // Returns [$root, $child]

Related Methods

  • getAncestorsAndSelf retrieves ancestors of a node with the current node included.

Getting the root of a node

$root = $node->root();

Getting the neighbors of a node

$root = (new Node('root'))
    ->addChild($child1 = new Node('child1'))
    ->addChild($child2 = new Node('child2'))
    ->addChild($child3 = new Node('child3'))
;

$child2->getNeighbors(); // Returns [$child1, $child3]

Related Methods

  • getNeighborsAndSelf retrieves neighbors of current node and the node itself.

Getting the number of nodes in the tree

$node->getSize();

Getting the depth of a node

$node->getDepth();

Getting the height of a node

$node->getHeight();

The Builder

The builder provides a convenient way to build trees. It is provided by the Builder class, but you can implement your own builder making an implementation of the BuilderInterfaceclass.

Example

Let's see how to build the following tree, where the nodes label are represents nodes values:

       A
      / \
     B   C
        /|\
       D E F
      /|
     G H

And here is the code:

$builder = new Tree\Builder\NodeBuilder;

$builder
    ->value('A')
    ->leaf('B')
    ->tree('C')
        ->tree('D')
            ->leaf('G')
            ->leaf('H')
            ->end()
        ->leaf('E')
        ->leaf('F')
        ->end()
;

$nodeA = $builder->getNode();

The example should be self-explanatory, but here you are a brief description of the methods used above.

Builder::value($value)

Set the value of the current node to $value

Builder::leaf($value)

Add to the current node a new child whose value is $value.

Builder::tree($value)

Add to the current node a new child whose value is $value, and set the new node as the builder current node.

Builder::end()

Returns to the context the builder was before the call to treemethod, i.e. make the builder go one level up.

Builder::getNode()

Returns the current node.

Traversing a tree

Yield

You can obtain the yield of a tree (i.e. the list of leaves in a pre-order traversal) using the YieldVisitor.

For example, if $node is the tree built above, then

use Tree\Visitor\YieldVisitor;

$visitor = new YieldVisitor;

$yield = $node->accept($visitor);
// $yield will contain nodes B, G, H, E, F

Pre-order Traversal

You can walk a tree in pre-order:

use Tree\Visitor\PreOrderVisitor;

$visitor = new PreOrderVisitor;

$yield = $node->accept($visitor);
// $yield will contain nodes A, B, C, D, G, H, E, F

Post-order Traversal

You can walk a tree in post-order:

use Tree\Visitor\PostOrderVisitor;

$visitor = new PostOrderVisitor;

$yield = $node->accept($visitor);
// $yield will contain nodes B, G, H, D, E, F, C, A

Install

Run

$ composer require nicmart/tree

Tests

phpunit

Changelog

Please have a look at CHANGELOG.md.

Contributing

Please have a look at CONTRIBUTING.md.

License

This package is licensed using the MIT License.

Please have a look at LICENSE.md.

More Repositories

1

StringTemplate

A death-simple string templating engine for php.
PHP
200
star
2

Numbers

Transform numbers in various formats, like scientific notation or unit-suffix notation
PHP
54
star
3

UniversalMatcher

A rule based matcher engine for php.
PHP
28
star
4

Arrayze

Callback-based array adapter for PHP
PHP
25
star
5

Functionals

A set of functionals for PHP
PHP
23
star
6

Building

A PHP library that abstracts the definition of fluent builders.
PHP
16
star
7

Evolution

Functional Generative Drawing
Scala
15
star
8

Benchmark

A framework for benchmarking php code
PHP
13
star
9

DGIM

A PHP implementation of the DGIM algorithm to count a stream of ones in a window.
PHP
12
star
10

ExpressionTree

Build and dump ExpressionTrees
PHP
5
star
11

WeightedRandomDistribution

An implementation of the ALIAS method for Weighted Random Distributions
Scala
4
star
12

GetLocalization

Basic GetLocalization client for php
PHP
4
star
13

PhpLibraryBoilerplate

A basic template I use every time I create a new php library
PHP
4
star
14

PhpGenerics

Tools for generic type programming in php
PHP
4
star
15

RandomPainter

Generative Art experiments in Scala
Scala
3
star
16

Phpy

An experimental libarary for php code inspection and generation
PHP
2
star
17

Demiurge

A library for object creation
PHP
2
star
18

CoversCarousel

A death simple, mobile friendly, covers carousel plugin for jQuery
JavaScript
2
star
19

Finga

Finga is a open source PHP library for fingerprinting php values.
PHP
2
star
20

Rulez

A blazing fast rules engine for PHP
PHP
2
star
21

SearchConfig

An example of DomainSpecificQuery compiler configuration
PHP
2
star
22

DomainSpecificQuery

Build and define queries specific to the application domain and compile them into other formats
PHP
2
star
23

Babylon-Crawler

Scala
1
star
24

Markov

Text shuffler
Scala
1
star
25

LiveTerminal

A simple tool that helps writing live data to the terminal
PHP
1
star
26

TreeBuilder

A library for tree transforming
PHP
1
star
27

HierarchicalClustering

Hierarchical Clustering Library for PHP
PHP
1
star
28

ChurchScala

Church type encodings in Scala
Scala
1
star
29

RandoXML

A PHP Library whose aim is to generate XML documents in a random way, following probabilistic rules defined in a configuration file
PHP
1
star