将一组单词插入二叉排序树(相当于排序),中序遍历即可输出排序后的情况。下面的代码有误,想请教一下插入部分怎么写?
public class Node<T>
{
private T data;
private Node<T> lChild;
private Node<T> rChild; public Node(T val, Node<T> lp, Node<T> rp)
{
data = val;
lChild = lp;
rChild = rp;
} public Node(Node<T> lp, Node<T> rp)
{
data = default(T);
lChild = lp;
rChild = rp;
} public Node(T val)
{
data = val;
lChild = null;
rChild = null;
} public Node()
{
data = default(T);
lChild = null;
rChild = null;
} public T Data
{
get
{
return data;
}
set
{
value = data;
}
} public Node<T> LChild
{
get
{
return lChild;
}
set
{
lChild = value;
}
}

public Node<T> RChild
{
get
{
return rChild;
}
set
{
rChild = value;
}
}
  }
  
        public class BiTree<T>
        {
            private Node<T> head;            public Node<T> Head
            {
                get
                {
                    return head;
                }
                set
                {
                    head = value;
                }
            }            public BiTree()
            {
                head = null;
            }            public BiTree(T val)
            {
                Node<T> p = new Node<T>(val);
                head = p;
            }            public BiTree(T val, Node<T> lp, Node<T> rp)
            {
                Node<T> p = new Node<T>(val, lp, rp);
                head = p;
            }            public bool isEmpty()
            {
                if (head == null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
public Node<T> GetLChild(Node<T> p) 
            {
                return p.LChild;  
            }
            public Node<T> GetRChild(Node<T> p) 
            {
                return p.RChild;  
            }
}        
class Class1
{
static void Main(string[] args)
{
string []a = {"a","acg","b","ahh","aah"};
BiTree<string> T = new BiTree<string>();
Node<string> head = T.Head;
for (int i = 0; i < 5; i++)
{
T.Insert(head, a[i]);
}
InOrder(head);
return;
}

static void InOrder(Node<string> head)
{
if (head == null)
{
return;
}
InOrder(head.LChild);
Console.WriteLine("{0}", head.Data);
InOrder(head.RChild);
}
public void Insert(BiTree<string> bt, string key)
{
Node<string> p;
Node<string> parent = new Node<string>(); p = bt.Head;
while (p != null)
{
parent = p; if (String.Compare(p.Data , key) < 0)
{
p = p.RChild;
}
else
{
p = p.LChild;
}
} p = new Node<string>(key); if (parent == null)
{
bt.Head = p;
}
else if (String.Compare(p.Data , parent.Data) < 0 )
{
parent.LChild = p;
}
else
{
parent.RChild = p;
}
                // return 0;
} }

解决方案 »

  1.   

    添加一个节点最好是由BiTree来做(名字上Add比Insert好):
    public class BiTree<T> where T : IComparable<T>
    {
        //...
        public Node<T> Add(T val)
        {
            Node<T> node = new Node<T>(val);
            Add(this.Head, node);
            return node;
        }    private void Add(Node<T> parent, Node<T> n)
        {
            if (parent == null)
            {
                this.Head = n;
            }
            else if (n.Data.CompareTo(parent.Data) <= 0)
            {
                if (parent.LChild == null) parent.LChild = n;
                else Add(parent.LChild, n);
            }
            else
            {
                if (parent.RChild == null) parent.RChild = n;
                else Add(parent.RChild, n);
            }
        }
    }
      

  2.   

    这样的话输出怎么写?不出现stackoverflowexception情况