Tree class recommendation
-
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->chlinkI 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) -
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->chlinkI 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)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)
-
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)
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) -
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) -
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)
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"
-
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)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"
-
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->chlinkI 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)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
-
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"
-
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)
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) -
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)