Data structure and algorithm (C # Implementation) Series---tree (ii.)
Heavenkiller (original)
public class Inorder:iprepostvisitor
{
Private Ivisitor visitor;
Public inorder (Ivisitor _vis) {Visitor=_vis;}
#region Iprepostvisitor Members
public void Previsit (object _obj)
{
TODO: Add inorder.previsit implementation
}
public void Visit (object _obj)
{
TODO: Add inorder.visit implementation
This.visitor.Visit (_obj);
}
public void Postvisit (object _obj)
{
TODO: Add Inorder.postvisitor implementation
}
#endregion
}
public class Postorder:iprepostvisitor
{
Private Ivisitor visitor;
Public Postorder (Ivisitor _vis) {Visitor=_vis;}
#region Iprepostvisitor Members
public void Previsit (object _obj)
{
TODO: Add postorder.previsit implementation
}
public void Visit (object _obj)
{
TODO: Add postorder.visit implementation
}
public void Postvisit (object _obj)
{
TODO: Add Postorder.postvisitor implementation
This.visitor.Visit (_obj);
}
#endregion
}
Protected class Enumvisitor:ivisitor
{
Queue Thisqueue;
Public Enumvisitor (Queue _que)
{
This.thisqueue=_que;
}
#region Ivisitor Members
public void Visit (object _obj)
{
TODO: Add enumvisitor.visit implementation
This.thisQueue.Enqueue (_obj);
}
#endregion
}
#region IEnumerable Members
Public IEnumerator GetEnumerator ()
{
TODO: Add Tree.getenumerator implementation
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 I!--void Settraversaltype (Traversaltype _type)");
throw new Exception ("Warning:please set a travel type first!"); /if not set a type, a exception would happen
Break
}
return This.keyqueue.GetEnumerator ();
}
#endregion