using System;
using System.Collections.Generic;
using System.Text;namespace Algorithm
{
    class Tree<T> where T:System.IComparable<T>
    {
        private class Node
        {            private Node lChild;
            public Node LChild
            {
                get { return lChild; }
                set { LChild = value; }
            }            private Node rChild;
            public Node RChild
            {
                get { return rChild; }
                set { rChild = value; }
            }            private T data;
            public T Data
            {
                get { return data; }
                set { data = value; }
            }            public Node(T t)
            {
                data = t;
                lChild = null;
                rChild = null;
            }            public Node()
            {
                lChild = null;
                rChild = null;
            }
        }        private Node head;        public Tree()
        {
            head = new Node();
        }        public void AddNode(T t)
        {
            if (head.LChild == null)
            {
                Node node1 = new Node(t);
                head.LChild = node1;
                return;
            }
            Node tag = head;
            tag = tag.LChild;
            Node fatherNode = tag;
            while (tag != null)
            {
                if (t < tag.Data)
                {
                    fatherNode = tag;
                    tag = tag.LChild;
                }
                else if (t > tag.Data)
                {
                    fatherNode = tag;
                    tag = tag.RChild;
                }
                else if (t == tag.Data)
                {
                    Console.WriteLine("this data has existed.");
                }
            }            Node node2 = new Node(t);
            if (t < fatherNode.Data)
            {
                fatherNode.LChild = node2;
            }
            else if (t > fatherNode.Data)
            {
                fatherNode.RChild = node2;
            }
        }        public void Traverse()
        {
        }
    }    class Program
    {
        static void Main(string[] args)
        {        }
    }
}

解决方案 »

  1.   

    代码里有,比较大小不行如这段,出现了错误            while (tag != null)
                {
                    if (t < tag.Data)
                    {
                        fatherNode = tag;
                        tag = tag.LChild;
                    }
                    else if (t > tag.Data)
                    {
                        fatherNode = tag;
                        tag = tag.RChild;
                    }
                    else if (t == tag.Data)
                    {
                        Console.WriteLine("this data has existed.");
                    }
                } 说是泛型T和T无法比较
      

  2.   

    你重写一下T的Equals的方法,就可以啦!
      

  3.   

    t < fatherNode.Data
    这样的写法当然不行,要用IComparable <T>里的Compare方法比较。
      

  4.   

    如果你要用Tree<a>
    就必须定义a继承IComparableclass a : IComparable
    {    #region IComparable Members    int IComparable.CompareTo(object obj)
        {
            //throw new Exception("The method or operation is not implemented.");
        }    #endregion
    }
      

  5.   


    就是,在泛型算法中作比较应该使用CompareTo.
    要使用第一好的泛型算法,那么这个类就应该实现接口IComparable.
      

  6.   

    搞定了
    我写的
    不好
    using System;
    using System.Collections.Generic;
    using System.Text;namespace Algorithm
    {
        class Tree<T> where T:System.IComparable<T>
        {
            private class Node
            {            private Node next;
                public Node Next
                {
                    get { return next; }
                    set { next = value; }
                }            private Node rChild;
                public Node RChild
                {
                    get { return rChild; }
                    set { rChild = value; }
                }            private T data;
                public T Data
                {
                    get { return data; }
                    set { data = value; }
                }            public Node(T t)
                {
                    data = t;
                    next = null;
                    rChild = null;
                }            public Node()
                {
                    next = null;
                    rChild = null;
                }
            }        private Node head;        public int CompareTo(T t1, T t2)
            {
                return t1.CompareTo(t2);
            }        public Tree()
            {
                head = new Node();
            }        public void AddNode(T t)
            {
                Node tag = head.Next;
                Node pre = head;
                Node newNode = new Node(t);
                if (tag == null)
                {
                    pre.Next = newNode;
                    return;
                }
                while (tag != null)
                {
                    if (CompareTo(t, tag.Data) < 0)//插入的数据比目前的小 t < tag.data
                    {
                        newNode.Next = tag;
                        pre.Next = newNode;
                        return;
                    }
                    else if (CompareTo(t, tag.Data) == 0)
                    {
                        Console.WriteLine("this element exists.");
                        return;
                    }
                    else if (CompareTo(t, tag.Data) > 0)//插入的数据比目前的大 t > tag.data
                    {
                        pre = tag;
                        tag = tag.Next;
                    }
                }
                if (tag == null)
                {
                    pre.Next = newNode;
                }
            }        public void Traverse()
            {
                Node tag = head;
                tag = tag.Next;
                while (tag != null)
                {
                    Console.WriteLine(tag.Data);
                    tag = tag.Next;
                }
               
            }    }    class Program
        {
            static void Main(string[] args)
            {
                Tree<int> intTree = new Tree<int>();
                intTree.AddNode(1);
                intTree.AddNode(2);
                intTree.AddNode(3);
                intTree.AddNode(5);
                intTree.AddNode(4);
                intTree.Traverse();            Tree<string> strTree = new Tree<string>();
                strTree.AddNode("b");
                strTree.AddNode("d");
                strTree.AddNode("e");
                strTree.AddNode("a");
                strTree.AddNode("c");
                strTree.Traverse();            Console.ReadLine();
            }
        }
    }