using System;
using System.Collections.Generic;
using System.Text;namespace BinaryTree
{
    class Program
    {
        static void Main(string[] args)
        {
            BTree bt = new BTree();
            Node parent= null, current = null;
            int i = 1;
            while(i>0)
            {
                Console.WriteLine("Input an integer");
                i = int.Parse(Console.ReadLine());
                bt.find(i, ref parent, ref current);
                if (current != null) Console.WriteLine("{0}Already exist!", i);
                else
                {
                    bt.addNew(i, parent, current);
                    Console.WriteLine("{0} inserted!", i);
                }
            };
            return;
        }
    }
    class Node
    {
        public int ivalue;
        public Node lchild;
        public Node rchild;        public Node(int i, Node l, Node r)
        {
            ivalue = i;
            lchild = l;
            rchild = r;
        }
    }
    class BTree
    {
        public Node ROOT;
        public BTree()
        {
            ROOT = null;
        }        public bool contain(int element)
        {
            Node parent = null, currentNode = null;
            find(element, ref parent, ref currentNode);
            if(currentNode == null) return false;
            return true;
        }
        
        public bool add(int element)
        {
            Node tmp, parent = null, currentNode = null;
            find(element, ref parent, ref currentNode);
            if(currentNode != null)
            {
                return false;   // already there
            }
            else
            {
                tmp = new Node(element, null, null);
                if(parent == null) // empty tree
                    ROOT = tmp;
                else
                    if(element<parent.ivalue)
                        parent.lchild =tmp;
                    else
                        parent.rchild = tmp;
            }
            return true;
        }        // must make sure that the value is not in the tree
        public void addNew(int element, Node parent, Node currentNode)
        {
            //if(currentNode != null) return false;
            Node tmp;
            // currentNode should be null
            tmp = new Node(element, null, null);
            if (parent == null) // empty tree
                ROOT = tmp;
            else
                if (element < parent.ivalue)
                    parent.lchild = tmp;
                else
                    parent.rchild = tmp;
        }
                    
        public void find(int element, ref Node parent, ref Node currentNode)
        {
            currentNode = ROOT;
            parent = null;
            while ((currentNode != null) && (currentNode.ivalue != element))
            {
                parent = currentNode;
                if (element < currentNode.ivalue) // left
                    currentNode = currentNode.lchild;
                else    // right
                    currentNode = currentNode.rchild;
            }
        }                 
    }
}

解决方案 »

  1.   

    类中的成员变量为何用public修饰,用private是不是更好一些。
      

  2.   

    楼主应该是新学的把,这种写法不大好,比如说你类内嵌类,楼主是想像C/C++那样,有指针指向其他同类型的数据。但是你用类来这么实现就不大好了。在性能方面会有大大的折扣。由于C#是只有类,所以最好用数组的方式来实现二叉树,这时候每个节点存子节点就只是整型的数来标志位置而已了。在.net下,最好形成新的习惯。别为了贪方便,把成员全都置为public了,用属性的方式来封装private的成员,将来对你这个类能进行很好的扩展,比如lchild成员,如果需要在改变lchild的状态的时候执行重遍历的操作,如果写成public的话,你无法实现这点;如果写成private,用一个属性来封装它,那你就能在set里面执行重遍历操作,这样,在用户使用你这个类的时候,只要对lchild赋值,那你就能实现如上效果,不需要用户额外来调用方法了。
      从面向对象思想来说,一个成员的某个属性进行改变的时候,是需要触发某个事件,这点你应该在winform编程里面能体会到。
     综上:你的程序很幼稚,继续努力,你能做出非常好的二叉树的。ps:为什么要独立实现二叉树呢?如果实现N叉树不是更好吗?连二叉树都包容进去了
      

  3.   

    反驳楼上:
    1、类来这么实现就不大好了。在性能方面会有大大的折扣
    类的定义并不会产生多大的内存,当你class A;的时候系统只是生成一个句柄而已,当你new的时候生成对象,当你要保存东西的时候,在任何语言都需要内存的支持,所以是一样的。2、数组并不是最好实现二叉树的结构,因为会浪费非常多的空间,因为你要保证你的数组保存的是平衡二叉树才能节省资源,当你要保证节省资源的情况下,你需要生成AVL树,这样你的数组就要频繁的调整而导致性能的问题3、二叉树和N叉树的使用方式是不一样,二叉可以保证你查询的速度,N叉可以保证你查询的深度,所以是两个不同的概念综上:你的想法很幼稚,继续努力,你能理解什么是非常好的二叉树的。