数据结构---二叉树(C#)

复制代码
转载:http://blog.csdn.net/fan158/article/details/5490028

public interface IBinTree<T> : IDateStructrue { IList<T> PreOrder(); IList<T> InOrder(); IList<T> PostOrder(); IList<T> LevelOrder(); void PreCreator(T[] items); BTNode<T> Root { get;} int LeafCount { get;} }
复制代码
复制代码
namespace DateStructrue
{
    public class BTNode<T>
    {
        public T Item { get; set; }
        public BTNode<T> RNode { get; set; }
        public BTNode<T> LNode { get; set; }

        public BTNode(T item) : this(item, null, null) { }

        public BTNode(T item, BTNode<T> lNode, BTNode<T> rNode)
        { this.Item = item; this.LNode = lNode; this.RNode = rNode; }
    }


    public class BinTree<T>:IBinTree<T>
    {
        private T[] _items;
        private int i=-1;

        private IList<T> _PreList = new List<T>();
        private IList<T> _InList = new List<T>();
        private IList<T> _PostList = new List<T>();
        private IList<T> _LevelList = new List<T>();

        #region IBinTree<T> Members

        public IList<T> PreOrder()
        {
            return Order(this.Root, OrderType.Pre);
        }

        public IList<T> InOrder()
        {
            return Order(this.Root, OrderType.In);
        }

        public IList<T> PostOrder()
        {
            return Order(this.Root, OrderType.Post);
        }

        public IList<T> LevelOrder()
        {
            Queue<T> q = new Queue<T>();
            var node = this.Root;

            q.EnQueue(node.Item);

            while (q.Count > 0)
            {
                this._LevelList.Add(q.DeQueue());
                if (node.LNode != null) q.EnQueue(node.LNode.Item);
                if (node.RNode != null) q.EnQueue(node.RNode.Item);
            }
        }

        public void PreCreator(T[] items)
        {
            this._items = items;
            if (!Check) return;
            BTNode<T> head = PreCreatorT();
            this.Root = head;
        }

        public int LeafCount
        {
            get
            {
                return CountLeaf(this.Root);
            }
        }

        public BTNode<T> Root
        {
            get;
            private set;
        }

        #endregion

        #region IDateStructrue Members

        public int Count
        {
            get {
                return 0;
            }
        }

        public void Clear()
        {
            this.Root = null;
        }

        public bool IsEmpty
        {
            get { return this.Root == null; }
        }

        #endregion

        #region Helper

        private BTNode<T> PreCreatorT()
        {
            i++;
            if (_items[i].Equals(default(T))) return null;
            else
            {
                BTNode<T> node = new BTNode<T>(_items[i]);
                node.LNode = PreCreatorT();
                node.RNode = PreCreatorT();
                return node;
            }
        }
        private bool Check
        {
            get
            {
                int nullCount = 0;
                int dataCount = 0;
                foreach (T t in _items)
                {
                    if (t.Equals(default(T))) nullCount++;
                    else dataCount++;
                }
                return nullCount == dataCount + 1;
            }
        }
        private IList<T> Order(BTNode<T> node, OrderType type)
        {
            if (node == null) return null;
            switch (type)
            {
                case OrderType.Pre:
                    this._PreList.Add(node.Item);
                    Order(node.LNode, type);
                    Order(node.RNode, type);
                    return this._PreList;
                case OrderType.In:
                    Order(node.LNode, type);
                    this._InList.Add(node.Item);
                    Order(node.RNode, type);
                    return this._InList;
                case OrderType.Post:
                    Order(node.LNode, type);
                    Order(node.RNode, type);
                    this._PostList.Add(node.Item);
                    return this._PostList;
                default:
                    return null;
            }
        }
        private int CountLeaf(BTNode<T> node)
        {
            if (node == null) return 0;
            else
            {
                if (node.RNode == null && node.LNode == null)
                    return 1;
                else
                    return CountLeaf(node.LNode) + CountLeaf(node.RNode);
            }
        }
       
        #endregion

        enum OrderType { Pre, In, Post }
    }

    
}
复制代码

 

posted @   灬后知后觉  阅读(153)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示