|
數(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; } } }
|
溫馨提示:喜歡本站的話,請收藏一下本站!