{"id":13545610,"url":"https://github.com/nicmart/Tree","last_synced_at":"2025-04-02T15:31:33.842Z","repository":{"id":8157202,"uuid":"9578144","full_name":"nicmart/Tree","owner":"nicmart","description":"A basic but flexible tree data structure for php and a fluent tree builder implementation.","archived":false,"fork":false,"pushed_at":"2024-10-24T11:03:22.000Z","size":947,"stargazers_count":570,"open_issues_count":5,"forks_count":62,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-10-29T20:55:55.286Z","etag":null,"topics":["data-structrues","php","tree"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nicmart.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["localheinz"]}},"created_at":"2013-04-21T10:52:16.000Z","updated_at":"2024-10-28T01:23:05.000Z","dependencies_parsed_at":"2023-01-13T14:39:17.368Z","dependency_job_id":"2c0aafb7-c594-4f73-a97d-0166686fd6da","html_url":"https://github.com/nicmart/Tree","commit_stats":{"total_commits":376,"total_committers":16,"mean_commits":23.5,"dds":0.5079787234042553,"last_synced_commit":"229880e47b951c253cf2b329997fb52571a7439d"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicmart%2FTree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicmart%2FTree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicmart%2FTree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nicmart%2FTree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nicmart","download_url":"https://codeload.github.com/nicmart/Tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246841789,"owners_count":20842647,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-structrues","php","tree"],"created_at":"2024-08-01T11:01:06.959Z","updated_at":"2025-04-02T15:31:28.832Z","avatar_url":"https://github.com/nicmart.png","language":"PHP","readme":"# Tree\n\n[![Integrate](https://github.com/nicmart/Tree/workflows/Integrate/badge.svg?branch=master)](https://github.com/nicmart/Tree/actions)\n[![Release](https://github.com/nicmart/Tree/workflows/Release/badge.svg?branch=master)](https://github.com/nicmart/Tree/actions)\n\n[![Code Coverage](https://codecov.io/gh/nicmart/tree/branch/master/graph/badge.svg)](https://codecov.io/gh/nicmart/tree)\n[![Type Coverage](https://shepherd.dev/github/nicmart/tree/coverage.svg)](https://shepherd.dev/github/nicmart/tree)\n\n[![Latest Stable Version](https://poser.pugx.org/nicmart/tree/v/stable)](https://packagist.org/packages/nicmart/tree)\n[![Total Downloads](https://poser.pugx.org/nicmart/tree/downloads)](https://packagist.org/packages/nicmart/tree)\n[![Monthly Downloads](http://poser.pugx.org/nicmart/tree/d/monthly)](https://packagist.org/packages/nicmart/tree)\n\nIn Tree you can find a basic but flexible tree data structure for php together with and an handful Builder class, that enables you to build tree in a fluent way.\n\n## The tree data structure\n\nThe `Tree\\Node\\NodeInterface` interface abstracts the concept of a tree node. In `Tree` a Node has essentially two things:\na set of children (that implements the same `NodeInterface` interface) and a value.\n\nOn the other hand, the `Tree\\Node\\Node` gives a straight implementation for that interface.\n\n### Creating a node\n\n```php\nuse Tree\\Node\\Node;\n\n$node = new Node('foo');\n```\n\n### Getting and setting the value of a node\n\nEach node has a value property, that can be any php value.\n```php\n$node-\u003esetValue('my value');\necho $node-\u003egetValue(); //Prints 'my value'\n```\n\n### Adding one or more children\n\n```php\n$child1 = new Node('child1');\n$child2 = new Node('child2');\n\n$node\n    -\u003eaddChild($child1)\n    -\u003eaddChild($child2);\n```\n\n### Removing a child\n\n```php\n$node-\u003eremoveChild($child1);\n```\n\n### Getting the array of all children\n\n```php\n$children = $node-\u003egetChildren();\n```\n\n### Overwriting the children set\n\n```php\n$node-\u003esetChildren([new Node('foo'), new Node('bar')]);\n```\n\n### Removing all children\n\n```php\n$node-\u003eremoveAllChildren();\n```\n\n### Getting if the node is a leaf or not\n\nA leaf is a node with no children.\n\n```php\n$node-\u003eisLeaf();\n```\n\n### Getting if the node is a child or not\n\nA child is a node that has a parent.\n\n```php\n$node-\u003eisChild();\n```\n\n### Getting the parent of a node\n\nReference to the parent node is automatically managed by child-modifiers methods\n\n```php\n$root-\u003eaddChild($node = new Node('child'));\n$node-\u003egetParent(); // Returns $root\n```\n\n### Getting the ancestors of a node\n\n```php\n$root = (new Node('root'))\n    -\u003eaddChild($child = new Node('child'))\n    -\u003eaddChild($grandChild = new Node('grandchild'))\n;\n\n$grandchild-\u003egetAncestors(); // Returns [$root, $child]\n```\n\n#### Related Methods\n\n- `getAncestorsAndSelf` retrieves ancestors of a node with the current node included.\n\n### Getting the root of a node\n\n```php\n$root = $node-\u003eroot();\n```\n\n### Getting the neighbors of a node\n\n```php\n$root = (new Node('root'))\n    -\u003eaddChild($child1 = new Node('child1'))\n    -\u003eaddChild($child2 = new Node('child2'))\n    -\u003eaddChild($child3 = new Node('child3'))\n;\n\n$child2-\u003egetNeighbors(); // Returns [$child1, $child3]\n```\n\n#### Related Methods\n\n- `getNeighborsAndSelf` retrieves neighbors of current node and the node itself.\n\n### Getting the number of nodes in the tree\n\n```php\n$node-\u003egetSize();\n```\n\n### Getting the depth of a node\n\n```php\n$node-\u003egetDepth();\n```\n\n### Getting the height of a node\n\n```php\n$node-\u003egetHeight();\n```\n\n## The Builder\n\nThe builder provides a convenient way to build trees. It is provided by the ```Builder``` class,\nbut you can implement your own builder making an implementation of the ```BuilderInterface```class.\n\n### Example\n\nLet's see how to build the following tree, where the nodes label are represents nodes values:\n\n```\n       A\n      / \\\n     B   C\n        /|\\\n       D E F\n      /|\n     G H\n```\n\nAnd here is the code:\n\n```php\n$builder = new Tree\\Builder\\NodeBuilder;\n\n$builder\n    -\u003evalue('A')\n    -\u003eleaf('B')\n    -\u003etree('C')\n        -\u003etree('D')\n            -\u003eleaf('G')\n            -\u003eleaf('H')\n            -\u003eend()\n        -\u003eleaf('E')\n        -\u003eleaf('F')\n        -\u003eend()\n;\n\n$nodeA = $builder-\u003egetNode();\n```\n\nThe example should be self-explanatory, but here you are a brief description of the methods used above.\n\n### Builder::value($value)\n\nSet the value of the current node to ```$value```\n\n### Builder::leaf($value)\n\nAdd to the current node a new child whose value is ```$value```.\n\n### Builder::tree($value)\n\nAdd to the current node a new child whose value is ```$value```, and set the new node as the builder current node.\n\n### Builder::end()\n\nReturns to the context the builder was before the call to ```tree```method,\ni.e. make the builder go one level up.\n\n### Builder::getNode()\n\nReturns the current node.\n\n## Traversing a tree\n\n### Yield\n\nYou can obtain the yield of a tree (i.e. the list of leaves in a pre-order traversal) using\nthe YieldVisitor.\n\nFor example, if `$node` is the tree built above, then\n\n```php\nuse Tree\\Visitor\\YieldVisitor;\n\n$visitor = new YieldVisitor;\n\n$yield = $node-\u003eaccept($visitor);\n// $yield will contain nodes B, G, H, E, F\n```\n\n### Pre-order Traversal\n\nYou can walk a tree in pre-order:\n\n```php\nuse Tree\\Visitor\\PreOrderVisitor;\n\n$visitor = new PreOrderVisitor;\n\n$yield = $node-\u003eaccept($visitor);\n// $yield will contain nodes A, B, C, D, G, H, E, F\n```\n\n### Post-order Traversal\n\nYou can walk a tree in post-order:\n\n```php\nuse Tree\\Visitor\\PostOrderVisitor;\n\n$visitor = new PostOrderVisitor;\n\n$yield = $node-\u003eaccept($visitor);\n// $yield will contain nodes B, G, H, D, E, F, C, A\n```\n\n## Install\n\nRun\n\n```\n$ composer require nicmart/tree\n```\n\n# Tests\n\n```\nphpunit\n```\n\n## Changelog\n\nPlease have a look at [`CHANGELOG.md`](CHANGELOG.md).\n\n## Contributing\n\nPlease have a look at [`CONTRIBUTING.md`](.github/CONTRIBUTING.md).\n\n## License\n\nThis package is licensed using the MIT License.\n\nPlease have a look at [`LICENSE.md`](LICENSE.md).\n","funding_links":["https://github.com/sponsors/localheinz"],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicmart%2FTree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnicmart%2FTree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicmart%2FTree/lists"}