Skip to content
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

Iteration order of traverse and descendants? #38

Open
StarlessNights opened this issue Dec 5, 2024 · 0 comments
Open

Iteration order of traverse and descendants? #38

StarlessNights opened this issue Dec 5, 2024 · 0 comments

Comments

@StarlessNights
Copy link

The documentation doesn't specify the order in which NodeRef::traverse and NodeRef::descendants iterate over the nodes. This information should be added to the documentation. I.e. breadth-first, depth-first or something else?

In my specific usecase the only important thing about the order would be a guarantee that a node won't be visited before it's parent.

(A similar ambiguity applies to NodeRef::ancestors too.)

PS. This is a nice crate providing an intuitive tree structure!

@StarlessNights
Copy link
Author

StarlessNights commented Dec 9, 2024

I had a look at the code.

The traverse test illustrates the behavior nicely.

The order is depth-first, with traverse returning Edges instead of NodeRefs. Edge seems to be a thin wrapper around NodeRef, and only adds Open/Close variants. (Pardon me if I'm being illiterate, but the variant meanings are not obvious to me.) Apparently Edge::Close just means that the node has been visited already.

Anyway descendants skips the Edges and just yields NodeRefs in depth-first order, just as expected 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant