[Tree-hh] Inserting tree

Laurent Van Miegroet l.vanmiegroet at gmail.com
Tue Nov 3 11:06:44 GMT 2009


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/20091103/c653f814/attachment.htm 


More information about the Tree-hh mailing list