2017 © Pedro PelĂĄez
 

library tree

A basic but flexible php tree data structure and a fluent tree builder implementation. Fork nicmart/Tree

image

matejlauko/tree

A basic but flexible php tree data structure and a fluent tree builder implementation. Fork nicmart/Tree

  • Tuesday, April 7, 2015
  • by matejlauko
  • Repository
  • 2 Watchers
  • 1 Stars
  • 451 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 47 Forks
  • 0 Open issues
  • 10 Versions
  • 1 % Grown

The README.md

Tree Build Status

This is a fork of nicmart/tree php tree structure. It is used for personal projects., (*1)

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., (*2)

This package adds a Tree container which encapsulates the Nodes., (*3)

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., (*4)

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

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., (*6)

$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., (*7)

$node->isLeaf();

Getting if the node is a child or not

A child is a node that has a parent., (*8)

$node->isChild();

Getting the parent of a node

Reference to the parent node is automatically managed by child-modifiers methods, (*9)

$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]
  • 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]
  • getNeighborsAndSelf retrieves neighbors of current node and the node itself.

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., (*10)

Example

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

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

And here is the code:, (*12)

$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., (*13)

Builder::value($value)

Set the value of the current node to $value, (*14)

Builder::leaf($value)

Add to the current node a new child whose value is $value., (*15)

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., (*16)

Builder::end()

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

Builder::getNode()

Returns the current node., (*18)

Yield of a tree

You can obtain the yield of a tree (i.e. the list of leaves in a preorder traversal) using the YieldVisitor. For example, if $node is the tree builded above, then ```php use Tree\Visitor\YieldVisitor; $visitor = new YieldVisitor;, (*19)

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

Tests

phpunit

The Versions

07/04 2015

dev-master

9999999-dev https://github.com/matejlauko/Tree

A basic but flexible php tree data structure and a fluent tree builder implementation. Fork nicmart/Tree

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Matej Lauko

10/02 2015

v0.2.5

0.2.5.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4

 

16/06 2014

v0.2.4

0.2.4.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4

 

21/04 2014

v0.2.3

0.2.3.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4

 

18/04 2014

v0.2.2

0.2.2.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4

 

05/03 2014

v0.2.1

0.2.1.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4

 

05/03 2014

v0.2.0

0.2.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.4

 

02/03 2014

v0.1.2

0.1.2.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

01/03 2014

v0.1.1

0.1.1.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

22/04 2013

v0.1.0

0.1.0.0

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

  Sources   Download

MIT

The Requires

  • php >=5.3.3