dimasp / generictree
N-ary Tree
dev-master
2016-04-29 07:20 UTC
This package is not auto-updated.
Last update: 2025-05-02 23:11:47 UTC
README
N-ary tree with higher order function while traversing.
Requirements
- PHP 5.4.0 or later
Usage:
use DimasP\GenericTree as Tree;
require_once 'GenericTree.php';
$tree = new Tree\GenericTree();
$tree->insert(array (), "1");
$tree->insert(array (), "2");
$tree->insert(array (), "3");
$tree->insert(array (), "4");
$tree->insert(array (3), "4,1");
$tree->insert(array (3), "4,2");
$tree->insert(array (2), "3,1");
$tree->insert(array (2, 0), "3,1,1");
$preOp = function () {
echo "(";
};
$inOp = function ($arg) {
echo " $arg ";
};
$postOp = function () {
echo ")";
};
$tree->inOrder($preOp, $inOp, $postOp);
// prints ( 1 () 2 () 3 ( 3,1 ( 3,1,1 ())) 4 ( 4,1 () 4,2 ()))
Or alternatively you can remove this line in GenericTree.php
and Node.php
:
namespace DimasP\GenericTree;
and this line:
use Exception;
And load the library using require_once
.