欢迎光临
我们一直在努力

数据结构与算法(c#实现)系列—树(二)_c#教程

建站超值云服务器,限时71元/月

       数据结构与算法(C#实现)系列—树(二)


                    Heavenkiller(原创)


 


         public class InOrder:IPrePostVisitor


         {


              private IVisitor visitor;


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


              #region IPrePostVisitor 成员


 


              public void PreVisit(object _obj)


              {


                   // TODO:  添加 InOrder.PreVisit 实现


              }


 


              public void Visit(object _obj)


              {


                   // TODO:  添加 InOrder.Visit 实现


                   this.visitor.Visit(_obj);


              }


 


              public void PostVisit(object _obj)


              {


                   // TODO:  添加 InOrder.PostVisitor 实现


              }


 


              #endregion


 


          }


         public class PostOrder:IPrePostVisitor


         {


              private IVisitor visitor;


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


              #region IPrePostVisitor 成员


 


              public void PreVisit(object _obj)


              {


                   // TODO:  添加 PostOrder.PreVisit 实现


              }


 


              public void Visit(object _obj)


              {


                   // TODO:  添加 PostOrder.Visit 实现


              }


 


              public void PostVisit(object _obj)


              {


                   // TODO:  添加 PostOrder.PostVisitor 实现


                   this.visitor.Visit(_obj);


              }


 


              #endregion


 


         }


         protected class EnumVisitor:IVisitor


         {


              Queue thisQueue;


              public EnumVisitor(Queue _que)


              {


                   this.thisQueue=_que;


              }


              #region IVisitor 成员


 


              public void Visit(object _obj)


              {


                   // TODO:  添加 EnumVisitor.Visit 实现


                   this.thisQueue.Enqueue(_obj);


              }


 


              #endregion


         }


 


 


 


 


         #region IEnumerable 成员


 


         public IEnumerator GetEnumerator()


         {


              // TODO:  添加 Tree.GetEnumerator 实现


              EnumVisitor vis=new EnumVisitor(this.keyqueue);


              switch (this.traversaltype)


              {


                   case TraversalType.Breadth:


                       BreadthFirstTraversal(vis);


                        break;


                   case TraversalType.PreDepth:


                       PreOrder preVis=new PreOrder(vis);


                       DepthFirstTraversal(preVis);                      


                       break;


                   case TraversalType.InDepth:


                       InOrder inVis=new InOrder(vis);


                       DepthFirstTraversal(inVis);                   


                       break;


                  case TraversalType.PostDepth:


                       PostOrder postVis=new PostOrder(vis);


                       DepthFirstTraversal(postVis);                     


                       break;


             


                   default:


                       Console.WriteLine(“WARNING:please set a travel type first!–void SetTraversalType(TraversalType _type) “);


                       //throw new Exception(“WARNING:please set a travel type first!”);//if not set a type, a exception will happen


                       break;


              }


              return this.keyqueue.GetEnumerator();


         }


 


         #endregion


 

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 数据结构与算法(c#实现)系列—树(二)_c#教程
分享到: 更多 (0)