PHP Classes

PHP Tree Structure Class: Manipulate hierarchies of trees with data values

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 225 All time: 8,216 This week: 68Up
Version License PHP version Categories
php-tree-structure 1.2.0The PHP License5PHP 5, Data types
Description 

Author

This package can manipulate hierarchies of trees with data values.

It can create nodes of a balanced tree with left and right branches and perform other operations with those tree nodes like:

- Count the number of nodes in a tree
- Build a new tree from an existing root node
- Export the tree structure definition as a text string
- Etc.

Picture of zinsou A.A.E.Moïse
  Performance   Level  
Name: zinsou A.A.E.Moïse is available for providing paid consulting. Contact zinsou A.A.E.Moïse .
Classes: 50 packages by
Country: Benin Benin
Age: 34
All time rank: 6761 in Benin Benin
Week rank: 192 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

Recommendations

I want tree like Binary Tree but customisable
I want tree like Binary Tree but customisable in that I want to

Example

<?php
// require_once './src/btree.php';
// require_once './src/tree.php';
// require_once './src/node.php';
// require_once './src/nullnode.php';
// require_once './src/binarynode.php';

require_once('./Autoloader_src.php');
highlight_string('<?php
$btree= new Btree(new BinaryNode(4));
echo \'<pre>\';


$btree->getRoot()
->addChildren(array(\'left\'=>new notnullnode(\'son\')))
->getLeftChild()
->addChildren(
                array(
                \'left\'=>new notnullnode(\'grandson\'),
                \'right\'=>new notnullnode(\'c16\')
                )
            )
->getParent()
->replaceChild(new notnullnode(\'brotherofson\'),\'right\')
->getRightChild()
->addChildren(
            array(
                \'left\'=>new notnullnode(\'grandson1\'),
                \'right\'=>new notnullnode(\'c17\')
            )
)
->swapChildren()
->getParent()
->swapChildren()
->getLeftChild()
->getLeftChild()
->addChildren(
    array(
    \'left\'=>new notnullnode(\'doublegrandson\'),
    \'right\'=>new notnullnode(\'c18\')
    )
);


var_dump(count($btree));//return the number of true node
var_dump($btree->toArray());//return a representative array of a tree;


?>'
);
$btree= new Btree(new BinaryNode(4));
echo
'<pre>';


$btree->getRoot()
->
addChildren(array('left'=>new notnullnode('son')))
->
getLeftChild()
->
addChildren(
                array(
               
'left'=>new notnullnode('grandson'),
               
'right'=>new notnullnode('c16')
                )
            )
->
getParent()
->
replaceChild(new notnullnode('brotherofson'),'right')
->
getRightChild()
->
addChildren(
            array(
               
'left'=>new notnullnode('grandson1'),
               
'right'=>new notnullnode('c17')
            )
)
->
swapChildren()
->
getParent()
->
swapChildren()
->
getLeftChild()
->
getLeftChild()
->
addChildren(
    array(
   
'left'=>new notnullnode('doublegrandson'),
   
'right'=>new notnullnode('c18')
    )
);


var_dump(count($btree));//return the number of true node
var_dump($btree->toArray());//return a representative array of a tree;


highlight_string('<?php /*can use square bracket to progressively access the tree like an array*/
print_r($btree->getRoot()[\'left\'][\'lEft\'][\'rigHt\']);
print_r($btree[\'top\'][\'left\'][\'lEft\'][\'left\']);
$btree->getRoot()[\'left\'][\'lEft\'][\'left\'][\'value\']=\'c19\';
echo $btree[\'top\'][\'left\'][\'lEft\'][\'left\'][\'parentId\'].\'<br>\';//same as:
echo $btree[\'root\'][\'left\'][\'lEft\'][\'left\'][\'dad\'].\'<br>\';
?>'
);
/**/

print_r($btree->getRoot()['left']['lEft']['rigHt']);
print_r($btree['top']['left']['lEft']['left']);
$btree->getRoot()['left']['lEft']['left']['value']='c19';
echo
$btree['top']['left']['lEft']['left']['parentId'].'<br>';
echo
$btree['root']['left']['lEft']['left']['dad'].'<br>';


highlight_string('<?php
print_r($btree->getRoot()->getChildren()[\'left\']->getChildren()[\'left\']->getGrandPa());//same as

print_r($btree->getRoot()->getGrandSons()[\'left\'][\'left\']->getGrandPa());

?>'
);


print_r($btree->getRoot()->getChildren()['left']->getChildren()['left']->getGrandPa());

print_r($btree->getRoot()->getGrandSons()['left']['left']->getGrandPa());

// var_dump(eval('return '.$btree->export(true).';'));
// var_dump(isset($btree->getRoot()['left']));
// unset($btree->getRoot()['left']);
// var_dump(isset($btree->getRoot()['left']));
highlight_string('<?php
var_dump(isset($btree->getRoot()[\'left\'][\'children\']));
var_dump($btree->getRoot()[\'left\'][\'left\'][\'uncle\'][\'brother\']);

?>'
);

var_dump(isset($btree->getRoot()['left']['children']));
var_dump($btree->getRoot()['left']['left']['uncle']['brother']);

highlight_string('<?php
$x=new Stemnode(\'tada\');
$x->addchild($y=new stemnode(1));
$x->addchild(new stemnode(1));
$x->addchild(new stemnode(1));
$y->addchild(new stemnode(1));
$y[0]->addchild($nt=new notnullnode(6));
var_dump($x[0][0][\'parentId\']);

var_dump(eval(\'return \'.$x->export(true).\';\'));

var_dump($t=new Tree($x));
$nt->addChildren(array(\'left\'=>new BinaryNode(\'fortestpurpose\')));
var_dump($t[\'top\'][0][0][0][\'left\']);

echo count($btree);
echo \'<br>\';
echo count($t);
var_dump($t[\'top\'][\'grandsons\']);
echo \'</pre>\';

foreach($t[\'top\'] as $child){
    echo $child.\'<br>\';
   
}
$t[\'top\']->rewind();
$t[\'top\']->next();
    echo \'<br>\';
        echo \'<br>\';
        echo \'<br>\';
   
var_dump($t[\'top\']->prev());

?>'
);


$x=new Stemnode('tada');
$x->addchild($y=new stemnode(1));
$x->addchild(new stemnode(1));
$x->addchild(new stemnode(1));
$y->addchild(new stemnode(1));
$y[0]->addchild($nt=new notnullnode(6));
var_dump($x[0][0]['parentId']);

var_dump(eval('return '.$x->export(true).';'));

var_dump($t=new Tree($x));
$nt->addChildren(array('left'=>new BinaryNode('fortestpurpose')));
var_dump($t['top'][0][0][0]['left']);

echo
count($btree);
echo
'<br>';
echo
count($t);
var_dump($t['top']['grandsons']);
echo
'</pre>';

foreach(
$t['top'] as $child){
    echo
$child.'<br>';
   
}
$t['top']->rewind();
$t['top']->next();
    echo
'<br>';
        echo
'<br>';
        echo
'<br>';
   
var_dump($t['top']->prev());
?>



Details

This package provides the mean to handle linked nodes of data as Binary tree. It is also possible to create tree where nodes can have more than two children. example using chaining methods on the tree to build it and make changes at the same time. $btree= new Btree(new notnullnode(4)); $btree->getRoot() ->addChildren(array('left'=>new notnullnode('son'))) ->getLeftChild() ->addChildren( array( 'left'=>new notnullnode('grandson'), 'right'=>new notnullnode('c16') ) ) ->getParent() ->replaceChild(new notnullnode('brotherofson'),'right') ->getRightChild() ->addChildren( array( 'left'=>new notnullnode('grandson1'), 'right'=>new notnullnode('c17') ) ) ->swapChildren() ->getParent() ->swapChildren() ->getLeftChild() ->getLeftChild() ->addChildren( array( 'left'=>new notnullnode('doublegrandson'), 'right'=>new notnullnode('c18') ) ); you can also use array access style on the tree: print_r($btree->getRoot()['left']['lEft']['rigHt']); print_r($btree['top']['left']['lEft']['left']); $btree->getRoot()['left']['lEft']['left']['value']='c19'; echo $btree['top']['left']['lEft']['left']['parentId'].'<br>'; echo $btree['root']['left']['lEft']['left']['dad'].'<br>'; you can use the array access style to get brother,uncle,dad,childrens etc... see the arrayAccess methods implemented in the different node classes. you can count the number of real node in the tree using: count($btree) or $btree->count(); NB: each time we use exists we mean instanceof notnullnode. Abstract node methods array ( 'export' => 'An wrapper for the var_export function but which avoid ugly notice about circular reference', 'getBrother' => 'return the brother node of the current', 'getGrandPa' => 'return the parent of the parent of the current node if exists', 'getId' => 'return the unique Id assigned to each node at its creation', 'getNephews' => 'return an array containing the children of the current node\'s brother ', 'getParent' => 'return the current node\'s parent', 'getType' => 'return the type(left or right) of child of the current node if it has a parent', 'getUncle' => 'return the brother of the parent of the current node if exists', 'getValue' => 'return the value of the node', 'hasDad' => 'return whether or not the current node has parent', 'hasGrandPa' => 'return whether or not the current node has parent which has itself a parent', 'hasNephews' => 'return whether or not the children of the current node\'s brother exist', 'hasParent' => 'see hasDad', 'hasRealBrother' =>'return whether or not the current node has a real brother', 'hasUncle' => 'return whether or not the current node has a real Uncle', 'makeOrphan' => 'unlink the current node from its dad if exists', 'setType' => 'switch the type of the current node', ) nullnode extends node array ( '__call' => 'implemented to avoid fat errors will always return the same node, the current nullnode', '__construct' => 'create a node with the value null which can never have children for Aesthetic and logical purpose', '__debugInfo' => 'return null', '__set_state' => 'import a null node exported with var_export()', '__toString' => 'return null', 'hasChildren' => 'return false every time', ) notnullnode extends node array ( '__call' => 'implemented to avoid fat errors will always return the same node, the current nullnode', '__construct' => 'create a real node which can have children and not null value', '__debugInfo' => 'return array containing value ,left and right child', '__set_state' => 'import a not null node exported with var_export()', '__toString' => 'return a serialized value of the node', 'addChildren' => 'add one or the two children to the node', 'getChildren' => 'return an array of the current node\'s childrens' , 'getChildrenNum' =>'return the number of true nodes', 'getGrandSons' =>'return an array containing the four grandsons', 'getLeftChild' =>'return the left child', 'getRightChild' =>'return the right child, 'hasChild' => 'return whether or not the current node has at least one not null node as child', 'hasChildren' => 'return whether or not the current node has two not null nodes as children', 'isDad' => 'see hasChild', 'offsetExists' =>'the purpose of ArrayAccess::offsetExists', 'offsetGet' =>'the purpose of ArrayAccess::offsetGet', 'offsetSet' => 'the purpose of ArrayAccess::offsetSet', 'offsetUnset' => 'the purpose of ArrayAccess::offsetUnset', 'removeChild' =>'replace a child by a nullnode if it isn't yet by specifying its type or randomly when called without argument', 'removeChildren' => 'replace the two children by two nullnode if there aren't yet', 'replaceChild' => 'replace a child by another node', 'setValue' => 'set the value of the node', 'swapChildren' => 'swap the children of the current node', ) Stemnode extends node array ( '__call' => 'implemented to avoid fat errors will always return the same node, the current stemnode', '__construct' => 'create a real node which can have more than two children and not null value', '__debugInfo' => 'return array containing value ,and children', '__set_state' => 'import a stem node exported with var_export()', '__toString' => 'return a serialized value of the node', 'addchild' =>'add one child to the node', 'current' => 'the purpose of Iterator::current ,return the current child', 'getBrothers' => 'return the brothers of the current node only one if the parent is a notnullnode and more if it is a stemnode', 'getChildren' => 'return the children', 'getChildrenNum' => 'return the number of true node', 'getGrandSons' => 'return an array containing all the grandsons', 'getNephews' => 'return an array containing all the nephews', 'getUncles' => 'return the uncles of the current node only one if the grandpa is a notnullnode and more if it is a stemnode', 'hasChild' => 'return whether or not the current node has at least one true node as child', 'hasRealBrother' => 'return whether or not the current node has a real brother', 'isDad' => 'see hasChild', 'key' => 'the purpose of Iterator::key,return the key/type of the current child', 'makeOrphan' => 'make the current stem node orphan', 'next' => 'the purpose of Iterator::next,return the next child and move the internal pointer', 'offsetExists' =>'the purpose of ArrayAccess::offsetExists', 'offsetGet' =>'the purpose of ArrayAccess::offsetGet', 'offsetSet' => 'the purpose of ArrayAccess::offsetSet', 'offsetUnset' => 'the purpose of ArrayAccess::offsetUnset', 'removeChild' => 'remove a child', 'removeChildren' => 'remove all children', 'replaceChild' => 'replace a child', 'rewind' => 'the purpose of Iterator::rewind,reset the internal pointer', 'setType' => 'switch the type of the current node', 'setValue' => 'set the value of the node', 'swapChildren' => 'swap the children of the current node', 'valid' => 'the purpose of Iterator::valid,return whether or not the current position is a valid position', ) Btree array ( '__construct' => 'create a new tree from an orphan notnullnode root', '__set_state' => 'import a Btree exported with var_export()', 'buildFrom' =>'static function which takes a notnullnode make it orphan and create a new tree from it', 'count' => 'return the number of all real nodes in the tree', 'export' => 'An wrapper for the var_export function but which avoid ugly notice about circular reference', 'getRoot' => 'return the top level node equals to use $node[\'root\'] or $node[\'top\']', 'getTree' => return the whole tree but not an btree object, 'offsetExists' =>'the purpose of ArrayAccess::offsetExists', 'offsetGet' =>'the purpose of ArrayAccess::offsetGet', 'offsetSet' => 'the purpose of ArrayAccess::offsetSet', 'offsetUnset' => 'the purpose of ArrayAccess::offsetUnset', 'prepare_count' => 'private static method which recursively count the number of real children node', 'prepare_toArray' => 'private static method which recursively build an exploitable array from the tree' 'toArray' =>'return an human readable and easily exploitable of the btree object', ) array ( '__construct' => 'create a new tree from an orphan notnullnode or stemnode root', '__set_state' => 'import a Tree exported with var_export()', 'buildFrom' =>'static function which takes a notnullnode or a stemnode make it orphan and create a new tree from it', 'count' => 'return the number of all real nodes in the tree', 'export' => 'An wrapper for the var_export function but which avoid ugly notice about circular reference', 'getRoot' => 'return the top level node equals to use $node[\'root\'] or $node[\'top\']', 'getTree' => return the whole tree but not an btree object, 'offsetExists' =>'the purpose of ArrayAccess::offsetExists', 'offsetGet' =>'the purpose of ArrayAccess::offsetGet', 'offsetSet' => 'the purpose of ArrayAccess::offsetSet', 'offsetUnset' => 'the purpose of ArrayAccess::offsetUnset', ) See the example file for a little how to use demonstration.For the rest you will just be limited by your imagination. keep in mind that notnullnode name has been kept for ascending compatibility but the name BinaryNode can be used to call the same class. Use the forum for bug reporting,suggestions and feedback and don't forget to rate the package.

  Files folder image Files (9)  
File Role Description
Files folder imagesrc (6 files)
Accessible without login Plain text file Autoloader_src.php Aux. autoloader script
Accessible without login Plain text file example.php Example example script
Accessible without login Plain text file readme.txt Doc. readme

  Files folder image Files (9)  /  src  
File Role Description
  Plain text file BinaryNode.php Class BinaryNode/notnullnode class source
  Plain text file Btree.php Class binary tree class source
  Plain text file node.php Class abstract node class source
  Plain text file Nullnode.php Class null node class source
  Plain text file StemNode.php Class StemNode class source
  Plain text file Tree.php Class Tree class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 0%
Total:225
This week:0
All time:8,216
This week:68Up