人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

數據結構與算法(C#完成)系列---樹(一)

數據結構與算法(C#完成)系列---樹(一)

更新時間:2022-07-19 文章作者:未知 信息來源:網絡 閱讀次數:

       數據結構與算法(C#實現)系列---樹(一)

                                          Heavenkiller(原創)

首先我們給樹下一個定義:

樹是一個有限的、非空的結點集,

T={r} or T1 or T2 or…or Tn

它具有下列性質:

1.集合指定的結點r叫做樹的根結點

2.其余的結點可以劃分成n個子集,T1,T2,…Tn(n>=0),其中每一個子集都是一棵樹。

 

樹的其它定義如度,葉子,高等就請大家查閱別的資料吧,到處都有的。

 

樹的主要性質一個就是遍歷,分為深度遍歷和廣度遍歷

在這里分別實現為DepthFirstTravesal()和WidthFirstTravesal()

其中深度遍歷又分為前序遍歷、中序遍歷、和后序遍歷

這是是采用適配器技術實現的。

 

using System;

using System.Collections;

 

namespace DataStructure

{

     /// <summary>

     /// Tree 的摘要說明。

     /// when traverse, traversaltype can't be changed,or throw a  exception

     /// 支持枚舉、比較、深度復制

     /// </summary>

     public abstract class Tree:IEnumerable,IComparable

     {

         public Tree()

         {

              //

              // TODO: 在此處添加構造函數邏輯

              //

         }

         protected Queue keyqueue=new Queue();//僅僅用于枚舉時存放數據,不參與Equals實現中的比較

         protected TraversalType traversaltype=TraversalType.Breadth;// choose a traversal  type,and  DepthFirst is default

         //protected uint degree=0;//degree of the tree, init  it as 0

         //protected uint height=0;//height of the tree, init it as 0

 

         //枚舉不同的遍歷類型

         public enum TraversalType

         {

              Breadth=1,//廣度遍歷

              PreDepth=2,//前序遍歷

              InDepth=3,//中序遍歷

              PostDepth=4//后序遍歷

             

         };

         //public virtual abstract object  Key{}

        

         public abstract Tree this[uint _index]{get;set;}//if I only use get, can I change it later?

 

         public  abstract object Key{get;}

         public  abstract uint Degree{get;}

         //public  abstract uint Height{get;}

         public  void SetTraversalType(TraversalType _type){traversaltype=_type;}//set a traversal a type, if it's not set manually, DepthFirst will be chosen in default

 

         public  abstract bool IsEmpty();// property takes the place of IsEmpty()

         public  abstract bool IsLeaf();

        

         //Only Visit, needn't queue

         public virtual void DepthFirstTraversal(IPrePostVisitor _vis)//middle depthfirst traversal

         {

              //通過_vis使用不同的訪問者來進行前序、后序、中序遍歷

        

        

              if(!IsEmpty())

              {

                   _vis.PreVisit(this.Key);

 

                   if( this.Degree>=1 )

                   {

                       if( this.Degree>=2)

                       {

                            for(uint i=0;i<(this.Degree-1);i++)//

                            {

                                 this[i].DepthFirstTraversal(_vis);//recursive call

                                 //_vis.Visit(this.Key);

                            }

                       }

                       this[this.Degree-1].DepthFirstTraversal(_vis);

                   }

 

                   _vis.PostVisit(this.Key);

              }

             

             

         }

        

         public virtual void BreadthFirstTraversal(IVisitor _vis)//breadth first traversal

         {

              Queue tmpQueue=new Queue();//used to help BreadthFirstTraversal

              //this.keyqueue=new Queue();//store keys

 

              if(!this.IsEmpty())

                   tmpQueue.Enqueue(this);//enqueue the root node at first

              while(tmpQueue.Count!=0)//until the number of the queue is zero

              {

                   Tree headTree=(Tree)tmpQueue.Dequeue();

                   //this.keyqueue.Enqueue(headTree.Key);

                   _vis.Visit(headTree.Key);

                  

                   for(uint i=0;i<headTree.Degree;i++)

                   {

                       Tree childTree=headTree[i];

                       if(!childTree.IsEmpty())

                            tmpQueue.Enqueue(childTree);

                   }

              }

 

         }

        

         //------------------------------------------------end------------------------------------

         //內部成員類 用于提供不同遍歷類型的訪問者

         public class PreOrder:IPrePostVisitor

         {

              private IVisitor visitor;

              public PreOrder(IVisitor _vis){visitor=_vis;}

              #region IPrePostVisitor 成員

 

              public void PreVisit(object _obj)

              {

                   // TODO:  添加 PreOrder.PreVisit 實現

                   this.visitor.Visit(_obj);

              }

 

              public void Visit(object _obj)

              {

                   // TODO:  添加 PreOrder.Visit 實現

              }

 

              public void PostVisit(object _obj)

              {

                   // TODO:  添加 PreOrder.PostVisitor 實現

              }

 

              #endregion

 

         }

 

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 昌宁县| 广饶县| 遂溪县| 闸北区| 宝清县| 东乌| 洞口县| 晋城| 绿春县| 青浦区| 拉孜县| 玉林市| 元江| 铁岭市| 醴陵市| 文安县| 寿宁县| 博湖县| 尚志市| 平原县| 开封县| 余干县| 望江县| 伊春市| 个旧市| 北流市| 禹州市| 县级市| 衡东县| 平顺县| 岳池县| 丰顺县| 海城市| 军事| 满洲里市| 同仁县| 安平县| 嘉荫县| 张家港市| 仁怀市| 辽阳市|