erikerlandson / st_tree

A fast and flexible c++ template class for tree data structures
https://github.com/erikerlandson/st_tree/wiki
Apache License 2.0
95 stars 20 forks source link

Question about obtaining a node_type pointer to the root node #25

Open ranchmed opened 4 years ago

ranchmed commented 4 years ago

I am thinking about using st_tree on a new project, and have been doing some preliminary testing.

I have one question about obtaining a pointer to the root node to help me construct a tree using node_type iterators.

In an earlier issue, you commented that you could get a node_type iterator from the tree by doing:

st_tree::tree<int> tree;
int a{1};
auto it = tree.root().insert(a);

Upon execution, I get an empty tree exception. This makes sense as the insert is done after the root() is dereferenced.

I can make it work by inserting a placeholder as follows:

st_tree::tree<int> tree;
int a{1};
tree.insert(0);  // Placeholder!
auto it = tree.root().insert(a);

But then we have an extra ply that really isn't part of the tree.

Is there another way to insert a root node in a tree, and then to get an node_type iterator for that node?

Thanks, Mark

erikerlandson commented 4 years ago

Hi Mark! My memory of some design decisions is a bit rusty (I wrote this 10 years ago) but IIRC this is a consequence of some assymetries from trying to support a tree<> that could be either empty or have one root element. In more modern FP parlance it's really the analog of something like scala Option[T] acting like a sequence that is either empty or has one element.

It's slightly hinky under the hood, but I think it might be well defined to have tree<> return begin/end in either the empty or non empty case.

erikerlandson commented 3 years ago

I see this has been open nearly a year :grimacing:

However, I'm going to leave this open in the event that either I have the bandwidth to work on it, or someone else wants to contribute an implementation.