Tree Structure ?

J

JezB

I have trawled through the System.Collections namespace looking for some
structure that will enable me to represent and manipulate tree structures,
as yet to no avail.

Of course I can represent a tree as a Hashtable with one part of the Value
of each entry pointing to the key of the parent, but it strikes me that this
is only usefull for drilling up from a child to a parent node - to find all
the children of a node I'd need to scan the whole table!

There must be a native tree-like structure with supporting methods to help
in tree-walking ... please someone point me to it !
 
J

JezB

Yes but they are in the Windows.Forms namespace. I need something more
generic since I'm writing library code that could conceivably used from
other types of project.

I imagine that I have to write my own set of classes to handle this - there
is a standard design pattern for representing trees - however I'm surprised
there's nothing native in the Collections namespace.
 
V

Vijaye Raji

An object collection can by itself act as a tree. This works because an
array of objects can be represented as an object too.

So, you can make a tree data structure out of just an array of objects.

-vJ
 
J

Jacob

I agree that it would be nice if there was such a collection. But really
Tree-style collections are just collections of collections. It should be a
walk in the park to write a simple class/object that holds collections of
types like itself. Implementing an ArrayList would work fine, but if you
want to write a custom strong-typed collection for your new object, try
CodeSmith .NET CollectionGen
http://www.sellsbrothers.com/tools/


Jacob
 
J

james

public class MyTreeNode
{

private MyTreeNode parent;
private IList children;

public MyTreeNode ( MyTreeNode aParent )
{
parent = aParent;
}
public bool IsRoot { get { return parent == null; } }
public bool HasChildren { get { if ( children != null && children.Count
0 ) return true; return false; } }
public Children { get { return children;} set { children = value; }
}



You can take it from here

JIM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top