How to constructor a storage class to store Tree data in C++
-
Dear Friends, I want to constructor a storage class, which should have the ability to store Tree data in tree hierarchy manner. For example: Like Root folder -->sub folders---> sub folders --> files. your suggestion and guidance are highly appreciate. Thanks and Regards, S Shanmuga Raja
-
Dear Friends, I want to constructor a storage class, which should have the ability to store Tree data in tree hierarchy manner. For example: Like Root folder -->sub folders---> sub folders --> files. your suggestion and guidance are highly appreciate. Thanks and Regards, S Shanmuga Raja
-
Actually in want to store it in vector or list or any other data structure. For that, I want to constructor class.
-
Actually in want to store it in vector or list or any other data structure. For that, I want to constructor class.
-
Dear Friends, I want to constructor a storage class, which should have the ability to store Tree data in tree hierarchy manner. For example: Like Root folder -->sub folders---> sub folders --> files. your suggestion and guidance are highly appreciate. Thanks and Regards, S Shanmuga Raja
Assuming you have string leafs (e.g. file names) in your branches, the minimal structure is:
struct StringTreeNode
{
std::string name;
std::vector<StringTreeNode> branches;
}A leaf is just a branch with no subnodes.
-
That's a lot of overhead, presumes a Windows platform, and pulls in a lot of GUI-related dependencies just to store some data.
-
Dear Friends, I want to constructor a storage class, which should have the ability to store Tree data in tree hierarchy manner. For example: Like Root folder -->sub folders---> sub folders --> files. your suggestion and guidance are highly appreciate. Thanks and Regards, S Shanmuga Raja
Check out http://tree.phi-sci.com/index.html[^]. The documentation offers some insight into its proper use with the two types of iterators it provides.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
That's a lot of overhead, presumes a Windows platform, and pulls in a lot of GUI-related dependencies just to store some data.
-
Not so, it's a quite simple way of creating the structure that the OP asked about. There is no GUI overhead if the control is never displayed.
There is code size overhead for pulling in a GUI library which may not be needed. For instance, a in a command-line application, linking in Treeview (and all classes it depends on) could significantly affect the size of the executable, the time to start up, and it would also affect portability of the code.
-
There is code size overhead for pulling in a GUI library which may not be needed. For instance, a in a command-line application, linking in Treeview (and all classes it depends on) could significantly affect the size of the executable, the time to start up, and it would also affect portability of the code.
-
Dear Friends, I want to constructor a storage class, which should have the ability to store Tree data in tree hierarchy manner. For example: Like Root folder -->sub folders---> sub folders --> files. your suggestion and guidance are highly appreciate. Thanks and Regards, S Shanmuga Raja
You can use a struct or a class with nested vector of same type structs:
#include <vector>
#include <string>
#include <iostream>struct TreeNode {
std::string payload;
std::vector<TreeNode> children;
TreeNode(const std::string &p): payload(p) {}
TreeNode() {} // it's a must if we are to store nodes in a vector
};void walk_tree(TreeNode &node, int indent = 0) {
std::cout << std::string(indent * 4, ' ')
<< node.payload << std::endl;
for (size_t i = 0; i < node.children.size(); ++i)
walk_tree(node.children[i], indent + 1);
}int main() {
TreeNode a("home");
a.children.push_back(TreeNode("peter"));
a.children.push_back(TreeNode("jane"));
a.children[0].children.push_back(TreeNode("game.exe"));
a.children[1].children.push_back(TreeNode("homework.txt"));
walk_tree(a);
return 0;
}