Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Is anybody aware of generic tree for c# that has all of net4 goodies and is stable,fast snd tested. or some for java (maybe I could port it).(probably I'll just get use a google sanswer , but ...) Thanks in advance.
Posted
Comments
Monjurul Habib 30-May-11 17:24pm    
your question is not clear.

Just a followup on SAKryukovs' answer, how about:
public class TreeNode< T >
{
  public class ChildList : BindingList< TreeNode< T > >
  {}
  
  private ChildList children;
  private T data;

  public TreeNode()
  {
    children = new ChildList();
  }
  
  public TreeNode(T data)
    : this()
  {
    this.data = data;
  }
    
  public T Data
  {
    get
    {
      return data;
    }
    set
    {
      data = value;
    }
  } 

  public ChildList Children
  {
    get
    {
      return children;
    }
  } 

} 


Best regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-May-11 22:10pm    
Very similar variant, my 5. (Explicit Data getter/setter only needed for C#.v.2.0.)
--SA
Espen Harlinn 31-May-11 5:23am    
Thanks SAKryukov, it would be logical to add change notification on the setter - writing it like this was a small hint :)
You don't need a data special structure for the tree. Having such data type in a standard library would be absolutely redundant, because a tree is just a list of some elements which has a "children" member of the same type.

Here is the simplest example:

C#
using Tree = System.Collections.Generic.List<TreeNode>;

class TreeNodeData { /*...*/ }

class TreeNode {
    public TreeNodeData Data { get; set; }
    public Tree Children { get { return fChildren; } }
    public void AddChild(TreeNode child) { //lazily
        if (fChildren == null)
            fChildren = new Tree();
        fChildren.Add(child);
    } //AddChild

    //remove, delete by index, access by index, etc.

    // must not be constructed here, to avoid infinite recursion;
    // lazy construction required, see above
    Tree fChildren;
} //TreeNode

//...

MyTree tree = new Tree();
tree.Data = //...
tree.AddChild(new Tree());
//...


Got an idea?

—SA
 
Share this answer
 
v4
Comments
Monjurul Habib 30-May-11 17:25pm    
cool.my5
Sergey Alexandrovich Kryukov 30-May-11 17:25pm    
Thank you, Monjurul.
--SA
Espen Harlinn 30-May-11 18:43pm    
Good points, my 5
Sergey Alexandrovich Kryukov 30-May-11 22:06pm    
Thank you, Espen.
--SA
King.Duan 30-May-11 20:41pm    
nice work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900