代码改变世界

建一棵二叉树,能够输入树的各个结点,并能够输出用不同方法遍历的遍历序列;分别建立二叉树存储结构的的输入函数、输出层序遍历序列的函数、输出先序遍历序列的函数

2013-04-13 18:40  呆河马  阅读(1328)  评论(0)    收藏  举报
using System;
using System.Collections.Generic;
using System.Text;
using Queue;

namespace Tree
{
     //二叉树的二叉链表的结点类的实现如下所示
    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 = rp;
            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
            {
                data = value;
            }
        }

        //左孩子属性
        public Node<T> LChild
        {
            get
            {
                return lChild;
            }
            set
            {
                lChild = value;
            }
        }

        //右孩子属性
        public Node<T> RChild
        {
            get
            {
                return rChild;
            }
            set
            {
                rChild = value;
            }
        }
    }

    //不带头结点的二叉树的二叉链表的类BiTree<T>
    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> Root()
        {
            return head;
        }

        //获取结点的左孩子结点
        public Node<T> GetLChild(Node<T> p)
        {
            return p.LChild;
        }

        //获取结点的右孩子结点
        public Node<T> GetRChild(Node<T> p)
        {
            return p.RChild;
        }

        //将结点p的左子树插入值为val的新结点
        //原来的左子树成为新结点的左子树
        public void InsertL(T val, Node<T> p)
        {
            Node<T> tmp = new Node<T>(val);
            tmp.LChild = p.LChild;
            p.LChild = tmp;
        }

        //将结点p的右子树插入值为val的新结点
        //原来的右子树成为新结点的右子树
        public void InsertR(T val, Node<T> p)
        {
            Node<T> tmp = new Node<T>(val);
            tmp.RChild = p.RChild;
            p.RChild = tmp;
        }

        //若p非空,删除p的左子树
        public Node<T> DeleteL(Node<T> p)
        {
            if ((p == null) || (p.LChild == null))
            {
                return null;
            }

            Node<T> tmp = p.LChild;
            p.LChild=null;

            return tmp;
        }

        //若p非空,删除p的右子树
        public Node<T> DeleteR(Node<T> p)
        {
            if((p==null)||(p.RChild==null))
            {
                return null;
            }

            Node<T> tmp=p.RChild;
            p.RChild=null;

            return tmp;
        }

        //判断是否是叶子结点
        public bool IsLeaf(Node<T> p)
        {
            if((p!=null)&&(p.LChild==null)&&(p.RChild==null))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
//先序遍历
        public void PreOrder(Node<T> root)
        {
            //根结点为空
            if (root == null)
            {
                return;
            }

            //处理根结点
            Console.WriteLine("{0}", root.Data);

            //先序遍历左子树
            PreOrder(root.LChild);

            //先序遍历右子树
            PreOrder(root.RChild);
        }

        //中序遍历
        public void InOrder(Node<T> root)
        {
            //根结点为空
            if (root == null)
            {
                return;
            }

            //中序遍历左子树
            InOrder(root.LChild);

            //处理根结点
            Console.WriteLine("{0}", root.Data);

            //中序遍历右子树
            InOrder(root.RChild);
        }

        //后序遍历
        public void PostOrder(Node<T> root)
        {
            //根结点为空
            if (root == null)
            {
                return;
            }

            //后序遍历左子树
            PostOrder(root.LChild);

            //后序遍历右子树
            PostOrder(root.RChild);

            //处理根结点
            Console.WriteLine("{0}", root.Data);
        }

        //层序遍历
        public void LevelOrder(Node<T> root)
        {
            //根结点为空
            if (root == null)
            {
                return;
            }

            //设置一个队列保存层序遍历结点
            CSeqQueue<Node<T>> sq = new CSeqQueue<Node<T>>(50);

            //根结点入队
            sq.In(root);

            //队列非空,结点没有处理完
            while (!sq.IsEmpty())
            {
                //结点出队
                Node<T> tmp = sq.Out();

                //处理当前结点
                Console.WriteLine("{0}", tmp);

                //将当前结点的左孩子结点入队
                if (tmp.LChild != null)
                {
                    sq.In(tmp.LChild);
                }
                

                //将当前结点的右孩子结点入队
                if (tmp.RChild != null)
                {
                    sq.In(tmp.RChild);
                }
            }
        }

        //编写算法,在二叉树中查找值为value的结点
       public  Node<T> Search(Node<T> root, T value)
        {
            Node<T> p = root;

            if (p == null)
            {
                return null;
            }

            if (!p.Data.Equals(value))
            {
                return p;
            }

            if (p.LChild != null)
            {
                return Search(p.LChild, value);
            }

            if (p.RChild != null)
            {
                return Search(p.RChild, value);
            }

            return null;
        }

        //统计出二叉树中叶子结点的数目
        public int CountLeafNode(Node<T> root)
        {
            if (root == null)
            {
                return 0;
            }
            else
                if (root.RChild == null && root.RChild == null)
                {
                    return 1;
                }
                else
                {
                    return (CountLeafNode(root.LChild) + CountLeafNode(root.RChild));
                }
        }

        //编写算法,求二叉树的深度
        public int GetHeight(Node<T> root)
        {
            int lh;
            int rh;

            if (root == null)
            {
                return 0;
            }
            else
                if (root.LChild == null && root.RChild == null)
                {
                    return 1;
                }
                else
                {
                    lh = GetHeight(root.RChild);
                    rh = GetHeight(root.RChild);
                    return (lh > rh ? lh : rh) + 1;
                }
        }
    }
}