數(shù)據(jù)結(jié)構(gòu)與算法(C#實現(xiàn))系列---N叉樹(二)
2024-07-21 02:20:05
供稿:網(wǎng)友
數(shù)據(jù)結(jié)構(gòu)與算法(c#實現(xiàn))系列---n叉樹(二)
heavenkiller(原創(chuàng))
public override uint degree
{
get
{
return this.degree;
}
}
//-------------------------------------------------------------------------------------
//只用于空樹結(jié)點
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)生一個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é)點,將葉子結(jié)點變?yōu)橐粋€空結(jié)點,并返回葉子結(jié)點關(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個結(jié)點上,前提是這個結(jié)點必須是空結(jié)點,并且度數(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棵子樹并連上一個空樹,度數(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;
}
//----------------------------------------------------------------------------------
}
}
國內(nèi)最大的酷站演示中心!