成都网站建设设计

将想法与焦点和您一起共享

C#实现二叉树的遍历

    C#实现二叉树的前序、中序、后序遍历。

站在用户的角度思考问题,与客户深入沟通,找到灵武网站设计与灵武网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计制作、网站制作、企业官网、英文网站、手机端网站、网站推广、申请域名、网络空间、企业邮箱。业务覆盖灵武地区。

public class BinaryTreeNode
    {
        int value;
        BinaryTreeNode left;
        BinaryTreeNode right;

        /// 
        /// 前序遍历
        /// 
        /// 
        public static void PreOrder(BinaryTreeNode tree)
        {
            if (tree == null)
                return;

            System.Console.Write(tree.value + " ");
            PreOrder(tree.left);
            PreOrder(tree.right);
        }

        /// 
        /// 前序遍历循环实现
        /// 
        /// 
        public static void PreOrderLoop(BinaryTreeNode tree)
        {
            if (tree == null)
                return;
            Stack stack = new Stack();
            BinaryTreeNode node = tree;

            while(node != null || stack.Any()){
                if(node != null)
                {
                    stack.Push(node);
                    System.Console.Write(node.value + " ");
                    node = node.left;
                }
                else
                {
                    var item = stack.Pop();
                    node = item.right;
                }
            }
        }

        /// 
        /// 中序遍历
        /// 
        /// 
        public static void InOrder(BinaryTreeNode tree)
        {
            if (tree == null)
                return;

            InOrder(tree.left);
            System.Console.Write(tree.value + " ");
            InOrder(tree.right);
        }

        /// 
        /// 中序遍历循环实现
        /// 
        /// 
        public static void InOrderLoop(BinaryTreeNode tree)
        {
            if (tree == null)
                return;

            Stack stack = new Stack();
            BinaryTreeNode node = tree;

            while (node != null || stack.Any())
            {
                if (node != null)
                {
                    stack.Push(node);
                    node = node.left;
                }
                else
                {
                    var item = stack.Pop();
                    System.Console.Write(item.value + " ");
                    node = item.right;
                }
            }
        }

        /// 
        /// 后序遍历
        /// 
        /// 
        public static void PostOrder(BinaryTreeNode tree)
        {
            if (tree == null)
                return;

            PostOrder(tree.left);
            PostOrder(tree.right);
            System.Console.Write(tree.value + " ");
        }

        /// 
        /// 后序遍历循环实现1
        /// 
        /// 
        public static void PostOrderLoop(BinaryTreeNode tree)
        {
            if (tree == null)
                return;

            Stack stack = new Stack();
            BinaryTreeNode node = tree;
            BinaryTreeNode pre = null;
            stack.Push(node);

            while (stack.Any())
            {
                node = stack.Peek();
                if ((node.left == null && node.right == null) ||
                    (pre != null && (pre == node.left || pre == node.right)))
                {
                    System.Console.Write(node.value + " ");
                    pre = node;
                    stack.Pop();
                }
                else
                {
                    if (node.right != null)
                        stack.Push(node.right);

                    if (node.left != null)
                        stack.Push(node.left);
                }
            }
        }

        /// 
        /// 后续遍历循环实现2
        /// 
        /// 
        public static void PostOrderLoop2(BinaryTreeNode tree)
        {
            HashSet visited = new HashSet();
            Stack stack = new Stack();
            BinaryTreeNode node = tree;

            while (node != null || stack.Any())
            {
                if (node != null)
                {
                    stack.Push(node);
                    node = node.left;
                }
                else
                {
                    var item = stack.Peek();
                    if (item.right != null && !visited.Contains(item.right))
                    {
                        node = item.right;
                    }
                    else
                    {
                        System.Console.Write(item.value + " ");
                        visited.Add(item);
                        stack.Pop();
                    }
                }
            }
        }
    }

网站栏目:C#实现二叉树的遍历
链接URL:http://chengdu.cdxwcx.cn/article/gdieoj.html