明辉站/网站教程/内容

数据结构与算法(C#完成)系列---树(二)

网站教程2024-01-10 阅读
[摘要]数据结构与算法(C#实现)系列---树(二) Heavenkiller(原创) public class InOrder:IPrePostVisitor private IVisitor visit...

       数据结构与算法(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

 

……

相关阅读