Data structure and algorithm (C # Implementation) Series---tree

Source: Internet
Author: User
Tags abstract abstract tree bool key reference return
Data | structure | algorithm

First we give the tree a definition: The tree is a finite, non-empty set of nodes, t={r} or T1 or T2 or...or Tn

It has the following properties:

1. The node r specified by the collection is called the root node of the tree

2. The remaining nodes can be divided into n subsets, T1,t2,... Tn (n>=0), where each subset is a tree.

Other definitions of the tree such as degrees, leaves, advanced please consult other information, everywhere.

The main nature of a tree is traversal, divided into depth traversal and breadth traversal

Implemented here separately for Depthfirsttravesal () and Widthfirsttravesal ()

The depth traversal is divided into the preceding sequence traversal, the middle sequence traversal, and the sequential traversal, which is realized by the adapter technology.

Using System;

Using System.Collections;



Namespace Datastructure

{

<summary>

A summary description of the tree.

When Traverse, the Traversaltype can ' t be changed,or throw a exception

Supports enumerations, comparisons, and deep replication

</summary>

Public abstract class Tree:ienumerable,icomparable

{

Public Tree ()

{

//

TODO: Add constructor logic here

//

}

Protected queue Keyqueue=new Queue ()//is only used for enumeration when data is stored, not participating in the equals implementation comparison

Protected Traversaltype traversaltype=traversaltype.breadth;//Choose a traversal type,and Depthfirst is default

Protected uint Degree=0;//degree of the tree, init it as 0

Protected uint Height=0;//height of the tree, init it as 0



Enumerating the different traversal types

public enum Traversaltype

{

breadth=1,//Breadth Traversal

predepth=2,//Pre-sequence traversal

Sequential traversal in indepth=3,//

postdepth=4//subsequent traversal



};

Public virtual abstract Object key{}



Public abstract tree This[uint _index]{get;set;} Can I change it later?



public abstract object Key{get;}

public abstract uint Degree{get;}

public abstract uint Height{get;}

public void Settraversaltype (Traversaltype _type) {traversaltype=_type;} Set a traversal a type, if it's not set manually, Depthfirst is chosen in default



public abstract bool IsEmpty ()//property takes the place of IsEmpty ()

public abstract bool IsLeaf ();



Only Visit, Needn ' t queue

public virtual void Depthfirsttraversal (Iprepostvisitor _vis)//middle Depthfirst traversal

{

Using different visitors to _vis, sequential, and sequential traversal

if (! IsEmpty ())

{

_vis. Previsit (this. Key);



if (this. Degree>=1)

{

if (this. degree>=2)

{

for (UINT i=0;i<) (this. Degree-1); i++)//

{

This[i]. Depthfirsttraversal (_vis);//recursive call

_vis. Visit (this. Key);

}

}

This[this. DEGREE-1]. Depthfirsttraversal (_vis);

}



_vis. Postvisit (this. Key);

}


}


public virtual void Breadthfirsttraversal (Ivisitor _vis)//breadth-A-traversal

{

Queue tmpqueue=new queue ();//used to help Breadthfirsttraversal

This.keyqueue=new Queue ();//store keys



if (!this. IsEmpty ())

Tmpqueue.enqueue (this);//enqueue the root node at the

while (tmpqueue.count!=0)//until the number of the ' queue is zero

{

Tree headtree= (Tmpqueue.dequeue) ();

This.keyqueue.Enqueue (Headtree.key);

_vis. Visit (Headtree.key);



for (UINT i=0;i
{

Tree Childtree=headtree[i];

if (!childtree.isempty ())

Tmpqueue.enqueue (Childtree);

}

}



}

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

Overwrite Object.Equals ()---reference type realization

public override bool Equals (object _obj)

{

if (_obj==null)

Return false;//because this cannot be null

if (! (This. GetType () ==_obj. GetType ()))

Return false;//types are not equal or equal

Tree tmpobj= (_obj);

Comparing reference members

if (! Object.Equals (this. Key,tmpobj.key))

return false;



Comparing value type members

if (!this. Degree.equals (Tmpobj.degree))

return false;

if (!this. Height.equals (Tmpobj.height))

return false;



return true;

}

After this overload is ==,!=, you do not have to implement in a class that inherits later

public static bool operator== (tree _treea,tree _treeb)

{

return Object.Equals (_treea,_treeb);

}

public static bool Operator!= (tree _treea,tree _treeb)

{

Return! (_treea==_treeb);

}


#region IComparable Members



public virtual int CompareTo (object obj)

{

TODO: Add Tree.compareto implementation

return 0;

}



#endregion



}

}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.