|
[Tree-hh] Inserting treeLaurent Van Miegroet l.vanmiegroet at gmail.comThu Nov 5 12:45:05 GMT 2009
Hi Kasper, I come back to you with the real code, that may be more clear about what i want to do. I think, i prefere to use the append child in order to keep the data member Tree of my class for each of my object (see below) I have a base class LevelSet. From that class derive some special LevelSet like a LsPlane (which is just a Plane). But, i also have 3 special LevelSet which are union, diff, intersection. A Union, is just made of several levelset. So what i want to do, is built a tree like this : Lv / \ lvA lvC / \ | \ lv1 lv2 lv5 \ lvB / \ lv3 lv4. So, as i should be able to add a simple levelset or union to a union, i have introduced a Tree in the base class LevelSet: class LevelSet: public PhySet { protected: std::vector<double> levelSetValue; int flag; Material *mater; int tempSize; int _tag; static int GlobalTag; tree<LevelSet*> myTree; protected: .... public: tree<LevelSet*>::leaf_iterator begin_leaf(){return myTree.begin_leaf();} tree<LevelSet*>::leaf_iterator end_leaf(){return myTree.end_leaf();} tree<LevelSet*>::iterator beginTree(){return myTree.begin();} tree<LevelSet*>::iterator endTree(){return myTree.end();} }; *A simple levelset has a tree with only one head :* LevelSet::LevelSet() { flag=1; father=0; _tag=++GlobalTag; mater=0; tempSize=0; myTree.set_head(this); } An in Union : class LsUnion: public LevelSet {} void LsUnion::add(LevelSet * Ls) { Ls->setFather(this); listOfUnion.push_back(Ls); myTree.append_children(myTree.begin(),Ls->beginTree(),Ls->endTree()); } void LsUnion::print() const { //std::cout<<std::endl; std::cout<<this<<std::endl; std::cerr<<"Tag : "<<_tag<<std::endl; std::cerr<<"UNION"<<std::endl; tree<LevelSet*>::leaf_iterator it_leaf=myTree.begin_leaf(); tree<LevelSet*>::leaf_iterator end_leaf=myTree.end_leaf(); for(it_leaf;it_leaf!=end_leaf;it_leaf++) (*it_leaf)->print(); std::cerr<<"-------------"<<std::endl; tree<LevelSet*>::iterator it=myTree.begin(); tree<LevelSet*>::iterator end=myTree.end(); for(it;it!=end;it++) (*it)->print(); // int i; // for(i=0;i<listOfUnion.size();i++) // listOfUnion[i]->print(); } and my example is this : LsPlane lv1(cnt1,axis1); LsPlane lv2(cnt2,axis2); LsPlane lv3(cnt3,axis3); LsPlane lv4(cnt4,axis4); LsPlane lv5(cnt5,axis5); LsUnion lvA; lvA.add(lv1); lvA.add(lv2); LsUnion lvB; lvB.add(lv3); lvB.add(lv4); LsUnion lvC; lvC.add(lv5); lvC.add(lvB); LsUnion lv; lv.add(lvA); lv.add(lvC); lv.print(); When i call the print, the part with the leaves is ok, but i get an infinite loop for printing from begin to end(). To you have any idea or suggestion about the use of your Tree.hh for my purpose ? thanks again Laurent 2009/11/3 Laurent Van Miegroet <l.vanmiegroet at gmail.com> > Hello, > > 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 ? How > can i do that > > Thanks > Laurent. > > #include <iostream> > #include <vector> > #include "tree.hh" > class foo > { > int num; > public: > foo(int _num){num=_num;} > ~foo() { > std::cout << "~foo()" << std::endl; > } > void print(){std::cerr<<num<<" "<<this<<std::endl;} > }; > > int main() { > std::vector<foo*> bar; > foo *a=new foo(1); > foo *b=new foo(2); > foo *bb=new foo(3); > bar.push_back(a); > > tree<foo*> Tree; > Tree.set_head(a); > Tree.append_child(Tree.begin(),b); > Tree.append_child(Tree.begin(),bb); > > foo *c=new foo(4); > foo *d=new foo(5); > foo *e=new foo(6); > foo *f=new foo(7); > > tree<foo*> Tree2; > Tree2.set_head(c); > Tree2.append_child(Tree2.begin(),d); > Tree2.append_child(Tree2.begin(),e); > Tree2.append_child(Tree2.begin(),f); > > tree<foo*>::iterator it=Tree.begin(); > tree<foo*>::iterator end=Tree.end(); > for(it;it!=end;++it) > (*it)->print(); > std::cerr<<"----------------"<<std::endl; > > it=Tree2.begin(); > end=Tree2.end(); > for(it;it!=end;++it) > (*it)->print(); > > std::cerr<<"----------------"<<std::endl; > Tree.append_children(Tree.begin(),Tree2.begin(),Tree2.end()); > it=Tree.begin(); > end=Tree.end(); > for(it;it!=end;++it) > (*it)->print(); > > std::cerr<<"-----leaf-----------"<<std::endl; > tree<foo*>::leaf_iterator itl=Tree.begin_leaf(); > tree<foo*>::leaf_iterator endl=Tree.end_leaf(); > > for(itl;itl!=end;++itl) > { > std::cerr<<"leaf is"<<std::endl; > (*itl)->print(); > } > it=Tree2.begin(); > > (*it)->print(); > > return 0; > } > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.hepforge.org/lists-archive/tree-hh/attachments/20091105/ee7f690c/attachment.htm
More information about the Tree-hh mailing list |