文章索引 聚合服务页

工 具 文 档

您现在的位置: 创作设计空间 >> 站长教程 >> 编程技术 >> .NET >> 正文
站内文章搜索:

用C#实现数据结构--树(二)

作者:佚名    文章来源:站长之家    点击数:5242    更新时间:2009/1/6
 数据结构与算法(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

 ——来自:www.sinory.com


文章录入:文章录入:zhangmao    责任编辑:zm 
相关文章:
没有相关文章