C# 实现的一个二叉树类

原文地址 http://www.cnblogs.com/ppchouyou/archive/2008/07/18/1245819.html


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

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

 

namespace BinaryTree

{

    public class Tree where T:IComparable//where 指定TIComparable继承

    {

       ///

       /// 定义二叉树

       ///

       private T data;

       private Tree left;

       private Tree right;

       ///

       /// 构造函数:定义二叉树的根节点

       ///

       /// 二叉树的根节点

       public Tree(T nodeValue)

       {

           this.data = nodeValue;

           this.left = null;

           this.right = null;

        }

       ///

       /// 数据节点属性

       ///

       public T NodeData

       {

           get { return this.data; }

           set { this.data = value; }

       }

       ///

       /// 左子树

       ///

        public TreeLeftTree

       {

           get { return this.left; }

           set { this.left = value; }

       }

       ///

       /// 右子树

       ///

       public Tree RightTree

       {

           get { return this.right; }

           set { this.right = value; }

       }

       ///

       /// 向二叉叔中插入一个节点

       /// 存储思想,凡是小于该结点值的数据全部都在该节点的左子树中,凡是大于该结点结点值的数据全部在该节点的右子树中

       ///

       ///

       public void Insert(T newItem)

       {

           T currentNodeValue = this.NodeData;

           if (currentNodeValue.CompareTo(newItem) > 0)

           {

                if (this.LeftTree ==null)

                {

                    this.LeftTree = new Tree(newItem);

                }

                else

                {

                    this.LeftTree.Insert(newItem);

                }

           }

           else

           {

                if (this.RightTree== null)

                {

                    this.RightTree =new Tree(newItem);

                }

                else

                {

                    this.RightTree.Insert(newItem);

                }

           }

       }

       ///

       /// 前序遍历:先跟节点然后左子树,右子树

       ///

       ///

       public void PreOrderTree(Tree root)

       {

           if (root != null)

           {

                Console.Write(root.NodeData);

                PreOrderTree(root.LeftTree);

                PreOrderTree(root.RightTree);

           }

       }

       ///

       /// 中序遍历:左子树,根节点,右子树可以实现顺序输出

       ///

       ///

       public void InOrderTree(Tree root)

       {

           if (root != null)

           {

                InOrderTree(root.LeftTree);

                Console.Write(root.NodeData);

                InOrderTree(root.RightTree);

           }

       }

       ///

       /// 后序遍历:左子树,右子树,根节点

       ///

       ///

       public void PostOrderTree(Tree root)

       {

           if (root != null)

           {

                PostOrderTree(root.LeftTree);

                PostOrderTree(root.RightTree);

               Console.Write(root.NodeData);

           }

       }

       ///

       /// 逐层遍历:遍历思想是从根节点开始,访问一个节点然后将其左右子树的根节点依次放入链表中,然后删除该节点。

       /// 依次遍历直到链表中的元素数量为0即没有更下一层的节点出现时候为止。

       ///

       public void WideOrderTree()

        {

           List> nodeList = newList>();

           nodeList.Add(this);

           Tree 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();

       }

 

    }

}

你可能感兴趣的:(c#)