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

Not possible to navigate up all parents to the tree root #36

Open
macdew opened this issue Jun 10, 2022 · 1 comment
Open

Not possible to navigate up all parents to the tree root #36

macdew opened this issue Jun 10, 2022 · 1 comment

Comments

@macdew
Copy link

macdew commented Jun 10, 2022

In my scenario, I have an st_tree and an iterator to an arbitrary node in the tree. I then try to build the path for that node by visiting every parent node back to the tree root. However, this kind of loop doesn't work:

auto& parent_node = iterator_to_tree_node->parent();
while (!parent_node.is_root())
{
  // do something with parent_node.data();
  parent_node = parent_node.parent();
}

In the last line, parent_node = parent_node.parent() triggers the operator=() for node_type, and thus starts a graft.

I'm not sure that this operator=() is necessary to have as a node assignment? Perhaps a method should do this instead?

Alternatively, perhaps it would be an idea to add a parent-visitor iterator that could navigate up the tree?

In the end I solved this by using a recursive lambda to visit each parent, thus avoiding the assignment, but it feels a bit overkill to solve this problem that way.

@erikerlandson
Copy link
Owner

@macdew thanks for pointing this out, that is a bit unintuitive. I'm not immediately sure what to do in terms of backward compatibility - I'd hate to break people's code if they are making use of the current semantic.

Maybe making parent_node a pointer to a node, and using parent_node = &(parent_node.parent()) ? Not really a modern c++ pattern, though.

A new kind of iterator is definitely plausible.

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

2 participants