Data structures and algorithms (C # implementation) series --- N-tree (1)
Heavenkiller (original)
Each node of the N-tree has the same degree, which is N
Using System;
Using System. Collections;
Namespace DataStructure
{
/// <Summary>
/// Summary of the NaryTree. ----- N-tree
/// </Summary>
Public class NaryTree: Tree
{
// Member variables
Protected object key;
Protected uint degree;
Protected ArrayList treeList = new ArrayList ();
// Protected uint height = 0; // The default value is 0.
// Create an empty tree whose attribute of degree is _ degree
Public NaryTree (uint _ degree)
{
//
// TODO: add the constructor logic here
//
This. key = null;
This. degree = _ degree;
This. treeList = null;
}
// Construct an N-tree with leaf nodes
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. Capacity; I ++)
{
This. treeList. Add (this. GetEmptyInstance (_ degree ));
}
}
//-----------------------------------------------------------------
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 ;}
// Determine whether the node is a leaf node. If it is not an empty tree and each subtree is an empty tree, it is a leaf node.
Public override bool IsLeaf ()
{
If (IsEmpty ())
Return false;
For (uint I = 0; I <this. degree; I ++)
{
If (! (This [I]. IsEmpty ()))
Return false;
}
Return true;
}
// ------------------------------------- Inherited Attributes ---------------------------------
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 it is out of bounds, an exception is thrown.
If (this. IsEmpty ())
Return null; // if it is an empty tree, the indexer returns a null value.
Return (Tree) this. treeList [(int) _ index];
}
Set
{
This. treeList [(int) _ index] = value;
&