Download the PHP package calabrothers/php-ds-tree without Composer

On this page you can find all versions of the php package calabrothers/php-ds-tree. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-ds-tree

PHP Tree Support

Build Status Coverage Status Total Downloads License

A class to support bitmask operations.

Install

composer require calabrothers/php-ds-tree

Test

composer install
composer test

Tree Library

Referring to the example tree:

We can introduce the following relevant information to discuss library functionality:

Set Value
Root NodeA
Leaves{H, I, E, F, G}

Relationship with other nodes:

Set Value
Children(A){ B , C }
Parent(B)A
Siblings(D){ E, F, G }
Descendants(B){ D, E, H, I }
Anchestors(H){ D, B, A }

For each of this set, an additional set is defined including the argument itself, hence:

Set Value
ChildrenAndSelf(A){ A, B , C }
SiblingsAndSelf(D){ D, E, F, G }
DescendantsAndSelf(B){ B, D, E, H, I }
AnchestorsAndSelf(H){ D, B, A }

Static functions:

As Ds\Deque object, the Tree library provides access to the following operations:

These basic operation are provided as static functions and they operate with a given set of nodes. A magic function provide access to all the previously defined sets.

Example: Let us calculate

    y = 2 x sum( Descendants(A) )

First we need to get a set of nodes given as Descendants(A), then apply a map to double the value. Finally we should sum the elements of the resulting vector.

// Let build some tree...
/*
        A(1)
        /    \    
    B(2)   C(3)
            /   \
            D(4)  E(5)  

*/
$oA = new TreeNode(1);
$oB = new TreeNode(2);
$oC = new TreeNode(3);
$oD = new TreeNode(4);
$oE = new TreeNode(5);

Connect the nodes to form the tree:

// Connects the nodes
$oC->attachChild($oD);
$oC->attachChild($oE);
$oA->attachChild($oB);
$oA->attachChild($oC);

Compute the result:

// Compute 2 x Value, forall the Descendants(A)
$aValue = TreeNode::mapSet(
    $oA->getDescendants(), 
    function ($oValue) {
        return 2 * $oValue;
    }
);
echo $aValue->sum()."\n"; // 28

Referring to the same tree, we can for example get the list of nodes having even value as:

// Get nodes having even value
$aEven = TreeNode::filterSet(
    $oA->getNodes(),
    function ($oValue) {
        return $oValue % 2 == 0;
    }
);

echo "(".$aEven[0]->getValue().",".$aEven[1]->getValue().")"; // (2,4)

Tree Builder

Library provides a convenient class to help construction of trees.

The builder requires a callable, responsible to build new nodes. For example considering a tree of integer values:

$oTreeC = new TreeBuilder(
            function (int $nNumber) : int { 
                return $nNumber; 
            }
        );

With this information, everytime the function begin() is called, a new node is added, forwarding the parameters of the begin() function to the callable function specified in the TreeBuilder constructor.

$oTreeC
    ->begin(1)
        ->begin(2)
        ->end()
        ->begin(3)
            ->begin(4)
            ->end()
            ->begin(5)
            ->end()
        ->end()
    ->end();

It is possible for all the types with a proper __toString() method to see the node values. For instance, in this case:

echo $oTree;
// Result:
┌1
└────2
└────3
    └────4
    └────5

The TreeBuilder is interesting when combined with custom node class, for example, let us consider to build a Tree having as nodes the object of following class:

class TreeNodeExample {
    public $nX;
    public $szY;
    public function __construct(int $nX, string $szY) {
        $this->nX   = $nX;
        $this->szY  = $szY;
    }

    public function myMultiply(int $nZ) {
        $this->nX *= $nZ;
        $this->szY = implode("+", array_fill(0, $nZ,$this->szY));
    }

    public function __toString():string {
        return "($this->nX|$this->szY)";
    }
}

Then we can use a TreeBuilder as:

$oTreeC = new TreeBuilder(
    function (int $nX, string $szY) : TreeNodeExample { 
        return new TreeNodeExample($nX, $szY); 
    }
);

$oTreeC
    ->begin(1, 'one')
        ->begin(2, 'two')
        ->end()
        ->begin(3, 'three')
            ->begin(4, 'four')
                ->myMultiply(2)  // This will call the method of TreeNodeExample! :)
            ->end()
            ->begin(5, 'five')
                ->myMultiply(3)
            ->end()
        ->end()
    ->end();

echo $oTreeC;

┌(1|one)
└────(2|two)
└────(3|three)
    └────(8|four+four)
    └────(15|five+five+five)

Notes

I strongly reccomend to use PHP Ds extension rather than its equivalent Php version. Check https://github.com/php-ds/ext-ds for more information.

Credits

Support quality code

Foo

License

The MIT License (MIT). Please see LICENSE for more information.


All versions of php-ds-tree with dependencies

PHP Build Version
Package Version
Requires php-ds/php-ds Version dev-master
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package calabrothers/php-ds-tree contains the following files

Loading the files please wait ....