Data structures and algorithms (C # implementation) series --- N-tree (2)
Heavenkiller (original)
Public override uint Degree
{
Get
{
Return this. degree;
}
}
// Only used for empty Tree nodes
Public virtual void AttachKey (object _ obj)
{
If (! IsEmpty ())
Throw new Exception ("My: this node must be a empty tree node! ");
This. key = _ obj;
This. treeList = new ArrayList (); // generates a degree long array and initializes it as an empty tree.
This. treeList. Capacity = (int) this. degree;
For (int I = 0; I <this. degree; I ++)
{
TreeList. Add (new NaryTree (this. degree ));
}
/*
Foreach (object tmpObj in this. treeList)
{
TmpObj = new NaryTree (this. degree );
}
*/
}
// Only used for leaf nodes. The leaf node is changed to an empty node and the reference of the leaf node keyword is returned.
Public virtual object DetachKey ()
{
If (! IsLeaf ())
Throw new Exception ("My: this node must be a leaf node! ");
Object result = this. key; // store this leaf node temporary
This. key = null;
This. treeList = null;
Return result;
}
// Connect the subtree to the num node of the specified tree, provided that the node must be a null node with the same degree; otherwise, an exception is thrown.
Public virtual void AttachSubtree (uint num, NaryTree _ naryTree)
{
If (this. IsEmpty ())
Throw new Exception ("My: it cant be a empty tree! ");
If (! (This [num-1]. IsEmpty () | this. degree! = _ NaryTree. degree)
Throw new Exception ("My: this [I-1] must be empty and they shoshould have the same degree! ");
This [num-1] = _ naryTree;
}
// It is defined only as a non-empty tree. It is deleted from the given tree and connected to an empty tree. The degree is the same and the deleted subtree reference is returned.
Public virtual NaryTree DetachSubtree (uint num)
{
If (IsEmpty ())
Throw new Exception ("My: it cant be empty! ");
NaryTree tmpTree = this;
(NaryTree) this [num-1]). key = null;
(NaryTree) this [num-1]). treeList = null;
Return this;
}
}
}