C#二叉树简易实例

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        public class nodes<T>
        {
            T data;
            nodes<T> Lnode, Rnode, Pnode;

            public T Data  //
            {
                set { data = value; }
                get { return data; }
            }

            public nodes<T> LNode //
            {
                get { return Lnode; }
                set { Lnode = value; }
            }
            public nodes<T> RNode //
            {
                set { Rnode = value; }
                get { return Rnode; }
            }

            public nodes<T> PNode
            {
                set { Pnode = value; }
                get { return Pnode; }
            }
            public nodes() { }

            public nodes(T data)
            {
                this.data = data;
            }

            //先序遍历
            public static void PreOrder<T>(nodes<T> rootNode)
            {
                if (rootNode != null)
                {
                    Console.WriteLine(rootNode.Data);
                    PreOrder<T>(rootNode.LNode);
                    PreOrder<T>(rootNode.RNode);
                }
            }
            //中序遍历二叉树
            public static void MidOrder<T>(nodes<T> rootNode)
            {
                if (rootNode != null)
                {
                    MidOrder<T>(rootNode.LNode);
                    Console.WriteLine(rootNode.Data);
                    MidOrder<T>(rootNode.RNode);
                }
            }

            //后续遍历二叉树
            public static void AfterOrder<T>(nodes<T> rootNode)
            {
                if (rootNode != null)
                {
                    AfterOrder<T>(rootNode.LNode);
                    AfterOrder<T>(rootNode.RNode);
                    Console.WriteLine(rootNode.Data);
                }
            }
            //层次遍历
            public static void LayerOrder<T>(nodes<T> rootNode)
            {
                nodes<T>[] Nodes = new nodes<T>[20];
                int front = -1; //
                int rear = -1;  //
                if (rootNode != null)
                {
                    rear++;
                    Nodes[rear] = rootNode;
                }
                while (front != rear)
                {
                    front++;
                    rootNode = Nodes[front];
                    Console.WriteLine(rootNode.Data);
                    if (rootNode.LNode != null)
                    {
                        rear++;
                        Nodes[rear] = rootNode.LNode;
                    }
                    if (rootNode.RNode != null)
                    {
                        rear++;
                        Nodes[rear] = rootNode.RNode;
                    }
                }
            }

            //构造一颗已知的二叉树
           public  static nodes<string> BinTree()
            {
                nodes<string>[] binTree = new nodes<string>[8];    //创建结点          
                binTree[0] = new nodes<string>("A");
                binTree[1] = new nodes<string>("B");
                binTree[2] = new nodes<string>("C");
                binTree[3] = new nodes<string>("D");
                binTree[4] = new nodes<string>("E");
                binTree[5] = new nodes<string>("F");
                binTree[6] = new nodes<string>("G");
                binTree[7] = new nodes<string>("H");   //使用层次遍历二叉树的思想,构造一个已知的二叉树            
                binTree[0].LNode = binTree[1];
                binTree[0].RNode = binTree[2];
                binTree[1].RNode = binTree[3];
                binTree[2].LNode = binTree[4];
                binTree[2].RNode = binTree[5];
                binTree[3].LNode = binTree[6];
                binTree[3].RNode = binTree[7];         //返回二叉树的根结点           
                return binTree[0];
            }

        }
        static void Main(string[] args)
        {
            nodes<string> rootNode = nodes<string>.BinTree();

            Console.WriteLine("先序遍历二叉树");
            nodes<string>.PreOrder(rootNode);
            Console.WriteLine("中序遍历二叉树");
            nodes<string>.MidOrder(rootNode);
            Console.WriteLine("后序遍历二叉树");
            nodes<string>.AfterOrder(rootNode);
            Console.WriteLine("层次遍历二叉树");
            nodes<string>.LayerOrder(rootNode);
            Console.Read();
        }

    }
}
复制代码

 

posted on   HOT SUMMER  阅读(2115)  评论(0编辑  收藏  举报

编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· Ai满嘴顺口溜,想考研?浪费我几个小时
· Browser-use 详细介绍&使用文档
· 软件产品开发中常见的10个问题及处理方法
历史上的今天:
2012-04-23 正则第一天
2012-04-23 Des加密算法的PHP实现
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示