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

Build tree hierarchy by ancestors #5

Open
alexanderpa opened this issue Jan 29, 2016 · 8 comments
Open

Build tree hierarchy by ancestors #5

alexanderpa opened this issue Jan 29, 2016 · 8 comments

Comments

@alexanderpa
Copy link

It would be great to have possibility to build hierarchy tree not only by parent reference but with ancestors also.

@joaonuno
Copy link
Owner

Can you please share an example?

@alexanderpa
Copy link
Author

Yes, sure. For example you have flat list of nodes:
[
{ id: 1, name: 'first', ancestors: [] },
{ id: 2, name: 'second', ancestors: [1] },
{ id: 3, name: 'third', ancestors: [1,2] },
...
]

This list should be converted to:

{
id: 1,
name: 'first',
ancestors: [],
children: [
{
id: 2,
name: 'second',
ancestors: [1],
children: [...]
}
]
}

@joaonuno
Copy link
Owner

It seems that's not a tree, because a tree node should have just one ancestor (or parent) right?
Where would you represent node 3 in your example? It is a child of both nodes 1 and 2?

@alexanderpa
Copy link
Author

node3 should be the child node of node2

@alexanderpa
Copy link
Author

you could see an example how this tree structure stored in mongodb https://docs.mongodb.org/manual/tutorial/model-tree-structures-with-ancestors-array/

@joaonuno
Copy link
Owner

ok, got it, that's an optimization for fast path retrieval. For tree construction, all that's necessary is the last ancestor.

I guess that could be supported if instead of using a parent property we could specify a function to get the parent and in this case the developer could pass a function to get the last ancestor.

Something like:

flatToNested = new FlatToNested({
    // The function to get the parent id
    parent: function (node) {
        return node.ancestors[node.ancestors.length - 1];
    }
}});

What do you think?

@alexanderpa
Copy link
Author

Looks good to me.

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

3 participants
@joaonuno @alexanderpa and others