二叉树遍历(遍历C#实现)

本文详细介绍了二叉树的基本概念及其先序、中序、后序遍历的方法,包括递归与非递归实现方式,并提供了一个具体的二叉树构建实例。
部署运行你感兴趣的模型镜像
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace imikity4_二叉树_
{
    //二叉树的结构
    public class Tree
    {
        public string value;
        public Tree Left;
        public Tree Right;
    }
    class Program
    {
        //二叉树的建树过程 :
       //            A
       //           / \
       //         B     C
       //        / \   / 
       //       D   E  F
       //      /    / 
       //     G    H
        public static Tree CreateTree()
        {
            Tree tree = new Tree() { value= "A"};
            tree.Left = new Tree()
            {
                value = "B",
                Left = new Tree() { value = "D", Left = new Tree() { value = "G" } },
                Right = new Tree() { value = "E", Left = new Tree() { value = "H" } },
            };
            tree.Right = new Tree() { value = "C", Left = new Tree() { value = "F" } };
            return tree;
        }


        //先序遍历的递归实现
        //ABDGEHCF
        public static void PreOrder(Tree tree)
        {
            if (tree ==null)
            {
                return;
            }
            System.Console.Write(tree.value);
            PreOrder(tree.Left);
            PreOrder(tree.Right);
        }
        //先序遍历的非递归实现
        public static void PreOrderNoRecursion(Tree tree)
        {
            if (tree ==null)
            {
                return;
            }
            System.Collections.Generic.Stack<Tree> stack = new System.Collections.Generic.Stack<Tree>();
            //记录当前节点
            while (tree!=null || stack.Count !=0)
            {
                while (tree!= null)
                {
                    Console.Write(tree.value);
                    stack.Push(tree);
                    tree = tree.Left;
                }
                if (stack.Count!=0)
                {
                    tree = stack.Pop();
                    tree = tree.Right;
                }
            }


        }


        //中序遍历的递归实现
        //GDBHEAFC
        public static void InOrder(Tree tree)
        {
            if (tree ==null)
            {
                return;
            }
            InOrder(tree.Left);
            Console.Write(tree.value);
            InOrder(tree.Right);
        }


        //中序遍历的非递归实现
        public static void InOrderNoRecursion(Tree tree)
        {
            if (tree ==null)
            {
                return;
            }
            System.Collections.Generic.Stack<Tree> stack = new System.Collections.Generic.Stack<Tree>();
            while (tree!=null || stack.Count!=0)
            {
                while (tree!=null)
                {
                    stack.Push(tree);
                    tree = tree.Left;
                }
                if (stack.Count != 0)
                {
                    tree = stack.Pop();
                    Console.Write(tree.value);
                    tree = tree.Right;
                }
            }
        }


        //后序遍历的递归
        //GDHEBFCA
        public static void lastOrder(Tree tree)
        {
            if (tree ==null)
            {
                return;
            }
            lastOrder(tree.Left);
            lastOrder(tree.Right);
            Console.Write(tree.value);
        }
        //此处为比较复杂的非递归后序遍历
        public static void lastOrderNoRecursion(Tree tree)
        {
            if (tree==null)
            {
                return;
            }
            System.Collections.Generic.Stack<Tree> s1 = new System.Collections.Generic.Stack<Tree>();
            //声明一个栈用来标记该节点是否被访问过(0:表示未被访问  1:表示已被访问)
            System.Collections.Generic.Stack<int> s2 = new System.Collections.Generic.Stack<int>();
            int index = 1;
            while (tree!=null || s1.Count!=0)
            {
                while (tree!=null)
                {
                    s1.Push(tree);
                    s2.Push(0);
                    tree = tree.Left;
                }
                while (s1.Count!=0 && s2.Peek()==index)
                {
                    s2.Pop();
                    Console.Write(s1.Pop().value);
                }
                if (s1.Count!=0)
                {
                    s2.Pop();
                    //已经被访问过
                    s2.Push(1);
                    tree = s1.Peek();
                    tree = tree.Right;
                }
            }
        }




        //函数的入口
        static void Main(string[] args)
        {
            Tree tree = CreateTree();
            PreOrder(tree);
            Console.WriteLine("\n");
            PreOrderNoRecursion(tree);
            Console.WriteLine("\n");
            InOrder(tree);
            Console.WriteLine("\n");
            InOrderNoRecursion(tree);
            Console.WriteLine("\n");
            lastOrder(tree);
            Console.WriteLine("\n");
            lastOrderNoRecursion(tree);
            Console.ReadLine();
        }
    }
}

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值