數(shù)據(jù)結(jié)構(gòu)與算法(C#完成)系列---N叉樹(二)
發(fā)表時(shí)間:2024-01-14 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]數(shù)據(jù)結(jié)構(gòu)與算法(C#實(shí)現(xiàn))系列---N叉樹(二)Heavenkiller(原創(chuàng))public override uint Degree get return this.degree; ...
數(shù)據(jù)結(jié)構(gòu)與算法(C#實(shí)現(xiàn))系列---N叉樹(二)
Heavenkiller(原創(chuàng))
public override uint Degree
{
get
{
return this.degree;
}
}
//只用于空樹結(jié)點(diǎn)
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();//產(chǎn)生一個(gè)degree長的數(shù)組,并將其初始化為空樹
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);
}
*/
}
//只用于葉子結(jié)點(diǎn),將葉子結(jié)點(diǎn)變?yōu)橐粋(gè)空結(jié)點(diǎn),并返回葉子結(jié)點(diǎn)關(guān)鍵字的引用
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;
}
//將子樹連接到指定樹的第num個(gè)結(jié)點(diǎn)上,前提是這個(gè)結(jié)點(diǎn)必須是空結(jié)點(diǎn),并且度數(shù)相同,否則拋出異常
public virtual void AttachSubtree(uint num,NaryTree _naryTree)
{
if(this.IsEmpty())
throw new Exception("My:it can't 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 should have the same degree!");
this[num-1]=_naryTree;
}
//僅為非空樹定義,從給定樹中刪去它的第i棵子樹并連上一個(gè)空樹,度數(shù)相同,并且返回刪除的子樹引用
public virtual NaryTree DetachSubtree(uint num)
{
if (IsEmpty())
throw new Exception("My:it can't be empty! ");
NaryTree tmpTree=this;
((NaryTree)this[num-1]).key=null;
((NaryTree)this[num-1]).treeList=null;
return this;
}
}
}