Data structures and algorithms (C # implementation) series-tree

Source: Internet
Author: User
Tags abstract tree
Heavenkiller (original)

First, let's define the tree:

A tree is a finite, non-empty node set,

T = {r} or T1 or T2 or... Or Tn

It has the following properties:

1. the node r specified in the set 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 tree definitions, such as degree, leaf, and higher, are available everywhere.

 

One of the main features of a tree is traversal, which can be divided into deep traversal and breadth traversal.

Here, DepthFirstTravesal () and WidthFirstTravesal () are implemented respectively ()

Deep traversal is divided into pre-order traversal, middle-order traversal, and post-order traversal.

This is achieved by using the adapter technology.

 

Using System;

Using System. Collections;

 

Namespace DataStructure

{

/// <Summary>

/// Tree summary.

/// When traverse, traversaltype can't be changed, or throw a exception

/// Supports enumeration, comparison, and deep Replication

/// </Summary>

Public abstract class Tree: IEnumerable, IComparable

{

Public Tree ()

{

//

// TODO: add the constructor logic here

//

}

Protected Queue keyqueue = new Queue (); // It is used only for data storage during enumeration and is not used for comparison in Equals implementation.

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

 

// Enumerate different traversal types

Public enum TraversalType

{

Breadth = 1, // span Traversal

PreDepth = 2, // pre-order traversal

InDepth = 3, // traverse in the middle order

PostDepth = 4 // post-order traversal

};

// Public virtual abstract object Key {}

Public abstract Tree this [uint _ index] {get; set;} // if I only use get, 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 will be 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

{

// Use different visitors through _ vis to perform forward, backward, and central 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 first 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 first

While (tmpQueue. Count! = 0) // until the number of the queue is zero

{

Tree headTree = (Tree) 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 ------------------------------------

// The internal Member class is used to provide visitors of different traversal types.

Public class PreOrder: IPrePostVisitor

{

Private IVisitor visitor;

Public PreOrder (IVisitor _ vis) {visitor = _ vis ;}

# Region IPrePostVisitor Member

 

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

 

}
Data structures and algorithms (C # implementation) series --- tree (2)

Heavenkiller (original)

 

Public class InOrder: IPrePostVisitor

{

Private IVisitor visitor;

Public InOrder (IVisitor _ vis) {visitor = _ vis ;}

# Region IPrePostVisitor Member

 

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 Member

 

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 Member

 

Public void Visit (object _ obj)

{

// TODO: Add EnumVisitor. Visit implementation

This. thisQueue. Enqueue (_ obj );

}

 

# Endregion

}

 

 

 

 

# Region IEnumerable Member

 

Public IEnumerator GetEnumerator ()

{

// TODO: Add 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

Data structures and algorithms (C # implementation) series --- tree (III)

Heavenkiller (original)

 

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

Public override bool Equals (object _ obj)

{

If (_ obj = null)

Return false; // this cannot be null

If (! (This. GetType () = _ obj. GetType ()))

Return false; // The type is not equal.

Tree tmpObj = (Tree) _ obj;

// Compare referenced members

If (! Object. Equals (this. Key, tmpObj. Key ))

Return false;

// Compare value type members

If (! This. Degree. Equals (tmpObj. Degree ))

Return false;

// If (! This. Height. Equals (tmpObj. Height ))

// Return false;

 

Return true;

}

// Reload here = ,! =, It is not necessary to implement

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 Member

 

Public virtual int CompareTo (object obj)

{

// TODO: Add Tree. CompareTo implementation

Return 0;

}

 

# Endregion

}

}

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.