二叉树的遍历包含递归和非递归实现

public class TreeNode<T> where T : new()
    {
        public T Data { get; set; }
        public TreeNode<T> LChild { get; set; }
        public TreeNode<T> RChild { get; set; }

        public TreeNode(T val, TreeNode<T> lChild, TreeNode<T> rChild)
        {
            Data = val;
            LChild = lChild;
            RChild = rChild;
        }
        public TreeNode(TreeNode<T> lChild, TreeNode<T> rChild)
        {
            Data = default(T);
            LChild = lChild;
            RChild = rChild;
        }
        public TreeNode(T val)
        {
            Data = val;
            LChild = null;
            RChild = null;
        }
        public TreeNode()
        {
            Data = default(T);
            LChild = null;
            RChild = null;
        }
    }

    public class BiTree<T> where T : new()
    {
        public TreeNode<T> Head { get; set; }
        public bool IsRecur { get; set; }
        public BiTree()
        {
            Head = null;
        }
        public BiTree(T val, TreeNode<T> lChild, TreeNode<T> rChild)
        {
            TreeNode<T> p = new TreeNode<T>(val, lChild, rChild);
            Head = p;
        }

        public BiTree(T val)
        {
            TreeNode<T> p = new TreeNode<T>(val);
            Head = p;
        }

        public bool IsEmpty()
        {
            if (null == Head)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public TreeNode<T> Root()
        {
            return Head;
        }

        public TreeNode<T> GetLChild(TreeNode<T> p)
        {
            return p.LChild;
        }

        public TreeNode<T> GetRChild(TreeNode<T> p)
        {
            return p.RChild;
        }

        public void InsertL(T val,TreeNode<T> p)
        {
            TreeNode<T> temp = new TreeNode<T>(val);
            temp.LChild =p.LChild;
            p.LChild = temp;
        }

        public void InsertR(T val, TreeNode<T> p)
        {
            TreeNode<T> temp = new TreeNode<T>(val);
            temp.RChild = p.RChild;
            p.RChild = temp;
        }

        public TreeNode<T> DeleteL(TreeNode<T> p)
        {
            if (null == p || null == p.LChild)
            {
                return null;
            }
            TreeNode<T> temp = p.LChild;
            p.LChild = null;
            return temp;
        }

        public TreeNode<T> DeleteR(TreeNode<T> p)
        {
            if (null == p || null == p.RChild)
            {
                return null;
            }
            TreeNode<T> temp = p.RChild;
            p.RChild = null;
            return temp;
        }

        public bool IsLeaf(TreeNode<T> p)
        {
            if (null != p && null == p.LChild && null == p.RChild)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public void PreOrder(TreeNode<T> root)
        {
            if (IsRecur)
            {
                if (null == root)
                {
                    return;
                }
                Console.WriteLine(root.Data);
                PreOrder(root.LChild);
                PreOrder(root.RChild);
            }
            else
            {
                TreeNode<T> p = root;
                Stack<TreeNode<T>> s = new Stack<TreeNode<T>>();
                while (p != null || s.Count != 0)
                {
                    while (p != null)
                    {
                        Console.WriteLine(p.Data);
                        s.Push(p);
                        p = p.LChild;
                    }
                    if (s.Count != 0)
                    {
                        p = s.Peek();
                        s.Pop();
                        p = p.RChild;
                    }

                }

            }
        }

        public void InOrder(TreeNode<T> root)
        {
            if (IsRecur)
            {


                if (null == root)
                {
                    return;
                }
                InOrder(root.LChild);
                Console.WriteLine(root.Data);
                InOrder(root.RChild);
            }
            else
            {
                TreeNode<T> p = root;
                Stack<TreeNode<T>> s = new Stack<TreeNode<T>>();
                while (p != null || s.Count != 0)
                {
                    while (p != null)
                    {
                        s.Push(p);
                        p = p.LChild;
                    }
                    if (s.Count != 0)
                    {
                        p = s.Peek();
                        Console.WriteLine(p.Data);
                        s.Pop();
                        p = p.RChild;
                    }

                }
            }
        }

        public void PostOrder(TreeNode<T> root)
        {
            if (IsRecur)
            {
                if (null == root)
                {
                    return;
                }
                PostOrder(root.LChild);
                PostOrder(root.RChild);
                Console.WriteLine(root.Data);
            }
            else
            {
                TreeNode<T> p = root;
                Stack<TreeNode<T>> s = new Stack<TreeNode<T>>();
                TreeNode<T> prev = null;
                while (p != null || s.Count != 0)
                {
                    while (p != null)
                    {
                        s.Push(p);
                        p = p.LChild;
                    }
                    p = s.Peek();
                    if (p.RChild == null || prev == p.RChild)
                    {
                        Console.WriteLine(p.Data);
                        s.Pop();
                        prev = p;
                        p = null;
                    }
                    else
                    {
                        p = p.RChild;
                    }  
                }
            }
        }

    }

posted on 2016-09-22 17:31  fendoudexiaoniao  阅读(234)  评论(0编辑  收藏  举报

导航