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
Re: creation of simple tree like structure using stl vector c++, need help : )
Interesting structure! Do it! :thumb:
And, please, next time attach your image:
Attachment 36055
to your post!
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/ ]
Re: creation of simple tree like structure using stl vector c++, need help : )
Quote:
Originally Posted by
vlnikolic
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.
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?
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.
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?
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