明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

數(shù)據(jù)結(jié)構(gòu)與算法(C#完成)系列---樹(一)

[摘要]數(shù)據(jù)結(jié)構(gòu)與算法(C#實(shí)現(xiàn))系列---樹(一) Heavenkiller(原創(chuàng))首先我們給樹下一個(gè)定義:樹是一個(gè)有限的、非空的結(jié)點(diǎn)集,T=r or T1 or T2 or…or Tn它具有下列性質(zhì):1.集合指定的結(jié)點(diǎn)r...

       數(shù)據(jù)結(jié)構(gòu)與算法(C#實(shí)現(xiàn))系列---樹(一)

                                          Heavenkiller(原創(chuàng))

首先我們給樹下一個(gè)定義:

樹是一個(gè)有限的、非空的結(jié)點(diǎn)集,

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

它具有下列性質(zhì):

1.集合指定的結(jié)點(diǎn)r叫做樹的根結(jié)點(diǎn)

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

 

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

 

樹的主要性質(zhì)一個(gè)就是遍歷,分為深度遍歷和廣度遍歷

在這里分別實(shí)現(xiàn)為DepthFirstTravesal()和WidthFirstTravesal()

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

這是是采用適配器技術(shù)實(shí)現(xiàn)的。

 

using System;

using System.Collections;

 

namespace DataStructure

{

     /// <summary>

     /// Tree 的摘要說明。

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

     /// 支持枚舉、比較、深度復(fù)制

     /// </summary>

     public abstract class Tree:IEnumerable,IComparable

     {

         public Tree()

         {

              //

              // TODO: 在此處添加構(gòu)造函數(shù)邏輯

              //

         }

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

         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使用不同的訪問者來進(jìn)行前序、后序、中序遍歷

        

        

              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------------------------------------

         //內(nèi)部成員類 用于提供不同遍歷類型的訪問者

         public class PreOrder:IPrePostVisitor

         {

              private IVisitor visitor;

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

              #region IPrePostVisitor 成員

 

              public void PreVisit(object _obj)

              {

                   // TODO:  添加 PreOrder.PreVisit 實(shí)現(xiàn)

                   this.visitor.Visit(_obj);

              }

 

              public void Visit(object _obj)

              {

                   // TODO:  添加 PreOrder.Visit 實(shí)現(xiàn)

              }

 

              public void PostVisit(object _obj)

              {

                   // TODO:  添加 PreOrder.PostVisitor 實(shí)現(xiàn)

              }

 

              #endregion

 

         }