0

I'm trying to implement an N-ary Tree in c++ using the glib, but as I'm not a c++ expert, I'm having some problems finding out how to use it right. Does anybody have a simple example written in C++ to help me understand how to use the basic functions? I'm having special problems with g_node_traverse, I just can't get the GNodeTraverseFunc right.

You can find the description of the N-ary Tree here: http://developer.gnome.org/glib/stable/glib-N-ary-Trees.html

I found some examples in c, but I couldn't manage to translate them correctly into c++ here:

http://www.ibm.com/developerworks/linux/tutorials/l-glib/section7.html

Tried with the last piece of code for n-ary trees.

I appreciate your help.

  • Most of those functions should be simple, since I don't see anything about balancing or rotating. which one(s) are confusing you? – Mooing Duck Mar 07 '13 at 20:30
  • From the [FAQ](http://stackoverflow.com/faq): We feel the best Stack Overflow questions have a bit of source code in them, but your question should generally cover (A) a specific programming problem (B) a software algorithm (C) software tools commonly used by programmers (D) practical, answerable problems that are unique to the programming profession. You should only ask practical, answerable questions based on actual problems that you face. Your questions should be reasonably scoped. If you can imagine an entire book that answers your question, you’re asking too much. – Mooing Duck Mar 07 '13 at 20:32
  • I really wouldn't use anything in the Gnome N-ary library in C++, the C API does not map well into C++, and ought to be completely redesigned from scratch. Why do you even want this in C++? What's wrong with just adding a `std::vector children` member to your `T*` class? – Mooing Duck Mar 07 '13 at 20:35
  • @MooingDuck: Thanks for your comments. It's the first time I actually post a question. I appreciate the time you took to answer and any other comment you may have. – Altairtoral Mar 08 '13 at 12:26
  • Well, if you have answered your question, then this post should be closed. If you want a codereview, that goes on codereview.stackexchange.com – Mooing Duck Mar 08 '13 at 17:25

1 Answers1

1

Well, I have managed to run some code. The problem was basically the casts that were needed because Gnome uses gpointers and my data is to be stored in a struct. So my code is:

    gboolean iter(GNode* n, gpointer data) {
     node s=*(node *)(n->data);
     int ID=g_node_depth(n);

     if (G_NODE_IS_ROOT(n)==true)
     {
         std::cout<<"Node "<<ID<<" is a Root"<<std::endl;
     }
     else if (G_NODE_IS_LEAF(n)==true)
     {
         std::cout<<"Node "<<ID<<" is a Leaf"<<std::endl;
     }

     std::cout<<"Speed of Node "<<ID<<" is: "<<s.v<<std::endl;
     return FALSE;
    }

    int main(){

        node prueba,prueba1;
        prueba.phi=0; 
        prueba.v=1;   
        prueba.x=50;  
        prueba.y=100; //Position in y

        prueba1.phi=90;
        prueba1.v=6;
        prueba1.x=30;
        prueba1.y=90;

        GNode * root = g_node_new((gpointer) &prueba);
        g_node_append(root, g_node_new((gpointer) &prueba1));

        g_node_traverse(root, G_PRE_ORDER, G_TRAVERSE_ALL, -1, iter, NULL);

     return 0;

    }

Where my struct is:

    struct state {
        double x;    //Position in x of a car
        double y;    //Position in y "
        double phi; //Yaw angle of a car
        double v;   //Speed of a car
    };


    struct node {
        double x;
        double y;
        double phi;
        double v;
        std::vector <state > trajectory;
    };

The idea is to have the whole previous "trajectory" stored at each node so if I choose a random node/leaf I don't have to reconstruct the trajectory but just take it.

This code works now. It might be improved and I'm open to any comment.

I hope it's useful for someone.

  • If you're using the C API directly, you're bound to bump into C++ related inconveniences (such as necessary unsafe typecasts). Why not use the official C++ (glibmm) bindings instead? [There](https://developer.gnome.org/glibmm/stable/classGlib_1_1NodeTree.html), the nary tree is implemented as a template class, which should make things way easier for you. – Ancurio Mar 13 '13 at 13:15
  • @Ancurio Thanks a lot for the Tip! I didn't know there was an implementation for C++. Do you happen to have any sample code for it? – Altairtoral Mar 18 '13 at 17:49
  • This is something that I just scribbled down quickly from looking at the docs: http://pastebin.com/JNSpaaYu – Ancurio Mar 20 '13 at 12:43
  • @Ancurio Thanks a lot! I'm checking it out. – Altairtoral Mar 25 '13 at 11:33
  • Just in case anyone wants to use the TraverseFlags, I struggled a couple of hours trying to find out how to access them on your code @Ancurio. Here it is: `tree.traverse(sigc::ptr_fun(&travFun), Glib::TRAVERSE_PRE_ORDER,ENode::TRAVERSE_LEAVES,-1);` – Altairtoral Apr 10 '13 at 09:21