C# 表示树的对象

C# 表示树的对象,c#,.net,tree,binary-tree,C#,.net,Tree,Binary Tree,C#(或.net)中是否有表示二叉树(或出于好奇)和n元树的对象 我说的不是表示树控件,而是模型对象 如果没有,是否有好的外部实现?我不知道框架中有一个,但它是一个实现。该项目是一个很棒的数据结构和算法集合,包括一个 公共类二进制树:IVisitableCollection,ITree { //方法 公共void Add(二叉树子树); 公共虚拟空间宽度优先遍历(IVisitor visitor visitor); 公共虚拟空间 深度第一次遍历(OrderedVisitor OrderedVis

C#(或.net)中是否有表示二叉树(或出于好奇)和n元树的对象

我说的不是表示树控件,而是模型对象


如果没有,是否有好的外部实现?

我不知道框架中有一个,但它是一个实现。

该项目是一个很棒的数据结构和算法集合,包括一个

公共类二进制树:IVisitableCollection,ITree { //方法 公共void Add(二叉树子树); 公共虚拟空间宽度优先遍历(IVisitor visitor visitor); 公共虚拟空间 深度第一次遍历(OrderedVisitor OrderedVisitor); 公共二叉树GetChild(int索引); 公共bool移除(二进制树子级); 公共虚拟void RemoveLeft(); 公共虚拟无效清除器(); // ... //性质 公共虚拟T数据{get;set;} 公共整数度{get;} 公共虚拟整数高度{get;} 公共虚拟布尔IsLeafNode{get;} 公共二叉树this[int i]{get;} 公共虚拟二进制树左{get;set;} 公共虚拟二进制树权限{get;set;} // ... } 我尝试了一个简单(非平衡)的二进制搜索树

public sealed class BinarySearchTree<T> : IEnumerable<T>
{
   private readonly IComparer<T> _comparer;
   public BinaryTreeNode<T> Root { get; private set; }

   public BinarySearchTree()
   {    
   }

   public BinarySearchTree(IEnumerable<T> collection) : 
       this(collection, Comparer<T>.Default)
   {
   }

   public BinarySearchTree(IEnumerable<T> collection, IComparer<T> comparer)
   {
       if (collection == null) throw new ArgumentNullException("collection");
       if (comparer == null) throw new ArgumentNullException("comparer");

       _comparer = comparer;
       foreach (var item in collection)
           Add(item);
   }

   public BinarySearchTree(BinaryTreeNode<T> root)
   {
       Root = root;
   }

   public void Add(T val)   
   {    
       var newNode = new BinaryTreeNode<T>(val);
       if (Root == null)
       {
           Root = newNode;
           return;
       }

       Add(Root, newNode);  
   }

   void Add(BinaryTreeNode<T> root, BinaryTreeNode<T> node)
   {
       if (_comparer.Compare(node.Value, root.Value) <= 0)
       {
           if (root.Left == null)
               root.Left = node;
           else
               Add(root.Left, node);
       }
       else //node.Value > root.Value
       {
           if (root.Right == null)
               root.Right = node;
           else
               Add(root.Right, node);   
       }
   }

   public bool Contains(T val)
   {
       return Contains(Root, val);
   }

   bool Contains(BinaryTreeNode<T> node, T val)
   {
       if (node == null) 
           return false;

       var comparison = _comparer.Compare(val, node.Value);
       if (comparison == 0) //val = node.value
           return true;
       else if (comparison < 0) //val < node.Value
           return Contains(node.Left, val);
       else //val > node.Value
           return Contains(node.Right, val);
   }

   public T GetMinimum()
   {
       if (Root == null)
           return default(T);

       var node = Root;
       while (node.Left != null)
           node = node.Left;

       return node.Value;       
   }

   public T GetMaximum()
   {
       if (Root == null)
           return default(T);

       var node = Root;
       while (node.Right != null)
           node = node.Right;

       return node.Value;       
   }

   public IEnumerator<T> GetEnumerator()
   {
       return new BinaryTreeEnumerator<T>(Root);
   }

   IEnumerator IEnumerable.GetEnumerator()
   {
       return GetEnumerator();
   }
}

public sealed class BinaryTreeNode<T>
{
    public BinaryTreeNode<T> Left {get; set;}
    public BinaryTreeNode<T> Right {get; set;}      
    public T Value {get; private set;}

    public BinaryTreeNode(T val)
    {
        Value = val;
    }
}

public sealed class BinaryTreeEnumerator<T> : IEnumerator<T>
{
    private Stack<BinaryTreeNode<T>> _stack = new Stack<BinaryTreeNode<T>>();
    public T Current { get; private set; }

    public BinaryTreeEnumerator(BinaryTreeNode<T> root)
    {
        if (root == null)
            return; //empty root = Enumerable.Empty<T>()

        PushLeftBranch(root);
    }

    public void Dispose()
    {
        _stack = null; //help GC
    }

    public bool MoveNext()
    {
        if (_stack.Count == 0)
            return false;

        var node = _stack.Pop();
        Current = node.Value;

        if (node.Right != null)
            PushLeftBranch(node.Right);

        return true;
    }

    private void PushLeftBranch(BinaryTreeNode<T> node)
    {
        while (node != null)
        {
            _stack.Push(node);
            node = node.Left;
        }
    }

    public void Reset()
    {
        _stack.Clear();
    }

    object IEnumerator.Current
    {
        get { return Current; }
    }
}
公共密封类二进制搜索树:IEnumerable
{
专用只读IComparer\u comparer;
公共二进制树节点根{get;private set;}
公共二进制搜索树()
{    
}
公共二进制搜索树(IEnumerable集合):
此(集合、比较器、默认值)
{
}
公共二进制搜索树(IEnumerable集合、IComparer比较器)
{
如果(collection==null)抛出新的ArgumentNullException(“collection”);
如果(comparer==null)抛出新的ArgumentNullException(“comparer”);
_比较器=比较器;
foreach(集合中的var项)
增加(项目);
}
公共二进制搜索树(二进制树节点根)
{
根=根;
}
公共无效添加(T val)
{    
var newNode=newbinarytreenode(val);
if(Root==null)
{
根=新节点;
返回;
}
添加(根,新节点);
}
void Add(二进制树节点根,二进制树节点)
{
如果(_comparer.Compare(node.Value,root.Value)root.Value
{
if(root.Right==null)
root.Right=节点;
其他的
添加(root.Right,node);
}
}
公共布尔包含(T val)
{
返回包含(根,val);
}
布尔包含(二进制树节点,T val)
{
if(node==null)
返回false;
var比较=_comparer.Compare(val,node.Value);
如果(比较==0)//val=node.value
返回true;
else if(比较<0)//valnode.Value
返回包含(node.Right,val);
}
公共T GetMinimum()
{
if(Root==null)
返回默认值(T);
var节点=根;
while(node.Left!=null)
node=node.Left;
返回节点值;
}
公共T GetMaximum()
{
if(Root==null)
返回默认值(T);
var节点=根;
while(node.Right!=null)
node=node.Right;
返回节点值;
}
公共IEnumerator GetEnumerator()
{
返回新的二进制树计数器(根);
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
}
公共密封类二叉树烯
{
公共二进制树节点左{get;set;}
公共二进制树节点右{get;set;}
公共T值{get;私有集;}
公共二进制树节点(T val)
{
值=val;
}
}
公共密封类二进制树计数器:IEnumerator
{
私有堆栈_Stack=新堆栈();
公共T当前{get;私有集;}
公共BinarytreeNumerator(BinaryTreeNode根)
{
if(root==null)
return;//empty root=Enumerable.empty()
侧枝(根);
}
公共空间处置()
{
_stack=null;//帮助GC
}
公共图书馆
{
如果(_stack.Count==0)
返回false;
var节点=_stack.Pop();
当前=节点值;
if(node.Right!=null)
PushLeftBranch(node.Right);
返回true;
}
私有void PushLeftBranch(二进制树节点)
{
while(节点!=null)
{
_栈推(节点);
node=node.Left;
}
}
公共无效重置()
{
_stack.Clear();
}
对象IEnumerator.Current
{
获取{返回当前;}
}
}

我来晚了,但我说这两个词都是基于树的。

谢谢,非常有趣:)我从来没有听说过红黑树或八字树。没问题,在这种情况下,我强烈推荐这样一本数据结构和算法书,如果它实现IEnumerable接口,从而与LINQ和数据结构转换兼容,那么它将非常有用
public sealed class BinarySearchTree<T> : IEnumerable<T>
{
   private readonly IComparer<T> _comparer;
   public BinaryTreeNode<T> Root { get; private set; }

   public BinarySearchTree()
   {    
   }

   public BinarySearchTree(IEnumerable<T> collection) : 
       this(collection, Comparer<T>.Default)
   {
   }

   public BinarySearchTree(IEnumerable<T> collection, IComparer<T> comparer)
   {
       if (collection == null) throw new ArgumentNullException("collection");
       if (comparer == null) throw new ArgumentNullException("comparer");

       _comparer = comparer;
       foreach (var item in collection)
           Add(item);
   }

   public BinarySearchTree(BinaryTreeNode<T> root)
   {
       Root = root;
   }

   public void Add(T val)   
   {    
       var newNode = new BinaryTreeNode<T>(val);
       if (Root == null)
       {
           Root = newNode;
           return;
       }

       Add(Root, newNode);  
   }

   void Add(BinaryTreeNode<T> root, BinaryTreeNode<T> node)
   {
       if (_comparer.Compare(node.Value, root.Value) <= 0)
       {
           if (root.Left == null)
               root.Left = node;
           else
               Add(root.Left, node);
       }
       else //node.Value > root.Value
       {
           if (root.Right == null)
               root.Right = node;
           else
               Add(root.Right, node);   
       }
   }

   public bool Contains(T val)
   {
       return Contains(Root, val);
   }

   bool Contains(BinaryTreeNode<T> node, T val)
   {
       if (node == null) 
           return false;

       var comparison = _comparer.Compare(val, node.Value);
       if (comparison == 0) //val = node.value
           return true;
       else if (comparison < 0) //val < node.Value
           return Contains(node.Left, val);
       else //val > node.Value
           return Contains(node.Right, val);
   }

   public T GetMinimum()
   {
       if (Root == null)
           return default(T);

       var node = Root;
       while (node.Left != null)
           node = node.Left;

       return node.Value;       
   }

   public T GetMaximum()
   {
       if (Root == null)
           return default(T);

       var node = Root;
       while (node.Right != null)
           node = node.Right;

       return node.Value;       
   }

   public IEnumerator<T> GetEnumerator()
   {
       return new BinaryTreeEnumerator<T>(Root);
   }

   IEnumerator IEnumerable.GetEnumerator()
   {
       return GetEnumerator();
   }
}

public sealed class BinaryTreeNode<T>
{
    public BinaryTreeNode<T> Left {get; set;}
    public BinaryTreeNode<T> Right {get; set;}      
    public T Value {get; private set;}

    public BinaryTreeNode(T val)
    {
        Value = val;
    }
}

public sealed class BinaryTreeEnumerator<T> : IEnumerator<T>
{
    private Stack<BinaryTreeNode<T>> _stack = new Stack<BinaryTreeNode<T>>();
    public T Current { get; private set; }

    public BinaryTreeEnumerator(BinaryTreeNode<T> root)
    {
        if (root == null)
            return; //empty root = Enumerable.Empty<T>()

        PushLeftBranch(root);
    }

    public void Dispose()
    {
        _stack = null; //help GC
    }

    public bool MoveNext()
    {
        if (_stack.Count == 0)
            return false;

        var node = _stack.Pop();
        Current = node.Value;

        if (node.Right != null)
            PushLeftBranch(node.Right);

        return true;
    }

    private void PushLeftBranch(BinaryTreeNode<T> node)
    {
        while (node != null)
        {
            _stack.Push(node);
            node = node.Left;
        }
    }

    public void Reset()
    {
        _stack.Clear();
    }

    object IEnumerator.Current
    {
        get { return Current; }
    }
}