Data structure and algorithm

Source: Internet
Author: User

The N-fork tree has the same degree of every node as n

Using System;
Using System.Collections;
Namespace Datastructure
{
<summary>
Summary description of the narytree. -----N-Fork Tree
</summary>
public class Narytree:tree
{
Member variables
PRotected Object key;
protected UINT Degree;
Protected ArrayList treelist=new ArrayList ();
protected UINT height=0;//temporarily defaults to 0
Create a 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 an n-forked 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.capacity;i++)
{
THIS.TREELIST.ADD (this. Getemptyinstance (_degree));
}
}
//-----------------------------------------------------------------
Protected virtual Object Getemptyinstance (UINT _degree)
{return new Narytree (_degree);}
//-------------------------------------------------------------------
Judge whether the empty tree
public override bool IsEmpty ()
{return this.key==null;}
Determine if it is a leaf node. If that is not an empty tree and each Shang tree is an empty tree, the 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;
}
}
Indexing device
public override tree This[uint _index]
{
Get
{
if (_index>=this.degree)
throw new Exception ("My:out of index!"); /If out of bounds, throw an exception
if (this. IsEmpty ())
Return null;//If it is an empty tree, the indexer returns a null
return (tree) this.treelist[(int) _index];
}
Set
{
this.treelist[(int) _index]=value;
}
}

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.