CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2009
    Posts
    1

    creation of simple tree like structure using stl vector c++, need help : )

    hello everybody

    i would like to create a tree like structure in c++. since the number of children per node is arbitrary i would like to use stl vector for tree creation.

    tree should look something like this:

    https://i.postimg.cc/8zzT6jvJ/Diagram1.png

    also

    each node should contain some data, including one stl vector.

    at the end i would like to be able to "merge" hierarchically in one large vector - something like this:

    K-vector + I-vector + F-vector + B-vector + A-vector

    thank you very much for your interest

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: creation of simple tree like structure using stl vector c++, need help : )

    Interesting structure! Do it!

    And, please, next time attach your image:
    Attachment 36055
    to your post!
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: creation of simple tree like structure using stl vector c++, need help : )

    This is a multi-node or general tree. There's various implementations/info about this on the Internet. Eg:

    https://www.geeksforgeeks.org/generi...der-traversal/
    https://www.codeproject.com/Articles...-Multi-node-Da

    and loads of others...

    [Also asked here https://cplusplus.com/forum/general/284864/ ]
    Last edited by 2kaud; September 25th, 2022 at 03:57 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Feb 2017
    Posts
    677

    Re: creation of simple tree like structure using stl vector c++, need help : )

    Quote Originally Posted by vlnikolic View Post
    i would like to create a tree like structure in c++. since the number of children per node is arbitrary i would like to use stl vector for tree creation.
    You could start from an implementation of a binary tree. There are plenty available on the internet and in textbooks.

    In the node declaration, replace the left and right child node pointers with a std::vector of the same pointer type. Then modify the code to use the 0'th and 1'st vector index positions for the left and right pointers, respectively. When this works, you can easily generalize the code to handle an arbitrary number of child pointers in each node by looping over the vector.

    As the last step, if the pointers are low-level, I suggest you replace them with smart pointers. That is std::unique_ptr or std::shared_ptr. In this way, you need not worry about memory leaks from not having deleted discarded node objects properly.
    Last edited by wolle; September 26th, 2022 at 02:40 AM.

  5. #5
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: creation of simple tree like structure using stl vector c++, need help : )

    Really, the more important question is when inserting data into the tree, what is the criteria for creating a new node - or to traverse existing nodes for insertion?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Oct 2022
    Posts
    2

    Re: creation of simple tree like structure using stl vector c++, need help : )

    i would like to create a tree like structure in c++. since the number of children per node is arbitrary i would like to use stl vector for tree creation.

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: creation of simple tree like structure using stl vector c++, need help : )

    That's the OP question - which has been answered above. What further info are you after?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Oct 2022
    Posts
    2

    Re: creation of simple tree like structure using stl vector c++, need help : )

    each node should contain some data, including one stl vector.

    at the end i would like to be able to "merge" hierarchically in one large vector - something like this:

    K-vector + I-vector + F-vector + B-vector + A-vector tech guides get-mobdrovip.com

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured