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#
  4. Tree class recommendation

Tree class recommendation

Scheduled Pinned Locked Moved C#
csharpcomdata-structurestutorial
10 Posts 4 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.
  • D Offline
    D Offline
    DaveyM69
    wrote on last edited by
    #1

    Hi, I need to create a data structure that is very tree like that operates similar to a directory structure. For example: /ch - will add "ch" to root /ch/01/config - will add "01" to ch from above and "config" to 01 /ch/02/insert - will add "02" to ch from first line and "insert" to 02 /ch/01/mix/01 - will add "mix" to 01 from second line and "01" to mix /config/chlink - will add "config" to root and "chlink" to config which will result in a Tree layout like:

    root->ch ->01 ->config
    ->mix ->01
    ->02 ->insert
    ->config->chlink

    I have been playing with this for a while now and just can't get anything I write to function in this way. If anyone could point me in the right direction it would be much appreciated!

    Dave
    Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

    L M 2 Replies Last reply
    0
    • D DaveyM69

      Hi, I need to create a data structure that is very tree like that operates similar to a directory structure. For example: /ch - will add "ch" to root /ch/01/config - will add "01" to ch from above and "config" to 01 /ch/02/insert - will add "02" to ch from first line and "insert" to 02 /ch/01/mix/01 - will add "mix" to 01 from second line and "01" to mix /config/chlink - will add "config" to root and "chlink" to config which will result in a Tree layout like:

      root->ch ->01 ->config
      ->mix ->01
      ->02 ->insert
      ->config->chlink

      I have been playing with this for a while now and just can't get anything I write to function in this way. If anyone could point me in the right direction it would be much appreciated!

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

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

      DaveyM69 wrote:

      If anyone could point me in the right direction it would be much appreciated!

      You'd need a class that can hold a reference to itself (the parent) and a collection of its childeren. Like below;

      using System;
      using System.Collections.ObjectModel;
      using System.Linq;
      using System.Text;

      class Program
      {
      public class Folder
      {
      Collection _children;
      public Folder Parent { get; private set; }
      public string Name { get; set; }
      public Collection Children { get { return _children; } }

          private Folder(Folder parent, string name)
          {
              \_children = new Collection();
              Parent = parent;
              Name = name;
          }
      
          public static Folder CreateNewRoot()
          {
              return new Folder(null, "Root");
          }
      
          public Folder CreateFolder(string name)
          {
              var result = new Folder(this, name);
              this.Children.Add(result);
              return result;
          }
      
          public override string ToString()
          {
              Folder currentFolder = this;
      
              var pathBuilder = new StringBuilder();
              while (currentFolder != null)
              {
                  pathBuilder.Insert(0, currentFolder.Name);
                  pathBuilder.Insert(0, '/');
                  currentFolder = currentFolder.Parent;
              }
      
              return pathBuilder.ToString();
          }
      }
      
      static Folder CreatePath(Folder startingFolder, string path)
      {
          string\[\] pathParts = path.Split(new\[\] { '/' }, StringSplitOptions.RemoveEmptyEntries);
          Folder currentFolder = startingFolder;
          for (int i = 0; i < pathParts.Length; i++)
          {
              string targetName = pathParts\[i\];
              // does current folder hold our target?
              Folder targetFolder = null;
              if (0 != currentFolder.Children.Count())
                  targetFolder = currentFolder.Children.FirstOrDefault(f => f.Name == targetName);
              if (null == targetFolder)
              {
                  // it don't exist yet
                  targetFolder = currentFolder.CreateFolder(targetName);
              }
              // on to the next one
              currentFolder = targetFolder;
          }
      
          return currentFolder;
      } 
      
      static void DumpFolderToConsole(Folder fromWhere)
      {
          Console.WriteLine(fromWhere)
      
      D B 3 Replies Last reply
      0
      • L Lost User

        DaveyM69 wrote:

        If anyone could point me in the right direction it would be much appreciated!

        You'd need a class that can hold a reference to itself (the parent) and a collection of its childeren. Like below;

        using System;
        using System.Collections.ObjectModel;
        using System.Linq;
        using System.Text;

        class Program
        {
        public class Folder
        {
        Collection _children;
        public Folder Parent { get; private set; }
        public string Name { get; set; }
        public Collection Children { get { return _children; } }

            private Folder(Folder parent, string name)
            {
                \_children = new Collection();
                Parent = parent;
                Name = name;
            }
        
            public static Folder CreateNewRoot()
            {
                return new Folder(null, "Root");
            }
        
            public Folder CreateFolder(string name)
            {
                var result = new Folder(this, name);
                this.Children.Add(result);
                return result;
            }
        
            public override string ToString()
            {
                Folder currentFolder = this;
        
                var pathBuilder = new StringBuilder();
                while (currentFolder != null)
                {
                    pathBuilder.Insert(0, currentFolder.Name);
                    pathBuilder.Insert(0, '/');
                    currentFolder = currentFolder.Parent;
                }
        
                return pathBuilder.ToString();
            }
        }
        
        static Folder CreatePath(Folder startingFolder, string path)
        {
            string\[\] pathParts = path.Split(new\[\] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            Folder currentFolder = startingFolder;
            for (int i = 0; i < pathParts.Length; i++)
            {
                string targetName = pathParts\[i\];
                // does current folder hold our target?
                Folder targetFolder = null;
                if (0 != currentFolder.Children.Count())
                    targetFolder = currentFolder.Children.FirstOrDefault(f => f.Name == targetName);
                if (null == targetFolder)
                {
                    // it don't exist yet
                    targetFolder = currentFolder.CreateFolder(targetName);
                }
                // on to the next one
                currentFolder = targetFolder;
            }
        
            return currentFolder;
        } 
        
        static void DumpFolderToConsole(Folder fromWhere)
        {
            Console.WriteLine(fromWhere)
        
        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        Thanks Eddy, I'll try this tomorrow :thumbsup: It's actually to hold and look up against OSC[^] addresses for a particular device. I don't need all the wild card stuff thankfully so with what you've given me I should be close. Thanks again :)

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        L B 2 Replies Last reply
        0
        • D DaveyM69

          Thanks Eddy, I'll try this tomorrow :thumbsup: It's actually to hold and look up against OSC[^] addresses for a particular device. I don't need all the wild card stuff thankfully so with what you've given me I should be close. Thanks again :)

          Dave
          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

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

          You're welcome, and good luck :)

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

          1 Reply Last reply
          0
          • L Lost User

            DaveyM69 wrote:

            If anyone could point me in the right direction it would be much appreciated!

            You'd need a class that can hold a reference to itself (the parent) and a collection of its childeren. Like below;

            using System;
            using System.Collections.ObjectModel;
            using System.Linq;
            using System.Text;

            class Program
            {
            public class Folder
            {
            Collection _children;
            public Folder Parent { get; private set; }
            public string Name { get; set; }
            public Collection Children { get { return _children; } }

                private Folder(Folder parent, string name)
                {
                    \_children = new Collection();
                    Parent = parent;
                    Name = name;
                }
            
                public static Folder CreateNewRoot()
                {
                    return new Folder(null, "Root");
                }
            
                public Folder CreateFolder(string name)
                {
                    var result = new Folder(this, name);
                    this.Children.Add(result);
                    return result;
                }
            
                public override string ToString()
                {
                    Folder currentFolder = this;
            
                    var pathBuilder = new StringBuilder();
                    while (currentFolder != null)
                    {
                        pathBuilder.Insert(0, currentFolder.Name);
                        pathBuilder.Insert(0, '/');
                        currentFolder = currentFolder.Parent;
                    }
            
                    return pathBuilder.ToString();
                }
            }
            
            static Folder CreatePath(Folder startingFolder, string path)
            {
                string\[\] pathParts = path.Split(new\[\] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                Folder currentFolder = startingFolder;
                for (int i = 0; i < pathParts.Length; i++)
                {
                    string targetName = pathParts\[i\];
                    // does current folder hold our target?
                    Folder targetFolder = null;
                    if (0 != currentFolder.Children.Count())
                        targetFolder = currentFolder.Children.FirstOrDefault(f => f.Name == targetName);
                    if (null == targetFolder)
                    {
                        // it don't exist yet
                        targetFolder = currentFolder.CreateFolder(targetName);
                    }
                    // on to the next one
                    currentFolder = targetFolder;
                }
            
                return currentFolder;
            } 
            
            static void DumpFolderToConsole(Folder fromWhere)
            {
                Console.WriteLine(fromWhere)
            
            B Offline
            B Offline
            BillWoodruff
            wrote on last edited by
            #5

            For this kind of code sample, I wish I had more than one up-vote :) thanks, Bill

            «To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"

            L 1 Reply Last reply
            0
            • D DaveyM69

              Thanks Eddy, I'll try this tomorrow :thumbsup: It's actually to hold and look up against OSC[^] addresses for a particular device. I don't need all the wild card stuff thankfully so with what you've given me I should be close. Thanks again :)

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              B Offline
              B Offline
              BillWoodruff
              wrote on last edited by
              #6

              I think Eddy V. has given you a great code-example. If you end up needing a very fancy Node structure that keeps track of ancestors, descendants, etc., this is a good resource: [^]. I came across this code for 'Node a few years ago when reading this thread on StackOverflow: [^] which has several code examples for different types of Tree data-structures in C#. cheers, Bill

              «To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"

              1 Reply Last reply
              0
              • D DaveyM69

                Hi, I need to create a data structure that is very tree like that operates similar to a directory structure. For example: /ch - will add "ch" to root /ch/01/config - will add "01" to ch from above and "config" to 01 /ch/02/insert - will add "02" to ch from first line and "insert" to 02 /ch/01/mix/01 - will add "mix" to 01 from second line and "01" to mix /config/chlink - will add "config" to root and "chlink" to config which will result in a Tree layout like:

                root->ch ->01 ->config
                ->mix ->01
                ->02 ->insert
                ->config->chlink

                I have been playing with this for a while now and just can't get anything I write to function in this way. If anyone could point me in the right direction it would be much appreciated!

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                You might also want to take a look as SQL Servers HierarchyID[^] structure, I use a string format of that for my NodeKey whenever I have to maintain a tree structure.

                Never underestimate the power of human stupidity RAH

                1 Reply Last reply
                0
                • B BillWoodruff

                  For this kind of code sample, I wish I had more than one up-vote :) thanks, Bill

                  «To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"

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

                  Thanks :)

                  1 Reply Last reply
                  0
                  • L Lost User

                    DaveyM69 wrote:

                    If anyone could point me in the right direction it would be much appreciated!

                    You'd need a class that can hold a reference to itself (the parent) and a collection of its childeren. Like below;

                    using System;
                    using System.Collections.ObjectModel;
                    using System.Linq;
                    using System.Text;

                    class Program
                    {
                    public class Folder
                    {
                    Collection _children;
                    public Folder Parent { get; private set; }
                    public string Name { get; set; }
                    public Collection Children { get { return _children; } }

                        private Folder(Folder parent, string name)
                        {
                            \_children = new Collection();
                            Parent = parent;
                            Name = name;
                        }
                    
                        public static Folder CreateNewRoot()
                        {
                            return new Folder(null, "Root");
                        }
                    
                        public Folder CreateFolder(string name)
                        {
                            var result = new Folder(this, name);
                            this.Children.Add(result);
                            return result;
                        }
                    
                        public override string ToString()
                        {
                            Folder currentFolder = this;
                    
                            var pathBuilder = new StringBuilder();
                            while (currentFolder != null)
                            {
                                pathBuilder.Insert(0, currentFolder.Name);
                                pathBuilder.Insert(0, '/');
                                currentFolder = currentFolder.Parent;
                            }
                    
                            return pathBuilder.ToString();
                        }
                    }
                    
                    static Folder CreatePath(Folder startingFolder, string path)
                    {
                        string\[\] pathParts = path.Split(new\[\] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                        Folder currentFolder = startingFolder;
                        for (int i = 0; i < pathParts.Length; i++)
                        {
                            string targetName = pathParts\[i\];
                            // does current folder hold our target?
                            Folder targetFolder = null;
                            if (0 != currentFolder.Children.Count())
                                targetFolder = currentFolder.Children.FirstOrDefault(f => f.Name == targetName);
                            if (null == targetFolder)
                            {
                                // it don't exist yet
                                targetFolder = currentFolder.CreateFolder(targetName);
                            }
                            // on to the next one
                            currentFolder = targetFolder;
                        }
                    
                        return currentFolder;
                    } 
                    
                    static void DumpFolderToConsole(Folder fromWhere)
                    {
                        Console.WriteLine(fromWhere)
                    
                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #9

                    Well thanks again Eddy, it's done the job brilliantly with only a few minor tweaks to allow for use in my situation! Awesome :)

                    Dave
                    Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                    L 1 Reply Last reply
                    0
                    • D DaveyM69

                      Well thanks again Eddy, it's done the job brilliantly with only a few minor tweaks to allow for use in my situation! Awesome :)

                      Dave
                      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

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

                      You're welcome, and thanks :)

                      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