|
[Tree-hh] Inserting treeKasper Peeters kasper.peeters at aei.mpg.deTue Nov 3 14:32:05 GMT 2009
Hi Laurent, > I am trying to use your tree structure. What i would like to do is adding > trees to other trees. I think, i managed to do it quite easily. > However, i would like to be able from any node to reach the new head of the > tree after inserting a tree. How can i do that ? > You can see the code below. I create 2 tree (tree, and tree2), after that i > would like from any node to reach the head of merged tree, so head a? First of all, what your code does when you insert Tree2 into Tree is is that it makes a copy of the nodes of Tree2 and puts that copy into Tree. So after the append_children() call, Tree2 is still just the object containing 4 /|\ 5 6 7 whereas Tree will become 1 /|\ 2 3 4 /|\ 5 6 7 If you want to move the nodes out of Tree2 and into Tree, you need to use some of the move_* members. E.g. insert the '3' node into Tree using tree<foo*>::iterator KK=Tree.append_child(Tree.begin(),bb); and then use the 'KK' iterator to do Tree.move_after(KK, Tree2.begin()); This will empty Tree2, and produce a Tree just as above. However, this does not yet solve your problem, because now Tree2.begin() will give an invalid iterator (==Tree2.end()), because Tree2 has been emptied. Tree.begin() returns the head of the full tree (i.e. '1'). In order to find '1' from e.g. '5', you have to walk upwards from there. If you insert with tree<foo*>::iterator KK5 = Tree2.append_child(Tree2.begin(),d); then you can walk to the top node with int depth=Tree.depth(KK5); for(int i=0; i<depth; ++i) KK5=Tree.parent(KK5); Does that help? (I should probably have added a 'get_head(iterator)' member which returns the top level node for a given node, but alas, I haven't yet). Cheers, Kasper
More information about the Tree-hh mailing list |