資料結構與演算法(C#實現)系列---N叉樹(二) (轉)

themoney發表於2007-09-30
資料結構與演算法(C#實現)系列---N叉樹(二) (轉)[@more@]

  資料結構與演算法(實現)系列---N叉樹(二):namespace prefix = o ns = "urn:schemas--com::office" />

  Heavenkiller(原創)

public overr uint Degree

  {

  get

  {

  return this.degree;

  }

  }

 

  //------------------------------------------------------------------------------------- 

 

  //只用於空樹結點

  public virtual void AttachKey( _obj)

  {

  if(!IsEmpty())

  throw new Exception("My:this node must be a empty tree node!");

  this.key=_obj;

  this.treeList=new ArrayList();//產生一個degree長的陣列,並將其初始化為空樹

  this.treeList.Capacity=(int)this.degree;

  for(int i=0;i

  {

  treeList.Add(new NaryTree(this.degree));

  }

  /*

  foreach(object tmpObj in this.treeList)

  {

  tmpObj=new NaryTree(this.degree);

  }

  */

  }

  //只用於葉子結點,將葉子結點變為一個空結點,並返回葉子結點關鍵字的引用

  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個結點上,前提是這個結點必須是空結點,並且度數相同,否則丟擲異常

  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棵子樹並連上一個空樹,度數相同,並且返回刪除的子樹引用

  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;

  }

  //----------------------------------------------------------------------------------

 

  }

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10794571/viewspace-974273/,如需轉載,請註明出處,否則將追究法律責任。

相關文章