Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to constructor a storage class to store Tree data in C++

How to constructor a storage class to store Tree data in C++

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialc++data-structures
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shanmugarajaa
    wrote on last edited by
    #1

    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

    L O S V 4 Replies Last reply
    0
    • S shanmugarajaa

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You could just use a Treeview control, which has all the functionality built in.

      S O 2 Replies Last reply
      0
      • L Lost User

        You could just use a Treeview control, which has all the functionality built in.

        S Offline
        S Offline
        shanmugarajaa
        wrote on last edited by
        #3

        Actually in want to store it in vector or list or any other data structure. For that, I want to constructor class.

        L 1 Reply Last reply
        0
        • S shanmugarajaa

          Actually in want to store it in vector or list or any other data structure. For that, I want to constructor class.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Then you need to think about the actual requirements of your class and create a design that will match those requirements. With such a vague question it is impossible to suggest much more.

          1 Reply Last reply
          0
          • S shanmugarajaa

            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

            O Offline
            O Offline
            Orjan Westin
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • L Lost User

              You could just use a Treeview control, which has all the functionality built in.

              O Offline
              O Offline
              Orjan Westin
              wrote on last edited by
              #6

              That's a lot of overhead, presumes a Windows platform, and pulls in a lot of GUI-related dependencies just to store some data.

              L 1 Reply Last reply
              0
              • S shanmugarajaa

                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

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                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)

                1 Reply Last reply
                0
                • O Orjan Westin

                  That's a lot of overhead, presumes a Windows platform, and pulls in a lot of GUI-related dependencies just to store some data.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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.

                  O 1 Reply Last reply
                  0
                  • L Lost User

                    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.

                    O Offline
                    O Offline
                    Orjan Westin
                    wrote on last edited by
                    #9

                    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.

                    L 1 Reply Last reply
                    0
                    • O Orjan Westin

                      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.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      All I did in my initial response was to suggest a possible solution to what was a fairly vague question. I leave it to the OP to decide whether that helps to solve their problem.

                      1 Reply Last reply
                      0
                      • S shanmugarajaa

                        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

                        V Offline
                        V Offline
                        Vaclav Naydenov
                        wrote on last edited by
                        #11

                        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;
                        }

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups