Skip to content

Commit b8645db

Browse files
committed
README upd
1 parent 6995f69 commit b8645db

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,108 @@ Library for working with trees.
1313
composer require smoren/tree-tools
1414
```
1515

16+
## Quick reference
17+
18+
#### Tree Walker
19+
| Reducer | Description | Code Snippet |
20+
|---------------------------------------------------|--------------------------------------------|------------------------------------------------------------------|
21+
| [`traverseDepthFirst`](#Traverse-Depth-First) | Iterates a tree using depth-first search | `TreeWalker::traverseDepthFirst($data, $childrenContainerKey)` |
22+
| [`traverseBreadthFirst`](#Traverse-Breadth-First) | Iterates a tree using breadth-first search | `TreeWalker::traverseBreadthFirst($data, $childrenContainerKey)` |
23+
24+
## Usage
25+
26+
### Tree Walker
27+
28+
#### Traverse-Depth-First
29+
30+
Iterates a tree like a flat collection using depth-first traversal.
31+
32+
If `$childrenContainerKey` is not null looks for children items using by this key only.
33+
34+
Otherwise, considers any subarray to contain children.
35+
36+
```TreeWalker::traverseDepthFirst(iterable $data, ?string $childrenContainerKey = null): Generator```
37+
38+
```php
39+
use Smoren\TreeTools\TreeWalker;
40+
41+
$tree = [
42+
[
43+
'id' => 1,
44+
'children' => [
45+
['id' => 11],
46+
[
47+
'id' => 12,
48+
'children' => [
49+
['id' => 121],
50+
['id' => 122],
51+
],
52+
],
53+
],
54+
],
55+
[
56+
'id' => 2,
57+
'children' => [
58+
['id' => 21],
59+
],
60+
],
61+
['id' => 3],
62+
];
63+
64+
$result = [];
65+
66+
foreach(TreeWalker::traverseDepthFirst($tree) as $item) {
67+
$result[] = $item['id'];
68+
}
69+
var_dump($result);
70+
// [1, 11, 12, 121, 122, 2, 21, 3]
71+
```
72+
73+
#### Traverse-Breadth-First
74+
75+
Iterates a tree like a flat collection using depth-breadth traversal.
76+
77+
If `$childrenContainerKey` is not null looks for children items using by this key only.
78+
79+
Otherwise, considers any subarray to contain children.
80+
81+
```TreeWalker::traverseBreadthFirst(iterable $data, ?string $childrenContainerKey = null): Generator```
82+
83+
```php
84+
use Smoren\TreeTools\TreeWalker;
85+
86+
$tree = [
87+
[
88+
'id' => 1,
89+
'children' => [
90+
['id' => 11],
91+
[
92+
'id' => 12,
93+
'children' => [
94+
['id' => 121],
95+
['id' => 122],
96+
],
97+
],
98+
],
99+
],
100+
[
101+
'id' => 2,
102+
'children' => [
103+
['id' => 21],
104+
],
105+
],
106+
['id' => 3],
107+
];
108+
109+
$result = [];
110+
111+
foreach(TreeWalker::traverseBreadthFirst($tree) as $item) {
112+
$result[] = $item['id'];
113+
}
114+
var_dump($result);
115+
// [1, 2, 3, 11, 12, 21, 121, 122]
116+
```
117+
16118
## Unit testing
17119
```
18120
composer install

src/TreeWalker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
class TreeWalker
1515
{
1616
/**
17-
* Iterates a tree like a flat collection using deep traversal.
17+
* Iterates a tree like a flat collection using depth-first traversal.
1818
*
1919
* If $childrenContainerKey is not null looks for children items using by this key only.
2020
*
@@ -31,7 +31,7 @@ public static function traverseDepthFirst(iterable $data, ?string $childrenConta
3131
}
3232

3333
/**
34-
* Iterates a tree like a flat collection using wide traversal.
34+
* Iterates a tree like a flat collection using breadth-first traversal.
3535
*
3636
* If $childrenContainerKey is not null looks for children items using by this key only.
3737
*

0 commit comments

Comments
 (0)