tags:

views:

634

answers:

2
+6  Q: 

Binary Tree in C#

Are there any objects in C# (or in .net) that represents a binary tree (or for curiosity) an n-ary tree?

I am not talking about presentation tree controls, but as model objects.

If not, are there any good external implementations?

+4  A: 

I'm not aware of one in the framework, but here is one implementation.

Yuriy Faktorovich
+8  A: 

The NGenerics project is a awesome collection of data structures and algorithms including a Binary Tree.

public class BinaryTree<T> : IVisitableCollection<T>, ITree<T>
{
  // Methods
  public void Add(BinaryTree<T> subtree);
  public virtual void breadthFirstTraversal(IVisitor<T> visitor);
  public virtual void 
         DepthFirstTraversal(OrderedVisitor<T> orderedVisitor);
  public BinaryTree<T> GetChild(int index);
  public bool Remove(BinaryTree<T> child);
  public virtual void RemoveLeft();
  public virtual void RemoveRight();

  // ...

  // Properties
  public virtual T Data { get; set; }
  public int Degree { get; }
  public virtual int Height { get; }
  public virtual bool IsLeafNode { get; }
  public BinaryTree<T> this[int i] { get; }
  public virtual BinaryTree<T> Left { get; set; }
  public virtual BinaryTree<T> Right { get; set; }

  // ...
}
Bob
Thanks, very interesting :) I had never heard of RedBlackTree or the SplayTree.
Russell
Bob