C#实现二叉树的遍历-创新互联-成都快上网建站

C#实现二叉树的遍历-创新互联

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

创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、成都做网站、宜黄网络推广、小程序设计、宜黄网络营销、宜黄企业策划、宜黄品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们大的嘉奖;创新互联建站为所有大学生创业者提供宜黄建站搭建服务,24小时服务热线:13518219792,官方网址:www.cdcxhl.com
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();
                    }
                }
            }
        }
    }

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


本文题目:C#实现二叉树的遍历-创新互联
标题URL:http://kswjz.com/article/degiop.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流