欢迎光临
我们一直在努力

c#的binarytree实现-.net教程,算法/线

建站超值云服务器,限时71元/月

由于在一个程序中需要二叉树,所以在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;
                }
  
                return false;
        }
  
        public void maketree(
                object element,
                binarytree left,
                binarytree right
                )
        {
                _root = new binarytreenode(element, left._root, right._root);
  
                left._root = null;
                right._root = null;
        }
  
        public void breaktree(
                object element,
                binarytree left,
                binarytree right
                )
        {
                element = _root._data;
                left._root = _root._leftchild;
                right._root = _root._rightchild;
                _root = null;
        }
  
        /// <summary>
        /// 前序遍历
        /// </summary>
        /// <param name="v"></param>
        /// <param name="t"></param>
        public void preorder(visitor v, binarytreenode t)
        {
                if(t != null)
                {
                        v(t);
                        preorder(v, t._leftchild);
                        preorder(v, t._rightchild);
                }
        }
  
        /// <summary>
        /// 中序遍历
        /// </summary>
        /// <param name="v"></param>
        /// <param name="t"></param>
        public void inorder(visitor v, binarytreenode t)
        {
                if(t != null)
                {
                        inorder(v, t._leftchild);
                        v(t);
                        inorder(v, t._rightchild);
                }
        }
  
        /// <summary>
        /// 后序遍历
        /// </summary>
  
        /// <param name="v"></param>
        /// <param name="t"></param>
        public void postorder(visitor v, binarytreenode t)
        {
                if(t != null)
                {
                        postorder(v, t._leftchild);
                        postorder(v, t._rightchild);
                        v(t);
                }
        }
  
        /// <summary>
        /// 逐层遍历
        /// </summary>
        /// <param name="v"></param>
        public void levelorder(visitor v)
        {
                queue __q;
                binarytreenode __t;
  
                __t = _root;
                __q = new queue();
  
                while(__t != null)
                {
                        v(__t);
  
                        if(__t._leftchild != null)
                        {
                                __q.enqueue(__t._leftchild);
                        }
  
                        if(__t._rightchild != null)
                        {
                                __q.enqueue(__t._rightchild);
                        }
  
                        try
                        {
                                __q.dequeue();
                        }
                        catch
                        {
                                return;
                        }
                }
        }
  
        public int height(binarytreenode t)
        {
                int __leftheight, __rightheight;
  
                if(t == null)
                {
                        return 0;
                }
  
                __leftheight = height(t._leftchild);
                __rightheight = height(t._rightchild);
  
                if(__leftheight > __rightheight)
                {
                        return ++__leftheight;
                }
  
                return ++__rightheight;
        }
  
        public int size
        {
                get
                {
                        _count = 0;
  
                        preorder(new visitor(add1), _root);
  
                        return _count;
                }
        }
  
        public void delete()
        {
                postorder(new visitor(free), _root);
  
                _root = null;
        }
  
        private static int _count;
  
        private static void add1(binarytreenode t)
        {
                _count ++;
        }
  
        private static void free(binarytreenode t)

        {
                t = null;
        }

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » c#的binarytree实现-.net教程,算法/线
分享到: 更多 (0)

相关推荐

  • 暂无文章