Here is tree.hh , which is a bit close to what you want to do, although a little different.
Here is a snippet of code extracted from his website.
int main(int, char **) { tree<string> tr; tree<string>::iterator top, one, two, loc, banana; top=tr.begin(); one=tr.insert(top, "one"); two=tr.append_child(one, "two"); tr.append_child(two, "apple"); banana=tr.append_child(two, "banana"); tr.append_child(banana,"cherry"); tr.append_child(two, "peach"); tr.append_child(one,"three"); loc=find(tr.begin(), tr.end(), "two"); if(loc!=tr.end()) { tree<string>::sibling_iterator sib=tr.begin(loc); while(sib!=tr.end(loc)) { cout << (*sib) << endl; ++sib; } cout << endl; tree<string>::iterator sib2=tr.begin(loc); tree<string>::iterator end2=tr.end(loc); while(sib2!=end2) { for(int i=0; i<tr.depth(sib2)-2; ++i) cout << " "; cout << (*sib2) << endl; ++sib2; } } }
Now, what else? Your implementation is simpler when it comes to add node to the tree. Although your version is inaudibly simpler, the developer of this library probably wanted to have information available without looking at the tree, such as the size of the tree.
I also assume that he did not want to store the root on all nodes due to performance. Therefore, if you want to implement it in your own way, I suggest that you save most of the logic and add a link to the parent tree in the iterator and rewrite a bit.
fulmicoton
source share