Tree data structure in C#

前端 未结 20 2501
梦如初夏
梦如初夏 2020-11-22 08:30

I was looking for a tree or graph data structure in C# but I guess there isn\'t one provided. An Extensive Examination of Data Structures Using C# 2.0 explains a bit about w

20条回答
  •  無奈伤痛
    2020-11-22 08:53

    Try this simple sample.

    public class TreeNode
    {
        #region Properties
        public TValue Value { get; set; }
        public List> Children { get; private set; }
        public bool HasChild { get { return Children.Any(); } }
        #endregion
        #region Constructor
        public TreeNode()
        {
            this.Children = new List>();
        }
        public TreeNode(TValue value)
            : this()
        {
            this.Value = value;
        }
        #endregion
        #region Methods
        public void AddChild(TreeNode treeNode)
        {
            Children.Add(treeNode);
        }
        public void AddChild(TValue value)
        {
            var treeNode = new TreeNode(value);
            AddChild(treeNode);
        }
        #endregion
    }
    

提交回复
热议问题