日期:2009-03-24  浏览次数:20485 次

由于在一个程序中需要二叉树,所以在C#中作了一个。
程序中共三个类:BinaryTreeNode、BinaryTree、Visitor。

Visitor是一个delegate。
  
public class BinaryTreeNode
{
        internal object _data;
        internal BinaryTreeNode _leftChild;
        internal BinaryTreeNode _rightChild;
  
        public BinaryTreeNode()
        {
        }
  
        public BinaryTreeNode(object e)
        {
                _data = e;
        }
  
        public BinaryTreeNode(object e,
                BinaryTreeNode left,
                BinaryTreeNode right
                )
        {
                _data = e;
                _leftChild = left;
                _rightChild = right;
        }
}
  
  
public delegate void Visitor(BinaryTreeNode u);
  
  
public class BinaryTree
{
        private BinaryTreeNode _root;
  
        public BinaryTree()
        {
        }
  
        public bool IsEmpty
        {
                get
                {
                        return _root == null;
                }
        }
  
        public bool Root(object x)
        {
                if(_root != null)
                {
                        x = _root._data;
                        return true;
                }
 &nb