Data Structure and Algorithm (C # Implementation) Series --- N fork Tree (1)
HEAVENKILLER (original)
Each node of the n fork tree is the same,
Using system;
Using system.collections;
Namespace Datastructure
{
///
/// NaryTree's summary description. ----- N fork
/// summary>
Public Class Narytree: Tree
{
// Member Variables
Protected Object Key;
Protected uint degree;
protected arraylist treelist = new arraylist ();
// protected uint height = 0; // Temporary default is 0
// Create An Empty Tree Whose Attribute of Degree IS _DEGREE
Public NaryTree (uint _degree)
{
//
// TODO: Add constructor logic here
//
THIS.KEY = NULL;
this.degree = _degree;
THIS.TREELIST = NULL;
}
// Construct a n tok tree with a leaf node
Public Narytree (uint _degree, object _key)
{
THIS.KEY = _Key;
this.degree = _degree;
THIS.TREELIST = New ArrayList ();
THIS.TREELIST.CAPACITY = (int) _degree;
For (int i = 0; I { This.Treelist.add (this.GetemptyInstance); } } / / -------------------------------------------------------------------------------------------- ----------------- Protected Virtual Object GetemptyInstance (uint _degree) {Return New Narytree (_degree); / / -------------------------------------------------------------------------------------------- ------------------- // judge WHether the tree is an empty tree Public Override Bool ISempty () {Return this.key == NULL;} / / Decide whether it is a leaf node. If it is not empty tree and each sub-tree is empty, it is a junction of the leaves. Public override bool isleaf () { IF (iSempty ()) Return False; For (uint i = 0; i { IF (! (this [i] .Isempty ()))) Return False; } Return True; } // --------------------------------------------- ---------------------- Public Override Object Key { get { Return this.Key; } } // Indexer Public Override Tree this [uint _index] { get { IF (_index> = this.degree) Throw new Exception ("My: Out of Index!"); // If you are out, throw an exception IF (this.isempty ()) Return null; // If it is empty tree, the indexer returns a NULL Return (Tree) this.treeelist [(int) _INDEX]; } set { THIS.TREELIST [(int) _INDEX] = VALUE; } }