PHP Classes
elePHPant
Icontem

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

Recommend this page to a friend!
  Info   View files Example   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-06-30 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 133 This week: 2All time: 8,744 This week: 250Up
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.

Recommendations

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

  Performance   Level  
Name: zinsou A.A.E.Moïse <contact>
Classes: 45 packages by
Country: Benin Benin
Innovation award
Innovation award
Nominee: 17x

Winner: 2x

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  
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  /  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

 Version Control Unique User Downloads Download Rankings  
 0%
Total:133
This week:2
All time:8,744
This week:250Up