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 (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-05-30 (21 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 101 This week: 99All time: 8,945 This week: 2Up
Version License PHP version Categories
php-tree-structure 1.0.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 is available for providing paid consulting. Contact zinsou A.A.E.Mo´se .
Classes: 40 packages by
Country: Benin Benin
Age: 28
All time rank: 11221 in Benin Benin
Week rank: 1 Up
Innovation award
Innovation award
Nominee: 14x

Winner: 2x

Details
This package provides the mean to handle linked nodes of data as Binary tree.
(It may be possible to add tree where nodes can have more than two children in the future) 


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 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 node',
  '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',
)

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',
)

See the example file for a little how to use demonstration.For the rest you will just be limited by your imagination.

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 (4 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 Btree.php Class binary tree class source
  Plain text file node.php Class abstract node class source
  Plain text file NotNullnode.php Class not null node class source
  Plain text file Nullnode.php Class null node class source

 Version Control Unique User Downloads Download Rankings  
 0%
Total:101
This week:99
All time:8,945
This week:2Up