Tree data structure in C#

前端 未结 20 2149
梦如初夏
梦如初夏 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 09:13

    Here is my implementation of BST

    class BST
    {
        public class Node
        {
            public Node Left { get; set; }
            public object Data { get; set; }
            public Node Right { get; set; }
    
            public Node()
            {
                Data = null;
            }
    
            public Node(int Data)
            {
                this.Data = (object)Data;
            }
    
            public void Insert(int Data)
            {
                if (this.Data == null)
                {
                    this.Data = (object)Data;
                    return;
                }
                if (Data > (int)this.Data)
                {
                    if (this.Right == null)
                    {
                        this.Right = new Node(Data);
                    }
                    else
                    {
                        this.Right.Insert(Data);
                    }
                }
                if (Data <= (int)this.Data)
                {
                    if (this.Left == null)
                    {
                        this.Left = new Node(Data);
                    }
                    else
                    {
                        this.Left.Insert(Data);
                    }
                }
            }
    
            public void TraverseInOrder()
            {
                if(this.Left != null)
                    this.Left.TraverseInOrder();
                Console.Write("{0} ", this.Data);
                if (this.Right != null)
                    this.Right.TraverseInOrder();
            }
        }
    
        public Node Root { get; set; }
        public BST()
        {
            Root = new Node();
        }
    }
    
    0 讨论(0)
  • 2020-11-22 09:14

    Here's a Tree

    public class Tree<T> : List<Tree<T>>
    {
        public  T Data { get; private set; }
    
        public Tree(T data)
        {
            this.Data = data;
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题