DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Node tree

  1. #1
    Join Date
    Oct 2008
    Posts
    31

    Node tree

    I think they call it a node tree?

    Anyways I can't figure out what I've done wrong, here's the code you can see what I'm trying to do.

    Code:
    #include <iostream>
    #include <string>
    
    class sMenu
    {
        public:
        sMenu *sub;
    
        //name
        std::string name;
    } Menu[2];
    
    int main()
    {
        // Menu 1: file
        Menu[0].name = "File";
    
        /*sub menu for file*/
        // allocate memory for it
        Menu[0].sub    = new int[2];
        Menu[0].sub[0].name = "Save";
        Menu[0].sub[1].name = "Load";
    
        // Menu 2: Tools
        Menu[1].name = "Tools";
    
        // allocate memory for it
        Menu[1].sub    = new int[2];
        Menu[1].sub[0].name = "Shape";
    
        // allocate memory for it
        Menu[1].sub[0].sub  = new int[2];
        Menu[1].sub[0].sub[0].name      = "Triangle";
        Menu[1].sub[0].sub[1].name      = "Square";
    
        Menu[1].sub[1] = "Spray";
    
        // Delete memory
    
        delete[] Menu[0].sub;
        delete[] Menu[1].sub;
    
        return 0;
    }
    My other problem is the delete I don't know if that would delete the whole of the "node tree" memory.

    Oh by the way after I figure this out I wanna know if there's a way to grab hold of another part of my tree without having to remember how many they are.

    Like ...

    if Menu[0].sub have a new node then
    Grab the next node
    endif

    (not real code)

    - Nicky

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    It leaks memory:

    Break it down and we see:

    // allocate memory for it
    Menu[1].sub = new int[2];

    // allocate memory for it
    Menu[1].sub[0].sub = new int[2];


    // Delete memory

    delete[] Menu[0].sub;
    delete[] Menu[1].sub;

    ----------------------
    so you allocate menu[1].sub.sub, so you must delete it, and do that first:
    delete [] menu[1].sub[0].sub;
    delete [] menu[1].sub;

    etc. Always delete from the inside out.

  3. #3
    Join Date
    Dec 2003
    Posts
    3,366
    Not knowing what its supposed to do, what it actually does, etc, I could not say what might be wrong with it. Post errors or describe what the problem is.


    You can use pointers to hold onto a subsections.
    For example:

    menu * toolz = &(menu[1]);
    or
    menu * shapez = &(menu[1].sub[1].sub[0]);

    etc. The above may be the wrong type or wrong location, but the point is that is how you could do it.

  4. #4
    Join Date
    Oct 2008
    Posts
    31
    Code:
    C:\Users\Home\Documents\Untitled1.cpp||In function 'int main()':|
    C:\Users\Home\Documents\Untitled1.cpp|20|error: cannot convert 'int*' to 'sMenu*' in assignment|
    C:\Users\Home\Documents\Untitled1.cpp|28|error: cannot convert 'int*' to 'sMenu*' in assignment|
    C:\Users\Home\Documents\Untitled1.cpp|32|error: cannot convert 'int*' to 'sMenu*' in assignment|
    C:\Users\Home\Documents\Untitled1.cpp|36|error: no match for 'operator=' in '*(Menu[1].sMenu::sub + 8u) = "Spray"'|
    C:\Users\Home\Documents\Untitled1.cpp|5|note: candidates are: sMenu& sMenu::operator=(const sMenu&)|
    ||=== Build finished: 4 errors, 0 warnings ===|
    That's the errors.

    Well I'm making a menu for a program I'm making in OpenGL. This is the menu for it, I have a charset texture so I can take the letters out of the image and bind them onto an object (most probobly rectangles).

    But for now I wanna just solve this problem seperate from my other code so any chance you can help me fix this? I've been looing around on google and I can't see what I done wrong, and this is my first time using a binary tree routine.

    After I do manage to get this fix I wanna be able to print this on screen like this (just for testing).

    Code:
    File
    .Save
    .Load
    
    tools
    .shape
    ..Square
    ..Triangle
    .Spray
    Than you for the advise so far about deleting the memory just wasn't sure if I deleted the main root it would delete the whole tree, but obvious not.

    Hope I cleared it up abit on what I'm trying to achieve.

    - Nicky

  5. #5
    Join Date
    Dec 2003
    Posts
    3,366
    first time tree eh? Oh no...

    Ok the errors are that you are trying to use 2 different types of pointers and that does not work, not even with polymorhpism. You want poly, you have to talk directly with the object by pointer dereference. Pointers are more basic than this, its a raw memory location where somthing IS in RAM, and no matter how much one class can pretend to be another, their binary size is not the same and the memory layout is different and so on, so the pointers dont polymorph.

    So to fix it, make the pointers the same type. But thats easier said than done, so... coming in a min, a post on binary trees and making life easier.

  6. #6
    Join Date
    Dec 2003
    Posts
    3,366
    Edit: this isnt a binary tree, which isnt a good solution to the task, its a list of lists. Also, your actual problem is in the next post, skip this if you do not care about the list of list example.


    OK as I gather it you want a menu across and down, so not a binary tree but a linked list of linked lists, effectively, and possibly subs of those to go across again, and down again, etc?

    And each one has a string, and whatever else inside it, a position on the screen maybe, and other graphics stuff.

    class menu
    {
    public: //ya ya you can private whatever you like later, this isnt a design its an example

    menu* down;
    menu* across;

    string menutxt;
    graphix_stuff gs;
    others moar;

    add_across(parameters);
    add_down(parameters);

    };

    and you get something like this:

    .. add_across(parameters)
    {
    across = new menu();
    across-> menutxt = parameters.mt;
    across-> gs = parameters.gs;
    etc;
    }

    Now, in your CTOR:
    menu()
    {
    across = 0;
    down = 0;
    //this saves a lot of setting them to null all the time.
    }

    -----------------

    then to use it, something like this.

    menu * mm; //main menu = mm. you can type it out in code, mm isnt the best name...

    mm = new menu(); //great, its across and down are null.

    menu->menutxt = "File";
    menu->down = new menu;
    menu->down-> menutxt = "Save";
    ...


    menu->across = new menu;
    menu->across->menutxt = "Edit";

    ....

    This gets ugly, note that after a bit its
    menu->across->across->across->across->...

    so you use iteration pointers:
    menu * mp;
    now, lets start totally over with MP and a new menu, so pretent MM is empty now.

    mp = menu; //ok same type
    mp-> menutxt = "file";
    mp-> across = new menu;
    mp = mp-> across;
    mp-> menutxt = "edit";
    mp-> across = new menu;
    mp = mp-> across;
    etc. Which is well suited to a function that takes the string, creates the new item, and such.

    All that, then to say this:

    addacross()
    {
    static bool firsttime = true;
    static menu * mp;
    if(firsttime)
    {
    firsttime = false;
    mp = this;
    mp->menutxt = parameter.txt;
    --- whatever else ---------
    }
    else
    {
    mp-> across = new menu;
    mp = mp-> across;
    mp-> menutxt = parameter.txt;
    -------whatever else ------------
    }
    }

    Again, this is just a guideline. You can do similar things in many ways --- including using an STL list of STL lists to handle all this sort of details for you.

    The main point is that there is only one object type, you take it across and down as needed, and delete it in reverse.

    Did all that make sense or help in any way?
    Last edited by jonnin; 06-01-2010 at 01:29 PM.

  7. #7
    Join Date
    Dec 2003
    Posts
    3,366
    finally:

    Menu[0].sub = new int[2];

    why is that an integer? You then try to do this
    Menu[0].sub[0].name = "Save"; (Clearly, int does not have a sub field!)

    That is your error. Perhaps, sub needs to be a menu* and all will be well?

  8. #8
    Join Date
    Apr 2010
    Posts
    5
    Now for really simple binary tree, as you do it, I'd personally write this:


    Code:
    #include <string>
    #include <boost/array.hpp>
    #include <boost/shared_ptr.hpp>
    
    using std::string;
    using boost::array;
    using boost::shared_ptr;
    
    struct Menu;
    
    typedef array<shared_ptr<Menu>,2> Duo;
    
    struct Menu
    {
        string name;
        Duo sub;
    };
    
    shared_ptr<Menu> CreateMenu(const string &name)
    {
        shared_ptr<Menu> menu(new Menu);
        menu->name = name;
        return menu;
    }
    
    Duo root;
    
    int main()
    {
    
        root[0] = CreateMenu("File");
        root[0]->sub[0] = CreateMenu("Save");
        root[0]->sub[1] = CreateMenu("Load");
    
        root[1] = CreateMenu("Tools");
        root[1]->sub[0] = CreateMenu("Shape");
        root[1]->sub[0]->sub[0] = CreateMenu("Square");
        root[1]->sub[0]->sub[1] = CreateMenu("Triangle");
        root[1]->sub[1] = CreateMenu("Spray");
        return 0;
    }
    But are you sure you want to use BINARY tree? That is quite awkward for generic menu (and also prone to typos ;-)
    Last edited by MirekCerny; 06-01-2010 at 01:05 PM.

  9. #9
    Join Date
    Dec 2003
    Posts
    3,366
    I don't think binary is the way to go, which is why I suggested (and hacked at) a list of lists instead. Its not even a tree at that point, really.

    Binary trees do a few things really well, this is not one of them =)

Similar Threads

  1. Replies: 0
    Last Post: 04-13-2010, 06:54 AM
  2. Replies: 0
    Last Post: 03-06-2009, 01:39 AM
  3. Tree Nodes
    By thealley in forum Java
    Replies: 5
    Last Post: 03-23-2008, 07:31 PM
  4. Replies: 0
    Last Post: 10-16-2002, 01:30 AM
  5. EMERGENT.... double linked list
    By mary in forum Java
    Replies: 0
    Last Post: 07-20-2001, 08:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center