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

Preorder traversal sequence on descendants #38

Closed
elhoyos opened this issue Jan 11, 2013 · 6 comments
Closed

Preorder traversal sequence on descendants #38

elhoyos opened this issue Jan 11, 2013 · 6 comments

Comments

@elhoyos
Copy link
Contributor

elhoyos commented Jan 11, 2013

I believe this can be a very particular feature, but do you have any thoughts on making tag.descendants return its results with a preorder sequence? What would you recommend?

grandparent = Tag.find_or_create_by_path(["grandparent", "parentA", "childA"]).parent.parent
Tag.find_or_create_by_path(["grandparent", "parentB", "childB"])
grandparent.descendants
=> ["parentA", "parentB", "childA", "childB"] # current traversal sequence
=> ["parentA", "childA", "parentB", "childB"] # preorder traversal sequence 
@mceachen
Copy link
Collaborator

OK—how do you think the interface should look?

A) We add a new .preordered scope (so it'd be .descendants.preordered), or
B) Add a new specific-to-descendants scope, like preordered_descendants.

I think A) might be very difficult to apply generically (like, how would it work with ancestors?), so I'm leaning towards B).

For reference, there's http://en.wikipedia.org/wiki/Tree_traversal#Pre-order

@mceachen
Copy link
Collaborator

FWIW, preorder is exactly what this user is asking for over email:

I'm using a line like this with will paginate: @page.comments.paginate(page: 1, per_page: 4).hash_tree. With 5 test comments, one of which is a child of the first comment, this gives me:

first comment.
second comment.
third comment.
fourth comment.

But what I want is this:

first comment.
child of first comment.
second comment.
third comment.

@mceachen mceachen reopened this Jan 26, 2013
@mceachen
Copy link
Collaborator

This is implemented in 3.8.0 as self_and_descendants_preordered: https://github.com/mceachen/closure_tree#deterministic-ordering

@leonelgalan
Copy link

Does a roots_and_descendants_preordered makes sense? I'm trying to show the whole tree (with multiple roots) in an outline preordered.

@mceachen
Copy link
Collaborator

@leonelgalan: Interesting, I'll think about it—it should be doable.

@mceachen
Copy link
Collaborator

@leonelgalan — just released 3.10.0, which has this new class method, roots_and_descendants_preordered. The README was updated as well.

Thanks for the suggestion!

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

No branches or pull requests

3 participants