nicmart/tree
Composer 安装命令:
composer require nicmart/tree
包简介
A basic but flexible php tree data structure and a fluent tree builder implementation.
README 文档
README
In 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.
The tree data structure
The Tree\Node\NodeInterface interface abstracts the concept of a tree node. In Tree a Node has essentially two things:
a set of children (that implements the same NodeInterface interface) and a value.
On the other hand, the Tree\Node\Node gives a straight implementation for that interface.
Creating a node
use Tree\Node\Node; $node = new Node('foo');
Getting and setting the value of a node
Each node has a value property, that can be any php value.
$node->setValue('my value'); echo $node->getValue(); //Prints 'my value'
Adding one or more children
$child1 = new Node('child1'); $child2 = new Node('child2'); $node ->addChild($child1) ->addChild($child2);
Removing a child
$node->removeChild($child1);
Getting the array of all children
$children = $node->getChildren();
Overwriting the children set
$node->setChildren([new Node('foo'), new Node('bar')]);
Removing all children
$node->removeAllChildren();
Getting if the node is a leaf or not
A leaf is a node with no children.
$node->isLeaf();
Getting if the node is a child or not
A child is a node that has a parent.
$node->isChild();
Getting the parent of a node
Reference to the parent node is automatically managed by child-modifiers methods
$root->addChild($node = new Node('child')); $node->getParent(); // Returns $root
Getting the ancestors of a node
$root = (new Node('root')) ->addChild($child = new Node('child')) ->addChild($grandChild = new Node('grandchild')) ; $grandchild->getAncestors(); // Returns [$root, $child]
Related Methods
getAncestorsAndSelfretrieves ancestors of a node with the current node included.
Getting the root of a node
$root = $node->root();
Getting the neighbors of a node
$root = (new Node('root')) ->addChild($child1 = new Node('child1')) ->addChild($child2 = new Node('child2')) ->addChild($child3 = new Node('child3')) ; $child2->getNeighbors(); // Returns [$child1, $child3]
Related Methods
getNeighborsAndSelfretrieves neighbors of current node and the node itself.
Getting the number of nodes in the tree
$node->getSize();
Getting the depth of a node
$node->getDepth();
Getting the height of a node
$node->getHeight();
The Builder
The builder provides a convenient way to build trees. It is provided by the Builder class,
but you can implement your own builder making an implementation of the BuilderInterfaceclass.
Example
Let's see how to build the following tree, where the nodes label are represents nodes values:
A
/ \
B C
/|\
D E F
/|
G H
And here is the code:
$builder = new Tree\Builder\NodeBuilder; $builder ->value('A') ->leaf('B') ->tree('C') ->tree('D') ->leaf('G') ->leaf('H') ->end() ->leaf('E') ->leaf('F') ->end() ; $nodeA = $builder->getNode();
The example should be self-explanatory, but here you are a brief description of the methods used above.
Builder::value($value)
Set the value of the current node to $value
Builder::leaf($value)
Add to the current node a new child whose value is $value.
Builder::tree($value)
Add to the current node a new child whose value is $value, and set the new node as the builder current node.
Builder::end()
Returns to the context the builder was before the call to treemethod,
i.e. make the builder go one level up.
Builder::getNode()
Returns the current node.
Traversing a tree
Yield
You can obtain the yield of a tree (i.e. the list of leaves in a pre-order traversal) using the YieldVisitor.
For example, if $node is the tree built above, then
use Tree\Visitor\YieldVisitor; $visitor = new YieldVisitor; $yield = $node->accept($visitor); // $yield will contain nodes B, G, H, E, F
Pre-order Traversal
You can walk a tree in pre-order:
use Tree\Visitor\PreOrderVisitor; $visitor = new PreOrderVisitor; $yield = $node->accept($visitor); // $yield will contain nodes A, B, C, D, G, H, E, F
Post-order Traversal
You can walk a tree in post-order:
use Tree\Visitor\PostOrderVisitor; $visitor = new PostOrderVisitor; $yield = $node->accept($visitor); // $yield will contain nodes B, G, H, D, E, F, C, A
Install
Run
$ composer require nicmart/tree
Tests
phpunit
Changelog
Please have a look at CHANGELOG.md.
Contributing
Please have a look at CONTRIBUTING.md.
License
This package is licensed using the MIT License.
Please have a look at LICENSE.md.
nicmart/tree 适用场景与选型建议
nicmart/tree 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20.99M 次下载、GitHub Stars 达 575, 最近一次更新时间为 2013 年 04 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nicmart/tree 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nicmart/tree 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 20.99M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 587
- 点击次数: 20
- 依赖项目数: 29
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2013-04-21