首页 > 开发 > 综合 > 正文

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

2024-07-21 02:17:25
字体:
来源:转载
供稿:网友

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表