Using C # to implement the data structure--tree (i)

Source: Internet
Author: User
Tags abstract abstract tree bool implement
Data structure and algorithm (C # Implementation) Series---tree (i.)

Heavenkiller (original)

First we give the tree a definition:

A 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 forward traversal, the middle sequence traversal, and the sequential traversal.

This is achieved using 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);

}

}

}

------------------------------------------------End------------------------------------

Internal member classes are used to provide accessors for different traversal types

public class Preorder:iprepostvisitor

{

Private Ivisitor visitor;

Public preorder (Ivisitor _vis) {Visitor=_vis;}

#region Iprepostvisitor Members

public void Previsit (object _obj)

{

TODO: Add preorder.previsit implementation

This.visitor.Visit (_obj);

}

public void Visit (object _obj)

{

TODO: Add preorder.visit implementation

}

public void Postvisit (object _obj)

{

TODO: Add Preorder.postvisitor implementation

}

#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.