CSDN博客

img heavenkiller

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

发表于2004/2/3 13:38:00  1211人阅读

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

相关博文

我的热门文章

img
取 消
img