数据结构与算法(C#实现)系列---树(二)
发布时间:2006-10-14 3:12:52   收集提供:gaoqian

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

 

 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50