惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - Nillson

传说中的Singleton.... 设计模式--简单工厂模式 策略模式 抽象类与接口 回顾一个面试题 再谈代理 常见的排序方法 预定义,宏定义 连接符,数值运算与函数 复杂查询 数据库中的Index和View的理解 重载和重写 采用递归的方法获得一棵树的所有叶节点 .NET中的新概念整理 4月要看的书 System.Runtime.InteropServices浅见 挂个牛人 一篇关于如何写注释的文章,值得收藏 Vistual Studio 2005到Vistual Studio 2008的版本转换问题 Visual Studio 2008 的一个Bug
C# 实现的一个二叉树类
Nillson · 2008-07-18 · via 博客园 - Nillson

昨天用C#写了一个二叉树的类,包括如何构造二叉树的根节点,向二叉树中插入一个节点顺便实现了一下二叉树的四种遍历方法:前序,中序,后序,逐层。前三种方法用了递归的方式,后一种方法用了一个链表来解决中间数据的存储问题。感觉这个东东确实包含了不少值得回味的东西。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace BinaryTree
{
    public class Tree<T> where T: IComparable<T>//where 指定T从IComparable<T>继承
    {
        /// <summary>
        /// 定义二叉树
        /// </summary>
        private T data;
        private Tree<T> left;
        private Tree<T> right;
        /// <summary>
        /// 构造函数:定义二叉树的根节点
        /// </summary>
        /// <param name="nodeValue">二叉树的根节点</param>
        public Tree(T nodeValue)
        {
            this.data = nodeValue;
            this.left = null;
            this.right = null;
        }
        /// <summary>
        /// 数据节点属性
        /// </summary>
        public T NodeData
        {
            get { return this.data; }
            set { this.data = value; }
        }
        /// <summary>
        /// 左子树
        /// </summary>
        public Tree<T> LeftTree
        {
            get { return this.left; }
            set { this.left = value; }
        }
        /// <summary>
        /// 右子树
        /// </summary>
        public Tree<T> RightTree
        {
            get { return this.right; }
            set { this.right = value; }
        }
        /// <summary>
        /// 向二叉叔中插入一个节点
        /// 存储思想,凡是小于该结点值的数据全部都在该节点的左子树中,凡是大于该结点结点值的数据全部在该节点的右子树中
        /// </summary>
        /// <param name="newItem"></param>
        public void Insert(T newItem)
        {
            T currentNodeValue = this.NodeData;
            if (currentNodeValue.CompareTo(newItem) > 0)
            {
                if (this.LeftTree == null)
                {
                    this.LeftTree = new Tree<T>(newItem);
                }
                else
                {
                    this.LeftTree.Insert(newItem);
                }
            }
            else
            {
                if (this.RightTree == null)
                {
                    this.RightTree = new Tree<T>(newItem);
                }
                else
                {
                    this.RightTree.Insert(newItem);
                }
            }
        }
        /// <summary>
        /// 前序遍历:先跟节点然后左子树,右子树
        /// </summary>
        /// <param name="root"></param>
        public void PreOrderTree(Tree<T> root)
        {
            if (root != null)
            {
                Console.Write(root.NodeData);
                PreOrderTree(root.LeftTree);
                PreOrderTree(root.RightTree);
            }
        }
        /// <summary>
        /// 中序遍历:左子树,根节点,右子树可以实现顺序输出
        /// </summary>
        /// <param name="root"></param>
        public void InOrderTree(Tree<T> root)
        {
            if (root != null)
            {
                InOrderTree(root.LeftTree);
                Console.Write(root.NodeData);
                InOrderTree(root.RightTree);
            }
        }
        /// <summary>
        /// 后序遍历:左子树,右子树,根节点
        /// </summary>
        /// <param name="root"></param>
        public void PostOrderTree(Tree<T> root)
        {
            if (root != null)
            {
                PostOrderTree(root.LeftTree);
                PostOrderTree(root.RightTree);
                Console.Write(root.NodeData);
            }
        }
        /// <summary>
        /// 逐层遍历:遍历思想是从根节点开始,访问一个节点然后将其左右子树的根节点依次放入链表中,然后删除该节点。
        /// 依次遍历直到链表中的元素数量为0即没有更下一层的节点出现时候为止。
        /// </summary>
        public void WideOrderTree()
        {
            List<Tree<T>> nodeList = new List<Tree<T>>();
            nodeList.Add(this);
            Tree<T> temp = null;
            while (nodeList.Count > 0)
            {
                Console.Write(nodeList[0].NodeData);
                temp = nodeList[0];
                nodeList.Remove(nodeList[0]);
                if (temp.LeftTree != null)
                {
                    nodeList.Add(temp.LeftTree);
                }
                if (temp.RightTree != null)
                {
                    nodeList.Add(temp.RightTree);
                }
            }
            Console.WriteLine();
        }

    }
}