参考资料《Visual C# 2010 从入门到精通》 18.3.2 使用泛型构造二叉树类

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

namespace BinaryTreeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ////创建一棵int树
            Tree<int> tree1 = new Tree<int>(10);

            ////增加节点
            tree1.Insert(5);
            tree1.Insert(11);
            tree1.Insert(5);
            tree1.Insert(-12);
            tree1.Insert(15);
            tree1.Insert(0);
            tree1.Insert(14);
            tree1.Insert(-8);
            tree1.Insert(10);
            tree1.Insert(8);
            tree1.Insert(8);
            //输出结果
            tree1.WalkTreeLeftToRight();
            Console.WriteLine("\r\n");
            tree1.WalkTreeRightToLeft();

            Console.WriteLine("\r\n");

            Tree<string> tree2 = new Tree<string>("Hello");
            tree2.Insert("World");
            tree2.Insert("How");
            tree2.Insert("Are");
            tree2.Insert("You");
            tree2.Insert("Today");
            tree2.Insert("I");
            tree2.Insert("Hope");
            tree2.Insert("You");
            tree2.Insert("Are");
            tree2.Insert("Feeling");
            tree2.Insert("Well");
            tree2.Insert("!");
            //输出结果
            tree2.WalkTreeLeftToRight();
            Console.WriteLine("\r\n");
            tree2.WalkTreeRightToLeft();

            //
            Console.ReadLine();
        }
    }

    //二叉树类
    public class Tree<TItem> where TItem : IComparable<TItem>
    {
        //类的属性
        public TItem NodeData { get; set; }
        public Tree<TItem> LeftTree { get; set; }
        public Tree<TItem> RightTree { get; set; }

        //类的构造函数
        public Tree(TItem nodeValue)
        {
            this.NodeData = nodeValue;
            this.LeftTree = null;
            this.RightTree = null;
        }

        //增加节点
        public void Insert(TItem newItem)
        {
            TItem currentNodeValue = this.NodeData;

            if (currentNodeValue.CompareTo(newItem) > 0)
            {
                //左子树(newItem < currentNodeValue)
                if (this.LeftTree == null)
                {
                    this.LeftTree = new Tree<TItem>(newItem);   //创建一个新树
                }
                else
                {
                    this.LeftTree.Insert(newItem);              //递归调用Insert方法
                }
            }
            else
            {
                //右子树(newItem >= currentNodeValue)
                if (this.RightTree == null)
                {
                    this.RightTree = new Tree<TItem>(newItem);  //创建一个新树
                }
                else
                {
                    this.RightTree.Insert(newItem);             //递归调用Insert方法
                }
            }
        }

        //遍历树(左->右)
        public void WalkTreeLeftToRight()
        {
            //1.左子树
            if (this.LeftTree != null)
            {
                this.LeftTree.WalkTreeLeftToRight();
            }

            //2.节点
            Console.Write(this.NodeData.ToString() + " ");

            //3.右子树
            if (this.RightTree != null)
            {
                this.RightTree.WalkTreeLeftToRight();
            }
        }

        //遍历树(右->左)
        public void WalkTreeRightToLeft()
        {
            //1.右子树
            if (this.RightTree != null)
            {
                this.RightTree.WalkTreeRightToLeft();
            }

            //2.节点
            Console.Write(this.NodeData.ToString() + " ");

            //3.左子树
            if (this.LeftTree != null)
            {
                this.LeftTree.WalkTreeRightToLeft();
            }
        }
    }
}


本文转载:CSDN博客