-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Tree] Add pre-order and level-order traversals #2498
base: main
Are you sure you want to change the base?
Conversation
2fad25b
to
442d99b
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2498 +/- ##
==========================================
+ Coverage 79.22% 79.30% +0.07%
==========================================
Files 161 161
Lines 8415 8455 +40
==========================================
+ Hits 6667 6705 +38
- Misses 1748 1750 +2 ☔ View full report in Codecov by Sentry. |
3e19794
to
c47fa1b
Compare
It will be nice if this PR was successful and merge. Thank you. |
Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
42c0094
to
079cec2
Compare
Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
Co-authored-by: Javier Spagnoletti <phansys@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we can add object
type to parameters
@@ -58,6 +58,7 @@ a release. | |||
|
|||
### Added | |||
- Tree: Add `Nested::ALLOWED_NODE_POSITIONS` constant in order to expose the available node positions | |||
- Tree: Add pre-order and level-order traversals (#2498). Add `getNextNode()` and `getNextNodes()` methods to traverse the tree |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be moved to the ## [Unreleased]
section
* | ||
* @throws InvalidArgumentException if input is invalid | ||
*/ | ||
public function getNextNodesQueryBuilder($root, $node = null, ?int $limit = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): QueryBuilder |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function getNextNodesQueryBuilder($root, $node = null, ?int $limit = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): QueryBuilder | |
public function getNextNodesQueryBuilder(object $root, ?object $node = null, ?int $limit = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): QueryBuilder |
Should we make this method private
?
* @param int|null $limit Maximum nodes to return. If null, all nodes will be returned | ||
* @param self::TRAVERSAL_* $traversalStrategy Strategy to use to traverse tree | ||
*/ | ||
public function getNextNodesQuery($root, $node = null, ?int $limit = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): Query |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function getNextNodesQuery($root, $node = null, ?int $limit = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): Query | |
public function getNextNodesQuery(object $root, ?object $node = null, ?int $limit = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): Query |
* @param object|null $node Current node. If null, first node will be returned | ||
* @param self::TRAVERSAL_* $traversalStrategy Strategy to use to traverse tree | ||
*/ | ||
public function getNextNode($root, $node = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): ?object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function getNextNode($root, $node = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): ?object | |
public function getNextNode(object $root, ?object $node = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): ?object |
* | ||
* @return array<object> | ||
*/ | ||
public function getNextNodes($root, $node = null, ?int $limit = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public function getNextNodes($root, $node = null, ?int $limit = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): array | |
public function getNextNodes(object $root, ?object $node = null, ?int $limit = null, string $traversalStrategy = self::TRAVERSAL_PRE_ORDER): array |
@franmomu I agree to add the
IMO, the second one it the best answer since we improve code without any BC break. Before doing any commit, is it OK for you @phansys ? |
} | ||
} elseif (self::TRAVERSAL_LEVEL_ORDER === $traversalStrategy) { | ||
if (!isset($config['level'])) { | ||
throw new \InvalidArgumentException('TreeLevel must be set to use level order traversal.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why \InvalidArgumentException
? $config['level']
isn't provided as an argument in this method.
LGTM. |
This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
github-actions, you can keep this PR opened. I'll fix conflicts soon. |
In order to get the next node when traversing a tree, this PRs add new methods
NestedTreeRepository::getNextNode
andNestedTreeRepository::getNextNodes
. They take some arguments :$root
is the top node and is used to limit result to this (sub-)tree$node
is the current node (or null for the first call)$limit
(only forgetNextNodes
) can limit nodes count to return$traversalStrategy
acceptsself::TRAVERSAL_PRE_ORDER
(default) orself::TRAVERSAL_LEVEL_ORDER
and is used to determine which algorithm use (see https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder).These methods can be used, for example, to read a book page after page: